TmplEvent.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <list>
  2. #include <functional>
  3. namespace CXXHelper
  4. {
  5. template <typename... Args>
  6. class Event
  7. {
  8. protected:
  9. std::list <std::function <void (Args...)> > m_handlers;
  10. public:
  11. Event () { }
  12. Event (Event && from)
  13. {
  14. m_handlers.swap (from.m_handlers);
  15. }
  16. Event (const Event & from)
  17. {
  18. from.CopyTo (*this);
  19. }
  20. Event & operator = (const Event & from)
  21. {
  22. from.CopyTo (*this);
  23. return (*this);
  24. }
  25. Event & operator = (Event && from)
  26. {
  27. m_handlers.swap (from.m_handlers);
  28. return (*this);
  29. }
  30. void swap (Event & from)
  31. {
  32. m_handlers.swap (from.m_handlers);
  33. }
  34. void CopyTo (Event & to) const
  35. {
  36. to.m_handlers = m_handlers;
  37. }
  38. void Release ()
  39. {
  40. m_handlers.clear ();
  41. }
  42. virtual ~Event () { }
  43. public:
  44. inline void operator () (Args... arg)
  45. {
  46. Invoke (arg...);
  47. }
  48. inline void Invoke (Args... arg)
  49. {
  50. for (auto & h : m_handlers)
  51. h (arg...);
  52. }
  53. public:
  54. inline void RemoveAll ()
  55. {
  56. Release ();
  57. }
  58. inline bool IsEmpty () const
  59. {
  60. return m_handlers.empty ();
  61. }
  62. inline int GetSize () const
  63. {
  64. return (int)m_handlers.size ();
  65. }
  66. inline void AppendFrom (const Event & from)
  67. {
  68. for (auto h : from.m_handlers)
  69. this->m_handlers.push_back (h);
  70. }
  71. public:
  72. Event & operator += (Event & from)
  73. {
  74. AppendFrom (from);
  75. return (*this);
  76. }
  77. public:
  78. template <typename TA>
  79. Event & operator += (TA v)
  80. {
  81. Push (v);
  82. return (*this);
  83. }
  84. public:
  85. // lambda 表达式会到这里
  86. // Binder (bind 结果) 会到这里
  87. template <typename TX>
  88. // inline void Push (const TX & handler)
  89. inline void Push (TX handler)
  90. {
  91. m_handlers.push_back (std::move (handler));
  92. }
  93. // 静态函数会到这里
  94. inline void Push (void (*fn) (Args...))
  95. {
  96. m_handlers.push_back (std::move (fn));
  97. }
  98. // 类的成员函数会到这里 - 3 个
  99. // 这里用了个小技巧: 把类实例和成员函数转换成一个临时的 lambda 函数
  100. template <typename TC>
  101. inline void Push (TC * inst, void (TC::* mfn) (Args...))
  102. {
  103. m_handlers.push_back ([inst, mfn] (Args... args) {(*inst.*mfn) (args...); });
  104. }
  105. template <typename TC>
  106. void Push (TC * inst, void (TC::* mfn) (Args...) const)
  107. {
  108. m_handlers.push_back ([inst, mfn] (Args... args) {(*inst.*mfn) (args...); });
  109. }
  110. template<typename TC>
  111. void Push (const TC * inst, void (TC::* mfn) (Args...) const)
  112. {
  113. m_handlers.push_back ([inst, mfn] (Args... args) {(*inst.*mfn) (args...); });
  114. }
  115. };
  116. }