C11DelegateHandler.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // C11DelegateHandler.hpp
  2. #pragma once
  3. #include <functional>
  4. using std::placeholders::_1;
  5. using std::placeholders::_2;
  6. using std::placeholders::_3;
  7. using std::placeholders::_4;
  8. using std::placeholders::_5;
  9. #include "DelegateArg.hpp"
  10. class DelegateArgs;
  11. template <typename TA>
  12. class C11DelegateHandler;
  13. template <typename TA>
  14. void OnDelegateInvokeFailed (const C11DelegateHandler <TA> * h);
  15. //-----------------------------------------------------------------------------
  16. // C11DelegateHandler
  17. //-----------------------------------------------------------------------------
  18. template <typename TA>
  19. class C11DelegateHandler
  20. {
  21. public:
  22. typedef const void * tKey;
  23. typedef std::function <void (const void * sender, TA * arg)> DelegateFunction;
  24. public:
  25. inline C11DelegateHandler (tKey key, DelegateFunction fun)
  26. {
  27. m_Key = key;
  28. m_Function = fun;
  29. m_pThis = NULL;
  30. }
  31. virtual ~C11DelegateHandler () { }
  32. public:
  33. C11DelegateHandler (const C11DelegateHandler & h)
  34. {
  35. m_Key = h.m_Key;
  36. m_Function = h.m_Function;
  37. m_pThis = h.m_pThis;
  38. }
  39. C11DelegateHandler (C11DelegateHandler && h)
  40. {
  41. m_Key = h.m_Key;
  42. m_Function.swap (h.m_Function);
  43. m_pThis = h.m_pThis;
  44. }
  45. C11DelegateHandler & operator = (const C11DelegateHandler & h)
  46. {
  47. m_Key = h.m_Key;
  48. m_Function = h.m_Function;
  49. m_pThis = h.m_pThis;
  50. return (*this);
  51. }
  52. public:
  53. inline void operator () (const void * sender, TA * arg)
  54. {
  55. Invoke (sender, arg);
  56. }
  57. virtual void Invoke (const void * sender, TA * arg)
  58. {
  59. #ifndef _DEBUG
  60. try
  61. #endif
  62. {
  63. m_Function (sender, arg);
  64. }
  65. #ifndef _DEBUG
  66. catch (...)
  67. {
  68. Beep (1000, 1000);
  69. OnDelegateInvokeFailed (this);
  70. }
  71. #endif
  72. }
  73. virtual bool IsEqual (const C11DelegateHandler & handler) const
  74. {
  75. return (m_Key == handler.m_Key) &&
  76. (m_pThis == handler.m_pThis);
  77. }
  78. virtual bool IsEqual (const tKey key) const
  79. {
  80. return (m_Key == key);
  81. }
  82. virtual bool IsEqual (const tKey key, const void * pThis) const
  83. {
  84. return (m_Key == key) && (m_pThis == pThis);
  85. }
  86. bool operator == (const C11DelegateHandler & h) const
  87. {
  88. return this->IsEqual (h);
  89. }
  90. bool operator == (const tKey key) const
  91. {
  92. return this->IsEqual (key);
  93. }
  94. protected:
  95. DelegateFunction m_Function;
  96. tKey m_Key;
  97. const void * m_pThis;
  98. };
  99. //-----------------------------------------------------------------------------
  100. // C11ClassDelegateHandler
  101. //-----------------------------------------------------------------------------
  102. //
  103. // member function as handlers
  104. //
  105. template <typename T, typename TA>
  106. class C11ClassDelegateHandler : public C11DelegateHandler <TA>
  107. {
  108. typedef C11DelegateHandler <TA> inherited;
  109. public:
  110. typedef void (T::*tMemFunc) (const void *, TA *);
  111. inline C11ClassDelegateHandler (void * pFun, T * pthis, tMemFunc functor)
  112. : inherited (pFun, std::bind (functor, pthis, _1, _2))
  113. {
  114. m_pThis = pthis;
  115. }
  116. public:
  117. // 此联合的目的是把指向成员函数的指针转换成 void *
  118. // 按照 C++ 语言的规范, 指向成员函数的指针是无法通过任何正常 cast 机制转换成 void *
  119. template <typename T>
  120. union tMemFunToVoid
  121. {
  122. typedef void (T::*tMemFunc) (const void *, TA *);
  123. void * pFunc;
  124. tMemFunc pMemFunc;
  125. inline tMemFunToVoid (tMemFunc fun)
  126. {
  127. pMemFunc = fun;
  128. }
  129. };
  130. };
  131. template <typename TA>
  132. inline void OnDelegateInvokeFailed (const C11DelegateHandler <TA> * h)
  133. {
  134. try
  135. {
  136. // _fatal ("Invoke Delegate %d failed", h);
  137. }
  138. catch (...)
  139. {
  140. try
  141. {
  142. // _fatal ("Invoke Delegate %d failed", h);
  143. }
  144. catch (...)
  145. {
  146. }
  147. }
  148. }