123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- // C11DelegateHandler.hpp
- #pragma once
- #include <functional>
- 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 <typename TA>
- class C11DelegateHandler;
- template <typename TA>
- void OnDelegateInvokeFailed (const C11DelegateHandler <TA> * h);
- //-----------------------------------------------------------------------------
- // C11DelegateHandler
- //-----------------------------------------------------------------------------
- template <typename TA>
- class C11DelegateHandler
- {
- public:
- typedef const void * tKey;
- typedef std::function <void (const void * sender, TA * arg)> 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 <typename T, typename TA>
- class C11ClassDelegateHandler : public C11DelegateHandler <TA>
- {
- typedef C11DelegateHandler <TA> 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 <typename T>
- union tMemFunToVoid
- {
- typedef void (T::*tMemFunc) (const void *, TA *);
- void * pFunc;
- tMemFunc pMemFunc;
- inline tMemFunToVoid (tMemFunc fun)
- {
- pMemFunc = fun;
- }
- };
- };
- template <typename TA>
- inline void OnDelegateInvokeFailed (const C11DelegateHandler <TA> * h)
- {
- try
- {
- // _fatal ("Invoke Delegate %d failed", h);
- }
- catch (...)
- {
- try
- {
- // _fatal ("Invoke Delegate %d failed", h);
- }
- catch (...)
- {
- }
- }
- }
|