C11UnsafeDelegate.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // C11UnsafeDelegate.hpp
  2. #pragma once
  3. #include <vector>
  4. #include "C11DelegateHandler.hpp"
  5. //-----------------------------------------------------------------------------
  6. // _tUnsafeDelegate_Base
  7. //-----------------------------------------------------------------------------
  8. template <typename T>
  9. class _tUnsafeDelegate_Base
  10. {
  11. protected:
  12. typedef T BASE_TA;
  13. typedef C11DelegateHandler <BASE_TA> C11EventHandler;
  14. public:
  15. _tUnsafeDelegate_Base () { }
  16. _tUnsafeDelegate_Base (_tUnsafeDelegate_Base && from)
  17. {
  18. m_handlers.swap (from.m_handlers);
  19. }
  20. _tUnsafeDelegate_Base (const _tUnsafeDelegate_Base &) = delete;
  21. _tUnsafeDelegate_Base & operator = (const _tUnsafeDelegate_Base &) = delete;
  22. _tUnsafeDelegate_Base & operator = (_tUnsafeDelegate_Base && from)
  23. {
  24. m_handlers.swap (from.m_handlers);
  25. return (*this);
  26. }
  27. void swap (_tUnsafeDelegate_Base & from)
  28. {
  29. m_handlers.swap (from.m_handlers);
  30. }
  31. void CopyTo (_tUnsafeDelegate_Base & from)
  32. {
  33. from.m_handlers = m_handlers;
  34. }
  35. void Release ()
  36. {
  37. m_handlers.clear ();
  38. }
  39. virtual ~_tUnsafeDelegate_Base () { }
  40. public:
  41. inline void operator () (const void * sender, T * arg)
  42. {
  43. Invoke (sender, arg);
  44. }
  45. inline virtual void Invoke (const void * sender, T * arg) = 0;
  46. protected:
  47. std::vector <C11EventHandler> m_handlers;
  48. protected:
  49. #if 1
  50. // 按完整的 handler 来查找
  51. inline bool IsExist (const C11EventHandler & handler) const
  52. {
  53. for (auto & h : m_handlers)
  54. {
  55. if (h == handler)
  56. return true;
  57. }
  58. return false;
  59. }
  60. // 按 Key 来查找
  61. inline bool IsExist (const typename C11EventHandler::tKey key) const
  62. {
  63. for (auto & h : m_handlers)
  64. {
  65. if (h == key)
  66. return true;
  67. };
  68. return false;
  69. }
  70. // 按 Key + 类实例指针 来查找
  71. inline bool IsExist (const typename C11EventHandler::tKey key, const void * pThis) const
  72. {
  73. for (auto & h : m_handlers)
  74. {
  75. if (h.IsEqual (key, pThis))
  76. return true;
  77. };
  78. return false;
  79. }
  80. #else
  81. inline bool IsExist (const C11EventHandler & handler) const
  82. {
  83. return false;
  84. }
  85. inline bool IsExist (const C11EventHandler::tKey key) const
  86. {
  87. return false;
  88. }
  89. inline bool IsExist (const C11EventHandler::tKey key, const void * pThis) const
  90. {
  91. return false;
  92. }
  93. #endif
  94. public:
  95. inline virtual void RemoveAll ()
  96. {
  97. Release ();
  98. }
  99. inline bool IsEmpty () const
  100. {
  101. return m_handlers.empty ();
  102. }
  103. inline int GetSize () const
  104. {
  105. return (int) m_handlers.size ();
  106. }
  107. public:
  108. // lambda 表达式会到这里
  109. // Binder (bind 结果) 会到这里
  110. template <typename T>
  111. inline void Push (const T & handler)
  112. {
  113. C11EventHandler h (& handler, handler);
  114. ForcePush (std::move (h));
  115. }
  116. // 静态函数会到这里
  117. inline void Push (void (*fn) (const void *, T *))
  118. {
  119. if (IsExist (fn)) return;
  120. C11EventHandler h (fn, fn);
  121. ForcePush (std::move (h));
  122. }
  123. // 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
  124. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  125. template <typename TA>
  126. inline void Push (void (*fn) (const void *, TA *))
  127. {
  128. if (IsExist (fn)) return;
  129. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  130. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  131. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from BASE_TA");
  132. typedef void (*tRightFunc) (const void *, DelegateArgs *);
  133. tRightFunc tfn = reinterpret_cast <tRightFunc> (fn);
  134. C11EventHandler h (tfn, tfn);
  135. ForcePush (std::move (h));
  136. }
  137. // 类成员函数
  138. // 模板参数可以自动推导, 因此以下 3 种写法都可以
  139. // D.Push <_MyTest> (&test, &_MyTest::OnCallback2);
  140. // D.Push <> (&test, &_MyTest::OnCallback2);
  141. // D.Push (&test, &_MyTest::OnCallback2);
  142. #if (_MSC_VER > 1800)
  143. template <typename T>
  144. inline void Push (T * inst, void (T::*mfn) (const void *, BASE_TA *))
  145. {
  146. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (mfn);
  147. if (IsExist (un.pFunc, inst)) return;
  148. C11ClassDelegateHandler <T, BASE_TA> h (un.pFunc, inst, mfn);
  149. ForcePush (std::move (h));
  150. }
  151. #endif
  152. #if 1
  153. // 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  154. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  155. template <typename TC, typename TA>
  156. inline void Push (TC * inst, void (TC::*mfn) (const void *, TA *))
  157. {
  158. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  159. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  160. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from BASE_TA");
  161. typedef void (TC::*tRightMemFunc) (const void *, T *);
  162. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  163. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  164. if (IsExist (un.pFunc, inst)) return;
  165. C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
  166. ForcePush (std::move (h));
  167. }
  168. #endif
  169. #if (_MSC_VER <= 1800)
  170. template <typename TC, typename TB, typename TA>
  171. inline void Push (TC * inst, void (TB::*mfn) (const void *, TA *))
  172. {
  173. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  174. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  175. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  176. static_assert (std::is_base_of <TB, TC>::value, "TC must be derived from TB");
  177. typedef void (TC::*tRightMemFunc) (const void *, TA *);
  178. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  179. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  180. if (IsExist (un.pFunc, inst)) return;
  181. C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
  182. ForcePush (std::move (h));
  183. }
  184. #endif
  185. // 判重
  186. public:
  187. // lambda 表达式会到这里
  188. // Binder (bind 结果) 会到这里
  189. template <typename T>
  190. inline void PushOnce (const T & handler)
  191. {
  192. if (IsExist (& handler)) return;
  193. C11EventHandler h (& handler, handler);
  194. ForcePush (std::move (h));
  195. }
  196. // 静态函数会到这里
  197. inline void PushOnce (void (*fn) (const void *, T *))
  198. {
  199. if (IsExist (fn)) return;
  200. C11EventHandler h (fn, fn);
  201. ForcePush (std::move (h));
  202. }
  203. // 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
  204. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  205. template <typename TA>
  206. inline void PushOnce (void (*fn) (const void *, TA *))
  207. {
  208. if (IsExist (fn)) return;
  209. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  210. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  211. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  212. typedef void (TC::*tRightMemFunc) (const void *, T *);
  213. tRightFunc tfn = reinterpret_cast <tRightFunc> (fn);
  214. C11EventHandler h (tfn, tfn);
  215. ForcePush (std::move (h));
  216. }
  217. // 类成员函数
  218. // 模板参数可以自动推导, 因此以下 3 种写法都可以
  219. // D.Push <_MyTest> (&test, &_MyTest::OnCallback2);
  220. // D.Push <> (&test, &_MyTest::OnCallback2);
  221. // D.Push (&test, &_MyTest::OnCallback2);
  222. #if (_MSC_VER > 1800)
  223. template <typename TC>
  224. inline void PushOnce (TC * inst, void (TC::*mfn) (const void *, BASE_TA *))
  225. {
  226. C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (mfn);
  227. if (IsExist (un.pFunc, inst)) return;
  228. C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, mfn);
  229. ForcePush (std::move (h));
  230. }
  231. #endif
  232. #if 1
  233. // 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  234. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  235. template <typename TC, typename TA>
  236. inline void PushOnce (TC * inst, void (TC::*mfn) (const void *, TA *))
  237. {
  238. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  239. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  240. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  241. typedef void (TC::*tRightMemFunc) (const void *, T *);
  242. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  243. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  244. if (IsExist (un.pFunc, inst)) return;
  245. C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
  246. ForcePush (std::move (h));
  247. }
  248. #endif
  249. #if (_MSC_VER <= 1800)
  250. template <typename TC, typename TB, typename TA>
  251. inline void PushOnce (TC* inst, void (TB::*mfn) (const void *, TA *))
  252. {
  253. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  254. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  255. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  256. static_assert (std::is_base_of <TB, TC>::value, "TC must be derived from TB");
  257. typedef void (TC::*tRightMemFunc) (const void *, T *);
  258. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  259. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  260. if (IsExist (un.pFunc, inst)) return;
  261. C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
  262. ForcePush (std::move (h));
  263. }
  264. #endif
  265. protected:
  266. inline virtual void ForcePush (C11EventHandler && handler)
  267. {
  268. m_handlers.push_back (handler);
  269. }
  270. };
  271. //-----------------------------------------------------------------------------
  272. // _tUnsafeDelegate, 支持 Pop
  273. //-----------------------------------------------------------------------------
  274. template <typename T>
  275. class _tUnsafeDelegate : public _tUnsafeDelegate_Base <T>
  276. {
  277. public:
  278. // lambda 表达式会到这里
  279. // Binder (bind 结果) 会到这里
  280. template <typename T>
  281. inline void Pop (const T & handler)
  282. {
  283. DoPop (& handler);
  284. }
  285. // 静态函数会到这里
  286. inline void Pop (void (*fn) (const void *, T *))
  287. {
  288. DoPop (fn);
  289. }
  290. /// 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
  291. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  292. template <typename TA>
  293. inline void Pop (void (*fn) (const void *, TA *))
  294. {
  295. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  296. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  297. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  298. DoPop (fn);
  299. }
  300. /// 模板参数可以自动推导, 因此以下 3 种写法都可以
  301. // D.Pop <_MyTest> (&test, &_MyTest::OnCallback2);
  302. // D.Pop <> (&test, &_MyTest::OnCallback2);
  303. // D.Pop (&test, &_MyTest::OnCallback2);
  304. //#if 0
  305. #if (_MSC_VER > 1800)
  306. template <typename TC>
  307. inline void Pop (TC * inst, void (TC::*mfn) (const void *, T *))
  308. {
  309. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (mfn);
  310. DoPop (un.pFunc, inst);
  311. }
  312. #endif
  313. /// 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
  314. // void OnCallback1 (const void * sender, EventArgs_Error * arg)
  315. #if 1
  316. template <typename TC, typename TA>
  317. inline void Pop (TC * inst, void (TC::*mfn) (const void *, TA *))
  318. {
  319. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  320. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  321. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  322. typedef void (TC::*tRightMemFunc) (const void *, T *);
  323. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  324. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  325. DoPop (un.pFunc, inst);
  326. }
  327. #endif
  328. #if (_MSC_VER <= 1800)
  329. // 避免如下警告
  330. // OnMouseWheelNormal .Pop (this, &BlowUpImageView::OnEventMouseWheel);
  331. template <typename TC, typename TB, typename TA>
  332. inline void Pop (TC * inst, void (TB::*mfn) (const void *, TA *))
  333. {
  334. // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
  335. // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
  336. static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
  337. static_assert (std::is_base_of <TB, TC>::value, "T must be derived from TB");
  338. typedef void (TC::*tRightMemFunc) (const void *, T *);
  339. tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
  340. C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
  341. DoPop (un.pFunc, inst);
  342. }
  343. #endif
  344. public:
  345. inline virtual void Invoke (const void * sender, T * arg) override
  346. {
  347. #if 1
  348. std::vector <C11EventHandler> temp = m_handlers;
  349. for (auto & h : temp)
  350. h (sender, arg);
  351. #else
  352. for (auto & h : m_handlers)
  353. h (sender, arg);
  354. #endif
  355. }
  356. protected:
  357. // 按 Key 来删除
  358. inline virtual bool DoPop (const typename C11EventHandler::tKey key)
  359. {
  360. for (auto it = m_handlers.begin (); it != m_handlers.end (); it++)
  361. {
  362. auto & h = (*it);
  363. if (h == key)
  364. {
  365. m_handlers.erase (it);
  366. return true;
  367. }
  368. };
  369. return false;
  370. }
  371. // 按 Key + 类实例指针 来删除
  372. inline virtual bool DoPop (const typename C11EventHandler::tKey key, const void * pThis)
  373. {
  374. for (auto it = m_handlers.begin (); it != m_handlers.end (); it++)
  375. {
  376. auto & h = (*it);
  377. if (h.IsEqual (key, pThis))
  378. {
  379. m_handlers.erase (it);
  380. return true;
  381. }
  382. };
  383. return false;
  384. }
  385. };
  386. using UnsafeDelegate = _tUnsafeDelegate <EventArgs_Null>;
  387. //-----------------------------------------------------------------------------
  388. // VC 2013 不支持以下两种自动推断, 因此这里做了版本判断, 如果是 VC 2013, 就禁止以下两个函数
  389. // inline void Push (T * inst, void (T::*mfn) (const void *, TA *))
  390. // inline void Pop (T * inst, void (T::*mfn) (const void *, TA *))
  391. //
  392. // Diff\DICOMCMoveSCUEx.cpp(349): error C2668: “UnsafeDelegate::Pop”: 对重载函数的调用不明确
  393. // E:\NMay.2017\Common\C11UnsafeDelegate.hpp(257): 可能是“void UnsafeDelegate::Pop<DICOMCMoveSCUEx,DelegateArgs>(T *,void (__cdecl
  394. // E:\NMay.2017\Common\C11UnsafeDelegate.hpp(246): 或 “void UnsafeDelegate::Pop<DICOMCMoveSCUEx>(T *,void (__cdecl DICOMCMoveSCUEx::* )(const
  395. //-----------------------------------------------------------------------------