TmplEvent.tlh 2.6 KB

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