fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <memory>
  4.  
  5. template <typename T>
  6. struct Message
  7. {
  8. T a;
  9. };
  10.  
  11. template <typename T>
  12. class Connection
  13. {
  14. typedef std::function<void(Message<T>&)> msg_handle;
  15.  
  16. public:
  17. void SetHandle(msg_handle handle)
  18. {
  19. handle_ = handle;
  20. }
  21.  
  22. void Run()
  23. {
  24. Message<T> t;
  25. std::cout << "Run() from Connection" << std::endl;
  26. handle_(t);
  27. }
  28.  
  29. private:
  30. msg_handle handle_;
  31. };
  32.  
  33. template <typename T>
  34. class Server
  35. {
  36. public:
  37. void OnMessage(Message<T>& msg, std::shared_ptr<Connection<T>> remote)
  38. {
  39. std::cout << "OnMessage() from Server" << std::endl;
  40. }
  41. };
  42.  
  43.  
  44. int main()
  45. {
  46. auto server = std::make_shared<Server<int>>();
  47. auto connection = std::make_shared<Connection<int>>();
  48. connection->SetHandle([=](auto& m){ server->OnMessage(m, connection); });
  49. connection->Run();
  50. return 0;
  51. }
Success #stdin #stdout 0s 5656KB
stdin
Standard input is empty
stdout
Run() from Connection
OnMessage() from Server