// C11DelegateHandler.hpp #pragma once #include using std::placeholders::_1; using std::placeholders::_2; using std::placeholders::_3; using std::placeholders::_4; using std::placeholders::_5; #include "DelegateArg.hpp" class DelegateArgs; template class C11DelegateHandler; template void OnDelegateInvokeFailed (const C11DelegateHandler * h); //----------------------------------------------------------------------------- // C11DelegateHandler //----------------------------------------------------------------------------- template class C11DelegateHandler { public: typedef const void * tKey; typedef std::function DelegateFunction; public: inline C11DelegateHandler (tKey key, DelegateFunction fun) { m_Key = key; m_Function = fun; m_pThis = NULL; } virtual ~C11DelegateHandler () { } public: C11DelegateHandler (const C11DelegateHandler & h) { m_Key = h.m_Key; m_Function = h.m_Function; m_pThis = h.m_pThis; } C11DelegateHandler (C11DelegateHandler && h) { m_Key = h.m_Key; m_Function.swap (h.m_Function); m_pThis = h.m_pThis; } C11DelegateHandler & operator = (const C11DelegateHandler & h) { m_Key = h.m_Key; m_Function = h.m_Function; m_pThis = h.m_pThis; return (*this); } public: inline void operator () (const void * sender, TA * arg) { Invoke (sender, arg); } virtual void Invoke (const void * sender, TA * arg) { #ifndef _DEBUG try #endif { m_Function (sender, arg); } #ifndef _DEBUG catch (...) { Beep (1000, 1000); OnDelegateInvokeFailed (this); } #endif } virtual bool IsEqual (const C11DelegateHandler & handler) const { return (m_Key == handler.m_Key) && (m_pThis == handler.m_pThis); } virtual bool IsEqual (const tKey key) const { return (m_Key == key); } virtual bool IsEqual (const tKey key, const void * pThis) const { return (m_Key == key) && (m_pThis == pThis); } bool operator == (const C11DelegateHandler & h) const { return this->IsEqual (h); } bool operator == (const tKey key) const { return this->IsEqual (key); } protected: DelegateFunction m_Function; tKey m_Key; const void * m_pThis; }; //----------------------------------------------------------------------------- // C11ClassDelegateHandler //----------------------------------------------------------------------------- // // member function as handlers // template class C11ClassDelegateHandler : public C11DelegateHandler { typedef C11DelegateHandler inherited; public: typedef void (T::*tMemFunc) (const void *, TA *); inline C11ClassDelegateHandler (void * pFun, T * pthis, tMemFunc functor) : inherited (pFun, std::bind (functor, pthis, _1, _2)) { m_pThis = pthis; } public: // 此联合的目的是把指向成员函数的指针转换成 void * // 按照 C++ 语言的规范, 指向成员函数的指针是无法通过任何正常 cast 机制转换成 void * template union tMemFunToVoid { typedef void (T::*tMemFunc) (const void *, TA *); void * pFunc; tMemFunc pMemFunc; inline tMemFunToVoid (tMemFunc fun) { pMemFunc = fun; } }; }; template inline void OnDelegateInvokeFailed (const C11DelegateHandler * h) { try { // _fatal ("Invoke Delegate %d failed", h); } catch (...) { try { // _fatal ("Invoke Delegate %d failed", h); } catch (...) { } } }