C11UnsafeDelegate.hpp 14 KB

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