123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- // C11UnsafeDelegate.hpp
- #pragma once
- #include <vector>
- #include "C11DelegateHandler.hpp"
- //-----------------------------------------------------------------------------
- // _tUnsafeDelegate_Base
- //-----------------------------------------------------------------------------
- template <typename T>
- class _tUnsafeDelegate_Base
- {
- protected:
- typedef T BASE_TA;
- typedef C11DelegateHandler <BASE_TA> C11EventHandler;
- public:
- _tUnsafeDelegate_Base () { }
- _tUnsafeDelegate_Base (_tUnsafeDelegate_Base && from)
- {
- m_handlers.swap (from.m_handlers);
- }
- _tUnsafeDelegate_Base (const _tUnsafeDelegate_Base &) = delete;
- _tUnsafeDelegate_Base & operator = (const _tUnsafeDelegate_Base &) = delete;
- _tUnsafeDelegate_Base & operator = (_tUnsafeDelegate_Base && from)
- {
- m_handlers.swap (from.m_handlers);
- return (*this);
- }
- void swap (_tUnsafeDelegate_Base & from)
- {
- m_handlers.swap (from.m_handlers);
- }
- void CopyTo (_tUnsafeDelegate_Base & from)
- {
- from.m_handlers = m_handlers;
- }
- void Release ()
- {
- m_handlers.clear ();
- }
- virtual ~_tUnsafeDelegate_Base () { }
- public:
- inline void operator () (const void * sender, T * arg)
- {
- Invoke (sender, arg);
- }
- inline virtual void Invoke (const void * sender, T * arg) = 0;
- protected:
- std::vector <C11EventHandler> m_handlers;
- protected:
- #if 1
- // 按完整的 handler 来查找
- inline bool IsExist (const C11EventHandler & handler) const
- {
- for (auto & h : m_handlers)
- {
- if (h == handler)
- return true;
- }
- return false;
- }
- // 按 Key 来查找
- inline bool IsExist (const typename C11EventHandler::tKey key) const
- {
- for (auto & h : m_handlers)
- {
- if (h == key)
- return true;
- };
- return false;
- }
- // 按 Key + 类实例指针 来查找
- inline bool IsExist (const typename C11EventHandler::tKey key, const void * pThis) const
- {
- for (auto & h : m_handlers)
- {
- if (h.IsEqual (key, pThis))
- return true;
- };
- return false;
- }
- #else
- inline bool IsExist (const C11EventHandler & handler) const
- {
- return false;
- }
- inline bool IsExist (const C11EventHandler::tKey key) const
- {
- return false;
- }
- inline bool IsExist (const C11EventHandler::tKey key, const void * pThis) const
- {
- return false;
- }
- #endif
- public:
- inline virtual void RemoveAll ()
- {
- Release ();
- }
- inline bool IsEmpty () const
- {
- return m_handlers.empty ();
- }
- inline int GetSize () const
- {
- return (int) m_handlers.size ();
- }
- public:
- // lambda 表达式会到这里
- // Binder (bind 结果) 会到这里
- template <typename T>
- inline void Push (const T & handler)
- {
- C11EventHandler h (& handler, handler);
- ForcePush (std::move (h));
- }
- // 静态函数会到这里
- inline void Push (void (*fn) (const void *, T *))
- {
- if (IsExist (fn)) return;
- C11EventHandler h (fn, fn);
- ForcePush (std::move (h));
- }
- // 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
- // void OnCallback1 (const void * sender, EventArgs_Error * arg)
- template <typename TA>
- inline void Push (void (*fn) (const void *, TA *))
- {
- if (IsExist (fn)) return;
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from BASE_TA");
- typedef void (*tRightFunc) (const void *, DelegateArgs *);
- tRightFunc tfn = reinterpret_cast <tRightFunc> (fn);
- C11EventHandler h (tfn, tfn);
- ForcePush (std::move (h));
- }
- // 类成员函数
- // 模板参数可以自动推导, 因此以下 3 种写法都可以
- // D.Push <_MyTest> (&test, &_MyTest::OnCallback2);
- // D.Push <> (&test, &_MyTest::OnCallback2);
- // D.Push (&test, &_MyTest::OnCallback2);
- #if (_MSC_VER > 1800)
- template <typename T>
- inline void Push (T * inst, void (T::*mfn) (const void *, BASE_TA *))
- {
- C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (mfn);
-
- if (IsExist (un.pFunc, inst)) return;
- C11ClassDelegateHandler <T, BASE_TA> h (un.pFunc, inst, mfn);
- ForcePush (std::move (h));
- }
- #endif
- #if 1
- // 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
- // void OnCallback1 (const void * sender, EventArgs_Error * arg)
- template <typename TC, typename TA>
- inline void Push (TC * inst, void (TC::*mfn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from BASE_TA");
- typedef void (TC::*tRightMemFunc) (const void *, T *);
- tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
-
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
- if (IsExist (un.pFunc, inst)) return;
- C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
- ForcePush (std::move (h));
- }
- #endif
- #if (_MSC_VER <= 1800)
- template <typename TC, typename TB, typename TA>
- inline void Push (TC * inst, void (TB::*mfn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- static_assert (std::is_base_of <TB, TC>::value, "TC must be derived from TB");
- typedef void (TC::*tRightMemFunc) (const void *, TA *);
- tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
-
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
- if (IsExist (un.pFunc, inst)) return;
- C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
- ForcePush (std::move (h));
- }
- #endif
- // 判重
- public:
- // lambda 表达式会到这里
- // Binder (bind 结果) 会到这里
- template <typename T>
- inline void PushOnce (const T & handler)
- {
- if (IsExist (& handler)) return;
- C11EventHandler h (& handler, handler);
- ForcePush (std::move (h));
- }
- // 静态函数会到这里
- inline void PushOnce (void (*fn) (const void *, T *))
- {
- if (IsExist (fn)) return;
- C11EventHandler h (fn, fn);
- ForcePush (std::move (h));
- }
- // 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
- // void OnCallback1 (const void * sender, EventArgs_Error * arg)
- template <typename TA>
- inline void PushOnce (void (*fn) (const void *, TA *))
- {
- if (IsExist (fn)) return;
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- typedef void (TC::*tRightMemFunc) (const void *, T *);
- tRightFunc tfn = reinterpret_cast <tRightFunc> (fn);
- C11EventHandler h (tfn, tfn);
- ForcePush (std::move (h));
- }
- // 类成员函数
- // 模板参数可以自动推导, 因此以下 3 种写法都可以
- // D.Push <_MyTest> (&test, &_MyTest::OnCallback2);
- // D.Push <> (&test, &_MyTest::OnCallback2);
- // D.Push (&test, &_MyTest::OnCallback2);
- #if (_MSC_VER > 1800)
- template <typename TC>
- inline void PushOnce (TC * inst, void (TC::*mfn) (const void *, BASE_TA *))
- {
- C11ClassDelegateHandler <T, BASE_TA>::tMemFunToVoid <T> un (mfn);
-
- if (IsExist (un.pFunc, inst)) return;
- C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, mfn);
- ForcePush (std::move (h));
- }
- #endif
- #if 1
- // 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
- // void OnCallback1 (const void * sender, EventArgs_Error * arg)
- template <typename TC, typename TA>
- inline void PushOnce (TC * inst, void (TC::*mfn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- typedef void (TC::*tRightMemFunc) (const void *, T *);
- tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
-
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
- if (IsExist (un.pFunc, inst)) return;
- C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
- ForcePush (std::move (h));
- }
- #endif
- #if (_MSC_VER <= 1800)
- template <typename TC, typename TB, typename TA>
- inline void PushOnce (TC* inst, void (TB::*mfn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- static_assert (std::is_base_of <TB, TC>::value, "TC must be derived from TB");
- typedef void (TC::*tRightMemFunc) (const void *, T *);
- tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
-
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
- if (IsExist (un.pFunc, inst)) return;
- C11ClassDelegateHandler <TC, BASE_TA> h (un.pFunc, inst, tfn);
- ForcePush (std::move (h));
- }
- #endif
- protected:
- inline virtual void ForcePush (C11EventHandler && handler)
- {
- m_handlers.push_back (handler);
- }
- };
- //-----------------------------------------------------------------------------
- // _tUnsafeDelegate, 支持 Pop
- //-----------------------------------------------------------------------------
- template <typename T>
- class _tUnsafeDelegate : public _tUnsafeDelegate_Base <T>
- {
- public:
- // lambda 表达式会到这里
- // Binder (bind 结果) 会到这里
- template <typename T>
- inline void Pop (const T & handler)
- {
- DoPop (& handler);
- }
- // 静态函数会到这里
- inline void Pop (void (*fn) (const void *, T *))
- {
- DoPop (fn);
- }
- /// 此函数用于静态函数函数的参数是 DelegateArgs 的继承类的情形. 例如
- // void OnCallback1 (const void * sender, EventArgs_Error * arg)
- template <typename TA>
- inline void Pop (void (*fn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- DoPop (fn);
- }
- /// 模板参数可以自动推导, 因此以下 3 种写法都可以
- // D.Pop <_MyTest> (&test, &_MyTest::OnCallback2);
- // D.Pop <> (&test, &_MyTest::OnCallback2);
- // D.Pop (&test, &_MyTest::OnCallback2);
- //#if 0
- #if (_MSC_VER > 1800)
- template <typename TC>
- inline void Pop (TC * inst, void (TC::*mfn) (const void *, T *))
- {
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (mfn);
- DoPop (un.pFunc, inst);
- }
- #endif
- /// 此函数用于成员函数的参数是 DelegateArgs 的继承类的情形. 例如
- // void OnCallback1 (const void * sender, EventArgs_Error * arg)
- #if 1
- template <typename TC, typename TA>
- inline void Pop (TC * inst, void (TC::*mfn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- typedef void (TC::*tRightMemFunc) (const void *, T *);
- tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
-
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
- DoPop (un.pFunc, inst);
- }
- #endif
- #if (_MSC_VER <= 1800)
- // 避免如下警告
- // OnMouseWheelNormal .Pop (this, &BlowUpImageView::OnEventMouseWheel);
- template <typename TC, typename TB, typename TA>
- inline void Pop (TC * inst, void (TB::*mfn) (const void *, TA *))
- {
- // 静态断言, 如果 TA 不是 DelegateArgs 的继承类, 将断言失败
- // 需要编译选项 ISO C++ 最新草案标准 (/std:c++latest)
- static_assert (std::is_base_of <BASE_TA, TA>::value, "TA must be derived from DelegateArgs");
- static_assert (std::is_base_of <TB, TC>::value, "T must be derived from TB");
- typedef void (TC::*tRightMemFunc) (const void *, T *);
- tRightMemFunc tfn = reinterpret_cast <tRightMemFunc> (mfn);
-
- C11ClassDelegateHandler <TC, BASE_TA>::tMemFunToVoid <TC> un (tfn);
- DoPop (un.pFunc, inst);
- }
- #endif
- public:
- inline virtual void Invoke (const void * sender, T * arg) override
- {
- #if 1
- std::vector <C11EventHandler> temp = m_handlers;
- for (auto & h : temp)
- h (sender, arg);
- #else
- for (auto & h : m_handlers)
- h (sender, arg);
- #endif
- }
- protected:
- // 按 Key 来删除
- inline virtual bool DoPop (const typename C11EventHandler::tKey key)
- {
- for (auto it = m_handlers.begin (); it != m_handlers.end (); it++)
- {
- auto & h = (*it);
- if (h == key)
- {
- m_handlers.erase (it);
- return true;
- }
- };
- return false;
- }
- // 按 Key + 类实例指针 来删除
- inline virtual bool DoPop (const typename C11EventHandler::tKey key, const void * pThis)
- {
- for (auto it = m_handlers.begin (); it != m_handlers.end (); it++)
- {
- auto & h = (*it);
- if (h.IsEqual (key, pThis))
- {
- m_handlers.erase (it);
- return true;
- }
- };
- return false;
- }
- };
- using UnsafeDelegate = _tUnsafeDelegate <EventArgs_Null>;
- //-----------------------------------------------------------------------------
- // VC 2013 不支持以下两种自动推断, 因此这里做了版本判断, 如果是 VC 2013, 就禁止以下两个函数
- // inline void Push (T * inst, void (T::*mfn) (const void *, TA *))
- // inline void Pop (T * inst, void (T::*mfn) (const void *, TA *))
- //
- // Diff\DICOMCMoveSCUEx.cpp(349): error C2668: “UnsafeDelegate::Pop”: 对重载函数的调用不明确
- // E:\NMay.2017\Common\C11UnsafeDelegate.hpp(257): 可能是“void UnsafeDelegate::Pop<DICOMCMoveSCUEx,DelegateArgs>(T *,void (__cdecl
- // E:\NMay.2017\Common\C11UnsafeDelegate.hpp(246): 或 “void UnsafeDelegate::Pop<DICOMCMoveSCUEx>(T *,void (__cdecl DICOMCMoveSCUEx::* )(const
- //-----------------------------------------------------------------------------
|