fork download
  1. // 注意 以下內容通通都省略了需要delete的問題
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6.  
  7. // 這可以不用看
  8. class _Event{
  9. public:
  10. template<typename T, typename TMemFun>
  11. void Attach(T* obj, TMemFun mf) {_handler = new _eventHandler<T, TMemFun>(obj, mf);}
  12. void _RaiseEvent() {_handler->f();}
  13. private:
  14. class _eventHandlerBase;
  15. private:
  16. _eventHandlerBase* _handler;
  17. private:
  18. class _eventHandlerBase{
  19. public:
  20. virtual void f() = 0;
  21. };
  22.  
  23. template<typename T, typename TMemFun>
  24. class _eventHandler : public _eventHandlerBase{
  25. public:
  26. _eventHandler(T* obj, TMemFun mf) : _obj(obj), _mf(mf) {}
  27.  
  28. // overrides
  29. void f() {(_obj->*_mf)();}
  30. private:
  31. T* _obj;
  32. TMemFun _mf;
  33. };
  34. };
  35.  
  36. // 假設出來的Happy
  37. class Happy{
  38. public:
  39. _Event OnSent;
  40. _Event OnReceive;
  41. _Event OnConnect;
  42. };
  43.  
  44. class Handler_Happy{
  45. public:
  46. Handler_Happy(Happy* happy) : _happy(happy) {}
  47.  
  48. // 假設處理函數都是長這樣的
  49. void OnSent() {std::cout << "OnSent: " << _happy << std::endl;}
  50. void OnReceive() {std::cout << "OnReceive: " << _happy << std::endl;}
  51. void OnConnect() {std::cout << "OnConnect: " << _happy << std::endl;}
  52.  
  53. private:
  54. Happy *_happy;
  55. };
  56.  
  57. void test(Happy* happys[], int size_happys)
  58. {
  59. using namespace std;
  60. vector<Handler_Happy> handlers;
  61.  
  62. for (int i=0; i<size_happys; i++){
  63. handlers.push_back(Handler_Happy(happys[i]));
  64. }
  65.  
  66.  
  67. for (int i=0; i<size_happys; i++){
  68. happys[i]->OnSent.Attach(&handlers[i], &Handler_Happy::OnSent);
  69. happys[i]->OnReceive.Attach(&handlers[i], &Handler_Happy::OnReceive);
  70. happys[i]->OnConnect.Attach(&handlers[i], &Handler_Happy::OnConnect);
  71. }
  72. }
  73.  
  74. int main()
  75. {
  76. Happy* happys[10];
  77. for (int i=0; i<10; i++)
  78. happys[i] = new Happy();
  79.  
  80. test(happys, 10); // 假設10個
  81.  
  82. for (int i=0; i<10; i++)
  83. happys[i]->OnSent._RaiseEvent();
  84.  
  85. for (int i=0; i<10; i++)
  86. happys[i]->OnReceive._RaiseEvent();
  87.  
  88. for (int i=0; i<10; i++)
  89. happys[i]->OnConnect._RaiseEvent();
  90.  
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
OnSent: 0
OnSent: 0x99d1018
OnSent: 0x99d1028
OnSent: 0x99d1038
OnSent: 0x99d1048
OnSent: 0x99d1058
OnSent: 0x99d1068
OnSent: 0x99d1078
OnSent: 0x99d1088
OnSent: 0x99d1098
OnReceive: 0
OnReceive: 0x99d1018
OnReceive: 0x99d1028
OnReceive: 0x99d1038
OnReceive: 0x99d1048
OnReceive: 0x99d1058
OnReceive: 0x99d1068
OnReceive: 0x99d1078
OnReceive: 0x99d1088
OnReceive: 0x99d1098
OnConnect: 0
OnConnect: 0x99d1018
OnConnect: 0x99d1028
OnConnect: 0x99d1038
OnConnect: 0x99d1048
OnConnect: 0x99d1058
OnConnect: 0x99d1068
OnConnect: 0x99d1078
OnConnect: 0x99d1088
OnConnect: 0x99d1098