TmplEvent.tlh 2.4 KB

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