C11SafeDelegate.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // C11SafeDelegate.hpp
  2. #pragma once
  3. #include <vector>
  4. #include "C11DelegateHandler.hpp"
  5. #include "FastExclusive.tlh"
  6. namespace UCT = ::Utility::Concurrent;
  7. //-----------------------------------------------------------------------------
  8. // SafeDelegate
  9. //-----------------------------------------------------------------------------
  10. class SafeDelegate
  11. {
  12. protected:
  13. typedef DelegateArgs BASE_TA;
  14. typedef C11DelegateHandler <BASE_TA> C11EventHandler;
  15. protected:
  16. UCT::ExclusiveVector <C11EventHandler> m_handlers;
  17. public:
  18. SafeDelegate () = default;
  19. SafeDelegate (SafeDelegate && from) = delete;
  20. /*
  21. {
  22. auto lock_this = m_handlers.Lock ();
  23. auto lock_from = from.m_handlers.Lock ();
  24. lock_this.As ()->swap (*lock_from.As ());
  25. }
  26. */
  27. SafeDelegate (const SafeDelegate &) = delete;
  28. SafeDelegate & operator = (const SafeDelegate &) = delete;
  29. SafeDelegate & operator = (SafeDelegate && from) = delete;
  30. /*
  31. {
  32. swap (from);
  33. return (*this);
  34. }
  35. void swap (SafeDelegate & from)
  36. {
  37. auto lock_this = m_handlers.Lock ();
  38. auto lock_from = from.m_handlers.Lock ();
  39. lock_this.As ()->swap (*lock_from.As ());
  40. }
  41. void CopyTo (SafeDelegate & from)
  42. {
  43. auto lock_this = m_handlers.Lock ();
  44. auto lock_from = from.m_handlers.Lock ();
  45. *lock_from.As () = *lock_this.As ();
  46. }
  47. */
  48. void Release ()
  49. {
  50. m_handlers.clear ();
  51. }
  52. virtual ~SafeDelegate () { }
  53. protected:
  54. #if 1
  55. // 按完整的 handler 来查找
  56. inline bool IsExist (const C11EventHandler & handler) const
  57. {
  58. return m_handlers.find (handler);
  59. }
  60. // 按 Key 来查找
  61. inline bool IsExist (const C11EventHandler::tKey key) const
  62. {
  63. return m_handlers.find_if ([& key] (const auto & Item)
  64. {
  65. return Item.IsEqual (key);
  66. });
  67. }
  68. // 按 Key + 类实例指针 来查找
  69. inline bool IsExist (const C11EventHandler::tKey key, const void * pThis) const
  70. {
  71. return m_handlers.find_if ([& key, pThis] (const auto & Item)
  72. {
  73. return Item.IsEqual (key, pThis);
  74. });
  75. }
  76. #else
  77. inline bool IsExist (const C11EventHandler & handler) const
  78. {
  79. return false;
  80. }
  81. inline bool IsExist (const C11EventHandler::tKey key) const
  82. {
  83. return false;
  84. }
  85. inline bool IsExist (const C11EventHandler::tKey key, const void * pThis) const
  86. {
  87. return false;
  88. }
  89. #endif
  90. public:
  91. inline virtual void RemoveAll ()
  92. {
  93. Release ();
  94. }
  95. inline bool IsEmpty () const
  96. {
  97. return m_handlers.empty ();
  98. }
  99. inline int GetSize () const
  100. {
  101. return (int) (m_handlers.size ());
  102. }
  103. public:
  104. // lambda 表达式会到这里
  105. // Binder (bind 结果) 会到这里
  106. template <typename T>
  107. inline void Push (const T & handler)
  108. {
  109. if (IsExist (& handler)) return;
  110. C11EventHandler h (& handler, handler);
  111. ForcePush (std::move (h));
  112. }
  113. // 静态函数会到这里
  114. inline void Push (void (*fn) (const void *, DelegateArgs *))
  115. {
  116. if (IsExist (fn)) return;
  117. C11EventHandler h (fn, fn);
  118. ForcePush (std::move (h));
  119. }
  120. // 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
  121. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  122. template <typename TA>
  123. inline void Push (void (*fn) (const void *, TA *))
  124. {
  125. if (IsExist (fn)) return;
  126. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  127. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  128. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  129. typedef void (*tRightFunc) (const void *, DelegateArgs *);
  130. tRightFunc tfn = reinterpret_cast <tRightFunc> (fn);
  131. C11EventHandler h (tfn, tfn);
  132. ForcePush (std::move (h));
  133. }
  134. // 类成员函数
  135. // 模板参数可以自动推导, 因此以下 3 种写法都可以
  136. // D.Push <_MyTest> (&test, &_MyTest::OnCallback2);
  137. // D.Push <> (&test, &_MyTest::OnCallback2);
  138. // D.Push (&test, &_MyTest::OnCallback2);
  139. #if (_MSC_VER > 1800)
  140. template <typename T>
  141. inline void Push (T * inst, void (T::*mfn) (const void *, BASE_TA *))
  142. {
  143. typename C11ClassDelegateHandler <T, BASE_TA>::template tMemFunToVoid <T> un (mfn);
  144. if (IsExist (un.pFunc, inst)) return;
  145. C11ClassDelegateHandler <T, BASE_TA> h (un.pFunc, inst, mfn);
  146. ForcePush (std::move (h));
  147. }
  148. #endif
  149. #if 1
  150. // 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  151. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  152. template <typename T, typename TA>
  153. inline void Push (T * inst, void (T::*mfn) (const void *, TA *))
  154. {
  155. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  156. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  157. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  158. typedef void (T::*tRightMemFunc) (const void *, DelegateArgs *);
  159. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  160. typename C11ClassDelegateHandler <T, BASE_TA>::template tMemFunToVoid <T> un (tfn);
  161. if (IsExist (un.pFunc, inst)) return;
  162. C11ClassDelegateHandler <T, BASE_TA> h (un.pFunc, inst, tfn);
  163. ForcePush (std::move (h));
  164. }
  165. #endif
  166. #if (_MSC_VER <= 1800)
  167. template <typename T, typename TB, typename TA>
  168. inline void Push (T * inst, void (TB::*mfn) (const void *, TA *))
  169. {
  170. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  171. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  172. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  173. static_assert (std::is_base_of <TB, T>::value, "T must be derived from TB");
  174. typedef void (T::*tRightMemFunc) (const void *, DelegateArgs *);
  175. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  176. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (tfn);
  177. if (IsExist (un.pFunc, inst)) return;
  178. C11ClassDelegateHandler <T, BASE_TA> h (un.pFunc, inst, tfn);
  179. ForcePush (std::move (h));
  180. }
  181. #endif
  182. public:
  183. template <typename T>
  184. inline void Pop (const T & handler)
  185. {
  186. DoPop (& handler);
  187. }
  188. inline void Pop (void (*fn) (const void *, DelegateArgs *))
  189. {
  190. DoPop (fn);
  191. }
  192. /// 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
  193. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  194. template <typename TA>
  195. inline void Pop (void (*fn) (const void *, TA *))
  196. {
  197. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  198. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  199. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  200. DoPop (fn);
  201. }
  202. /// 模板参数可以自动推导, 因此以下 3 种写法都可以
  203. // D.Pop <_MyTest> (&test, &_MyTest::OnCallback2);
  204. // D.Pop <> (&test, &_MyTest::OnCallback2);
  205. // D.Pop (&test, &_MyTest::OnCallback2);
  206. //#if 0
  207. #if (_MSC_VER > 1800)
  208. template <typename T>
  209. inline void Pop (T * inst, void (T::*mfn) (const void *, DelegateArgs *))
  210. {
  211. typename C11ClassDelegateHandler <T, BASE_TA>::template tMemFunToVoid <T> un (mfn);
  212. DoPop (un.pFunc, inst);
  213. }
  214. #endif
  215. /// 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  216. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  217. #if 1
  218. template <typename T, typename TA>
  219. inline void Pop (T * inst, void (T::*mfn) (const void *, TA *))
  220. {
  221. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  222. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  223. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  224. typedef void (T::*tRightMemFunc) (const void *, DelegateArgs *);
  225. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  226. typename C11ClassDelegateHandler <T, BASE_TA>::template tMemFunToVoid <T> un (tfn);
  227. DoPop (un.pFunc, inst);
  228. }
  229. #endif
  230. #if (_MSC_VER <= 1800)
  231. // 避免如下警告
  232. // OnMouseWheelNormal .Pop (this, &BlowUpImageView::OnEventMouseWheel);
  233. template <typename T, typename TB, typename TA>
  234. inline void Pop (T * inst, void (TB::*mfn) (const void *, TA *))
  235. {
  236. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  237. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  238. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  239. static_assert (std::is_base_of <TB, T>::value, "T must be derived from TB");
  240. typedef void (T::*tRightMemFunc) (const void *, DelegateArgs *);
  241. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  242. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (tfn);
  243. DoPop (un.pFunc, inst);
  244. }
  245. #endif
  246. public:
  247. inline void operator () (const void * sender, DelegateArgs * arg)
  248. {
  249. Invoke (sender, arg);
  250. }
  251. inline virtual void Invoke (const void * sender, DelegateArgs * arg)
  252. {
  253. #if 1
  254. auto temp = m_handlers.clone ();
  255. for (auto & h : temp)
  256. h (sender, arg);
  257. #else
  258. for (auto & h : m_handlers)
  259. h (sender, arg);
  260. #endif
  261. }
  262. protected:
  263. inline virtual void ForcePush (C11EventHandler && handler)
  264. {
  265. m_handlers.push (std::move (handler));
  266. }
  267. protected:
  268. // 按 Key 来删除
  269. inline virtual bool DoPop (const C11EventHandler::tKey key)
  270. {
  271. return m_handlers.erase_if ([& key] (const auto & Item)
  272. {
  273. return Item == key;
  274. });
  275. }
  276. // 按 Key + 类实例指针 来删除
  277. inline virtual bool DoPop (const C11EventHandler::tKey key, const void * pThis)
  278. {
  279. return m_handlers.erase_if ([& key, pThis] (const auto & Item)
  280. {
  281. return Item.IsEqual (key, pThis);
  282. });
  283. }
  284. };