Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <iostream>
- #include <boost/concept_check.hpp>
- #include <boost/type_erasure/any.hpp>
- #include <boost/type_erasure/member.hpp>
- #include <boost/type_erasure/free.hpp>
- #include <boost/type_erasure/builtin.hpp>
- #include <boost/mpl/vector.hpp>
- using namespace std;
- BOOST_TYPE_ERASURE_MEMBER((has_send), send)
- using Connection = boost::type_erasure::any<
- boost::mpl::vector<
- has_send<void(const char*)>,
- boost::type_erasure::copy_constructible<>>>;
- BOOST_TYPE_ERASURE_MEMBER((has_make), make)
- using ConnectionFactory = boost::type_erasure::any<
- boost::mpl::vector<
- has_make<Connection()>,
- boost::type_erasure::copy_constructible<>,
- boost::type_erasure::relaxed>>;
- struct TcpConnection
- {
- void send(const char* s) { cout << s << endl; }
- };
- struct UdpConnection
- {
- void send(const char* s) { cout << s << endl; }
- };
- struct TcpConnectionFactory
- {
- TcpConnection make() { return TcpConnection(); }
- };
- struct UdpConnectionFactory
- {
- UdpConnection make() { return UdpConnection(); }
- };
- template <class ConnFactory>
- void send(ConnFactory& cf)
- {
- Connection conn = cf.make();
- conn.send("hello");
- }
- int main(int argc, char* argv[])
- {
- ConnectionFactory cf;
- if (argc > 1 && argv[1] == "tcp"s)
- cf = TcpConnectionFactory();
- else
- cf = UdpConnectionFactory();
- send(cf);
- }
Add Comment
Please, Sign In to add comment