C11SafeDelegate.hpp 9.3 KB

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