fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <tuple>
  4. #include <vector>
  5.  
  6. #define USE_MEDIATOR
  7.  
  8. using namespace std;
  9.  
  10. template<class T>
  11. void show(T tup)
  12. {
  13. cout << "-----------------" << endl;
  14. cout << get<0>(tup) << endl;
  15. cout << get<1>(tup) << endl;
  16. cout << get<2>(tup) << endl;
  17. cout << "-----------------" << endl;
  18. }
  19.  
  20. template <class T>
  21. class NotifyParam
  22. {
  23. public:
  24. NotifyParam(T body)
  25. :body(body)
  26. {}
  27. T body;
  28. };
  29.  
  30. #ifdef USE_MEDIATOR
  31.  
  32. class MediatorBase
  33. {
  34. public:
  35. virtual void doCommand(int which, void* notifyParam) = 0;
  36. };
  37.  
  38. template<class T1, class T2>
  39. class MediatorA : public MediatorBase
  40. {
  41. public:
  42. virtual void doCommand(int which, void* notifyParam)
  43. {
  44. cout << "-----------------" << endl;
  45. cout << "----MediatorA----" << endl;
  46. switch (which)
  47. {
  48. case 1:
  49. show(*static_cast<T1*>(notifyParam));
  50. break;
  51. case 2:
  52. show(*static_cast<T2*>(notifyParam));
  53. break;
  54. }
  55. }
  56. };
  57.  
  58. template<class T1, class T2>
  59. class MediatorB : public MediatorBase
  60. {
  61. public:
  62. virtual void doCommand(int which, void* notifyParam)
  63. {
  64. cout << "-----------------" << endl;
  65. cout << "----MediatorB----" << endl;
  66. switch (which)
  67. {
  68. case 1:
  69. show(*static_cast<T1*>(notifyParam));
  70. break;
  71. case 2:
  72. show(*static_cast<T2*>(notifyParam));
  73. break;
  74. }
  75. }
  76. };
  77.  
  78. #endif
  79.  
  80. int main ()
  81. {
  82. auto tup1 = make_tuple("A", 1, 0.3);
  83. NotifyParam<decltype(tup1)> pp1(tup1);
  84. show(pp1.body);
  85.  
  86. auto tup2 = make_tuple("B", "cc", 1);
  87. NotifyParam<decltype(tup2)> pp2(tup2);
  88. show(pp2.body);
  89.  
  90. #ifdef USE_MEDIATOR
  91.  
  92. vector<MediatorBase*> vt = {new MediatorA<decltype(tup1), decltype(tup2)>, new MediatorB<decltype(tup1), decltype(tup2)>};
  93.  
  94. for(auto it : vt)
  95. {
  96. it->doCommand(1, &pp1.body);
  97. it->doCommand(2, &pp2.body);
  98. }
  99.  
  100. #endif
  101.  
  102. return 0;
  103. }
  104.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
-----------------
A
1
0.3
-----------------
-----------------
B
cc
1
-----------------
-----------------
----MediatorA----
-----------------
A
1
0.3
-----------------
-----------------
----MediatorA----
-----------------
B
cc
1
-----------------
-----------------
----MediatorB----
-----------------
A
1
0.3
-----------------
-----------------
----MediatorB----
-----------------
B
cc
1
-----------------