SHARE
TWEET

Untitled

a guest Jun 17th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. #include <functional>
  5.  
  6. class Conn;
  7. typedef void (Conn::*ProcFn)();
  8.  
  9. class Conn
  10. {
  11.   private:
  12.   void proc_ping();
  13.  
  14.   static const std::unordered_map<std::string, ProcFn> handlers;
  15.  
  16.   public:
  17.   void simulate();
  18. };
  19.  
  20. const std::unordered_map<std::string, ProcFn> Conn::handlers = {
  21.   std::pair("ping", &Conn::proc_ping)
  22. };
  23.  
  24. void Conn::proc_ping()
  25. {
  26.   std::cout << "ping!" << std::endl;
  27. }
  28.  
  29. void Conn::simulate()
  30. {
  31.   auto pfn = handlers.at("ping");
  32.  
  33.   std::invoke(pfn, this);
  34. }
  35.  
  36. int main()
  37. {
  38.   Conn c;
  39.  
  40.   c.simulate();
  41. }
  42.      
  43. #include <iostream>
  44. #include <string>
  45. #include <unordered_map>
  46. #include <functional>
  47.  
  48. class ConnBase;
  49. typedef void (ConnBase::*ProcFn)();
  50.  
  51. class ConnBase
  52. {
  53.   protected:
  54.   void proc_ping();
  55. };
  56.  
  57. void ConnBase::proc_ping()
  58. {
  59.   std::cout << "ping!" << std::endl;
  60. }
  61.  
  62. class ConnMgmt : public ConnBase
  63. {
  64.   protected:
  65.   static const std::unordered_map<std::string, ProcFn> handlers;
  66.  
  67.   void proc_create_user();
  68.  
  69.   public:
  70.   void simulate();
  71. };
  72.  
  73. void ConnMgmt::proc_create_user()
  74. {
  75.   std::cout << "create user!" << std::endl;
  76. }
  77.  
  78. const std::unordered_map<std::string, ProcFn> ConnMgmt::handlers = {
  79.   std::pair("ping", &ConnBase::proc_ping),
  80.   std::pair("create_user", &ConnMgmt::proc_create_user)
  81. };
  82.  
  83. void ConnMgmt::simulate()
  84. {
  85.   auto pfn = handlers.at("ping");
  86.  
  87.   std::invoke(pfn, this);
  88.  
  89.   pfn = handlers.at("create_user");
  90.  
  91.   std::invoke(pfn, this);
  92. }
  93.  
  94. int main()
  95. {
  96.   ConnMgmt c;
  97.  
  98.   c.simulate();
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top