C11BUSDelegate.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // C11UnsafeDelegate.hpp
  2. #pragma once
  3. #include <vector>
  4. #include "C11DelegateHandler.hpp"
  5. #include "EventArgs.BUS.hpp"
  6. //-----------------------------------------------------------------------------
  7. // BUSDelegate
  8. //-----------------------------------------------------------------------------
  9. class BUSDelegate
  10. {
  11. public:
  12. typedef EventArgs_BUSMessage BASE_TA;
  13. typedef C11DelegateHandler <BASE_TA> BUSHandler;
  14. protected:
  15. typedef std::pair <UINT32, BUSHandler> tPairHandler;
  16. std::vector <tPairHandler> m_handlers;
  17. public:
  18. BUSDelegate () = default;
  19. BUSDelegate (BUSDelegate && from)
  20. {
  21. swap (from);
  22. }
  23. BUSDelegate (const BUSDelegate &) = delete;
  24. BUSDelegate & operator = (const BUSDelegate &) = delete;
  25. BUSDelegate & operator = (BUSDelegate && from)
  26. {
  27. swap (from);
  28. return (*this);
  29. }
  30. void swap (BUSDelegate & from)
  31. {
  32. m_handlers.swap (from.m_handlers);
  33. OnNoResponse.swap (from.OnNoResponse);
  34. }
  35. virtual ~BUSDelegate () { }
  36. protected:
  37. #if 0
  38. // 按 Key 来查找
  39. inline bool IsExist (const UINT32 Command, const BUSHandler::tKey key) const
  40. {
  41. for (auto & h : m_handlers)
  42. {
  43. if (h.first == Command && h.second == key)
  44. return true;
  45. };
  46. return false;
  47. }
  48. // 按 Key + 类实例指针 来查找
  49. inline bool IsExist (const UINT32 Command, const BUSHandler::tKey key, const void * pThis) const
  50. {
  51. for (auto & h : m_handlers)
  52. {
  53. if (h.first == Command && h.second.IsEqual (key, pThis))
  54. return true;
  55. };
  56. return false;
  57. }
  58. #else
  59. inline bool IsExist (const UINT32 Command, const BUSHandler::tKey key) const
  60. {
  61. return false;
  62. }
  63. inline bool IsExist (const UINT32 Command, const BUSHandler::tKey key, const void * pThis) const
  64. {
  65. return false;
  66. }
  67. #endif
  68. public:
  69. inline void RemoveAll ()
  70. {
  71. m_handlers.clear ();
  72. }
  73. public:
  74. // lambda 表达式会到这里
  75. // Binder (bind 结果) 会到这里
  76. template <typename T>
  77. inline void Push (const UINT32 Command, const T & handler)
  78. {
  79. // printf ("\nEnter Push, Command : %d, handler : 0x%0llx", Command, & handler);
  80. // if (IsExist (Command, & handler)) return m_handlers.size ();
  81. BUSHandler h (& handler, handler);
  82. ForcePush (std::make_pair (Command, h));
  83. // return m_handlers.size ();
  84. }
  85. // 静态函数会到这里
  86. inline void Push (const UINT32 Command, void (*fn) (const void *, BASE_TA *))
  87. {
  88. if (IsExist (Command, fn)) return;
  89. BUSHandler h (fn, fn);
  90. ForcePush (std::make_pair (Command, h));
  91. }
  92. // 此函数用于静态函数的参数是 DelegateArgs 的继承类的情形. 例如
  93. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  94. template <typename TA>
  95. inline void Push (const UINT32 Command, void (*fn) (const void *, TA *))
  96. {
  97. if (IsExist (Command, fn)) return;
  98. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  99. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  100. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from EventArgs_BUSMessage");
  101. typedef void (*tRightFunc) (const void *, DelegateArgs *);
  102. tRightFunc tfn = reinterpret_cast <tRightFunc> (fn);
  103. BUSHandler h (tfn, tfn);
  104. ForcePush (std::make_pair (Command, h));
  105. }
  106. // 类成员函数
  107. // 模板参数可以自动推导, 因此以下 3 种写法都可以
  108. // D.Push <_MyTest> (&test, &_MyTest::OnCallback2);
  109. // D.Push <> (&test, &_MyTest::OnCallback2);
  110. // D.Push (&test, &_MyTest::OnCallback2);
  111. #if 0
  112. template <typename T>
  113. inline void Push (const UINT32 Command, T * inst, void (T::*mfn) (const void *, BASE_TA *))
  114. {
  115. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (mfn);
  116. if (IsExist (Command, un.pFunc, inst)) return;
  117. C11ClassDelegateHandler <T, BASE_TA> h (un.pFunc, inst, mfn);
  118. ForcePush (std::make_pair (Command, h));
  119. }
  120. #endif
  121. // 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  122. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  123. //#if (_MSC_VER > 1800)
  124. #if 1
  125. template <typename TC, typename TA>
  126. inline void Push (const UINT32 Command, TC * inst, void (TC::*mfn) (const void *, TA *))
  127. {
  128. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  129. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  130. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from EventArgs_BUSMessage");
  131. typedef void (TC::*tRightMemFunc) (const void *, BASE_TA *);
  132. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  133. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  134. if (IsExist (Command, un.pFunc, inst)) return;
  135. C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
  136. ForcePush (std::make_pair (Command, h));
  137. }
  138. #endif
  139. public:
  140. template <typename T>
  141. inline void Pop (const UINT32 Command, const T & handler)
  142. {
  143. DoPop (Command, & handler);
  144. }
  145. inline void Pop (const UINT32 Command, void (*fn) (const void *, BASE_TA *))
  146. {
  147. DoPop (Command, fn);
  148. }
  149. /// 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
  150. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  151. template <typename TA>
  152. inline void Pop (const UINT32 Command, void (*fn) (const void *, TA *))
  153. {
  154. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  155. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  156. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from EventArgs_BUSMessage");
  157. DoPop (Command, fn);
  158. }
  159. /// 模板参数可以自动推导, 因此以下 3 种写法都可以
  160. // D.Pop <_MyTest> (&test, &_MyTest::OnCallback2);
  161. // D.Pop <> (&test, &_MyTest::OnCallback2);
  162. // D.Pop (&test, &_MyTest::OnCallback2);
  163. #if 0
  164. template <typename T>
  165. inline void Pop (const UINT32 Command, T * inst, void (T::*mfn) (const void *, BASE_TA *))
  166. {
  167. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (mfn);
  168. DoPop (Command, un.pFunc, inst);
  169. }
  170. #endif
  171. /// 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  172. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  173. template <typename T, typename TA>
  174. inline void Pop (const UINT32 Command, T * inst, void (T::*mfn) (const void *, TA *))
  175. {
  176. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  177. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  178. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from EventArgs_BUSMessage");
  179. typedef void (T::*tRightMemFunc) (const void *, BASE_TA *);
  180. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  181. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (tfn);
  182. DoPop (Command, un.pFunc, inst);
  183. }
  184. public:
  185. inline void operator () (UINT32 Command, const void * sender, BASE_TA * arg)
  186. {
  187. Invoke (Command, sender, arg);
  188. }
  189. inline virtual void Invoke (UINT32 Command, const void * sender, BASE_TA * arg)
  190. {
  191. #if 0
  192. //#ifdef _DEBUG
  193. static int iEnter = 0;
  194. iEnter++;
  195. printf ("\r\n\r\n[%d] BUSDelegate.Invoke\r\n\r\n", iEnter);
  196. #endif
  197. for (auto & h : m_handlers)
  198. {
  199. if (h.first == Command)
  200. h.second (sender, arg);
  201. }
  202. }
  203. protected:
  204. inline void ForcePush (tPairHandler && handler)
  205. {
  206. m_handlers.push_back (handler);
  207. }
  208. protected:
  209. // 按 Key 来删除
  210. inline bool DoPop (UINT32 Command, const C11DelegateHandler <BASE_TA>::tKey key)
  211. {
  212. for (auto it = m_handlers.begin (); it != m_handlers.end (); it++)
  213. {
  214. auto & h = (*it);
  215. if ( (h.first == Command) && (h.second == key) )
  216. {
  217. m_handlers.erase (it);
  218. return true;
  219. }
  220. };
  221. return false;
  222. }
  223. // 按 Key + 类实例指针 来删除
  224. inline bool DoPop (UINT32 Command, const C11DelegateHandler <BASE_TA> ::tKey key, const void * pThis)
  225. {
  226. for (auto it = m_handlers.begin (); it != m_handlers.end (); it++)
  227. {
  228. auto & h = (*it);
  229. if ( (h.first == Command) && h.second.IsEqual (key, pThis))
  230. {
  231. m_handlers.erase (it);
  232. return true;
  233. }
  234. };
  235. return false;
  236. }
  237. public:
  238. _tUnsafeDelegate <EventArgs_BUSMessage> OnNoResponse;
  239. };