Guest User

Untitled

a guest
Mar 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <boost/concept_check.hpp>
  4. #include <boost/type_erasure/any.hpp>
  5. #include <boost/type_erasure/member.hpp>
  6. #include <boost/type_erasure/free.hpp>
  7. #include <boost/type_erasure/builtin.hpp>
  8. #include <boost/mpl/vector.hpp>
  9. using namespace std;
  10.  
  11. BOOST_TYPE_ERASURE_MEMBER((has_send), send)
  12.  
  13. using Connection = boost::type_erasure::any<
  14. boost::mpl::vector<
  15. has_send<void(const char*)>,
  16. boost::type_erasure::copy_constructible<>>>;
  17.  
  18. BOOST_TYPE_ERASURE_MEMBER((has_make), make)
  19.  
  20. using ConnectionFactory = boost::type_erasure::any<
  21. boost::mpl::vector<
  22. has_make<Connection()>,
  23. boost::type_erasure::copy_constructible<>,
  24. boost::type_erasure::relaxed>>;
  25.  
  26. struct TcpConnection
  27. {
  28. void send(const char* s) { cout << s << endl; }
  29. };
  30.  
  31. struct UdpConnection
  32. {
  33. void send(const char* s) { cout << s << endl; }
  34. };
  35.  
  36. struct TcpConnectionFactory
  37. {
  38. TcpConnection make() { return TcpConnection(); }
  39. };
  40.  
  41. struct UdpConnectionFactory
  42. {
  43. UdpConnection make() { return UdpConnection(); }
  44. };
  45.  
  46. template <class ConnFactory>
  47. void send(ConnFactory& cf)
  48. {
  49. Connection conn = cf.make();
  50. conn.send("hello");
  51. }
  52.  
  53. int main(int argc, char* argv[])
  54. {
  55. ConnectionFactory cf;
  56. if (argc > 1 && argv[1] == "tcp"s)
  57. cf = TcpConnectionFactory();
  58. else
  59. cf = UdpConnectionFactory();
  60.  
  61. send(cf);
  62. }
Add Comment
Please, Sign In to add comment