123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- // Delegate.h
- #pragma once
- //-----------------------------------------------------------------------------
- // 注: 我把 dynamic_cast 改成了 static_cast, 因为 dynamic_cast 需要RTTI
- // 但因为IsEqual是比较成员函数的地址,所以即使用 static_cast 也是正确的.
- //-----------------------------------------------------------------------------
- #define NewDelegateStatic(staticFunc) \
- new StaticDelegateHandler ((StaticDelegateHandler::pStaticFunctor)staticFunc, #staticFunc"")
- #define NewDelegate(theClass, instance, memberFunc) \
- new ClassDelegateHandler <theClass> (instance, (ClassDelegateHandler<theClass>::classMember) (theClass::memberFunc), #theClass"::"#memberFunc)
- #define MAXLEN_DELEGATE_NAME 64
- class DelegateArgs;
- class DelegateHandler;
- void OnDelegateInvokeFailed (const DelegateHandler * h);
- //
- // Arguments of a Delegate
- // You are supposed to derive this class to include
- // more informations regarding the delegate
- class DelegateArgs
- {
- public:
- DelegateArgs () { }
- virtual ~DelegateArgs () { }
- };
- //
- // this the base DelegateHandler which is the static/member function that is called
- //
- class DelegateHandler
- {
- public:
- inline DelegateHandler ()
- {
- m_Name[0] = 0;
- }
- inline DelegateHandler (const char * Name)
- {
- strncpy (m_Name, Name, sizeof(m_Name)-1);
- }
- inline void operator () (const void * sender, DelegateArgs * arg)
- {
- Invoke (sender, arg);
- }
- virtual void Invoke (const void * sender, DelegateArgs * arg) = 0;
- virtual int IsEqual (const DelegateHandler & handler) const = 0;
- virtual bool IsEmpty (void) const = 0;
- virtual DelegateHandler * Clone (void) const = 0;
- public:
- char m_Name [MAXLEN_DELEGATE_NAME];
- };
- //
- // static functions as handlers
- //
- class StaticDelegateHandler : public DelegateHandler
- {
- public:
- typedef void (* pStaticFunctor) (const void *, DelegateArgs *);
- inline StaticDelegateHandler (pStaticFunctor functor)
- {
- m_pStaticFunctor = functor;
- }
- inline StaticDelegateHandler (pStaticFunctor functor, const char * Name)
- : DelegateHandler (Name)
- {
- m_pStaticFunctor = functor;
- }
- inline virtual void Invoke (const void * sender, DelegateArgs * arg)
- {
- #ifndef _DEBUG
- try
- #endif
- {
- m_pStaticFunctor (sender, arg);
- }
- #ifndef _DEBUG
- catch (...)
- {
- Beep (1000, 1000);
- OnDelegateInvokeFailed (this);
- }
- #endif
- }
- inline virtual int IsEqual (const DelegateHandler & handler) const
- {
- // const StaticDelegateHandler * staticHandler = dynamic_cast <const StaticDelegateHandler *> (& handler);
- const StaticDelegateHandler * staticHandler = static_cast <const StaticDelegateHandler *> (& handler);
- if (staticHandler != 0)
- {
- return (staticHandler->m_pStaticFunctor == m_pStaticFunctor);
- }
- return false;
- }
- inline virtual bool IsEmpty (void) const
- {
- return (m_pStaticFunctor == NULL);
- }
- inline virtual DelegateHandler * Clone (void) const
- {
- StaticDelegateHandler * p;
- if (m_Name[0])
- p = new StaticDelegateHandler (m_pStaticFunctor, m_Name);
- else
- p = new StaticDelegateHandler (m_pStaticFunctor);
- return p;
- }
- protected:
- pStaticFunctor m_pStaticFunctor;
- };
- //
- // member function as handlers
- //
- template <typename class T>
- class ClassDelegateHandler : public DelegateHandler
- {
- public:
- typedef void (T::*classMember) (const void *, DelegateArgs *);
- inline ClassDelegateHandler (T * pthis, classMember functor)
- {
- m_pthis = pthis;
- m_classMember = functor;
- }
- inline ClassDelegateHandler (T * pthis, classMember functor, const char * Name)
- : DelegateHandler (Name)
- {
- m_pthis = pthis;
- m_classMember = functor;
- }
- inline virtual void Invoke (const void * sender, DelegateArgs * arg)
- {
- #ifndef _DEBUG
- try
- #endif
- {
- (* m_pthis.*m_classMember) (sender, arg);
- }
- #ifndef _DEBUG
- catch (...)
- {
- Beep (1000, 1000);
- OnDelegateInvokeFailed (this);
- }
- #endif
- }
- inline virtual int IsEqual (const DelegateHandler & handler) const
- {
- // const ClassDelegateHandler * classHandler = dynamic_cast<const ClassDelegateHandler *> ( &handler);
- const ClassDelegateHandler * classHandler = static_cast <const ClassDelegateHandler *> (& handler);
- if (classHandler != 0)
- {
- return (classHandler->m_classMember == m_classMember && m_pthis == classHandler->m_pthis);
- }
- return false;
- }
- inline virtual bool IsEmpty (void) const
- {
- return (m_classMember == NULL);
- }
- inline virtual DelegateHandler * Clone (void) const
- {
- class ClassDelegateHandler <T> * p;
- if (m_Name[0])
- p = new ClassDelegateHandler <T> (m_pthis, m_classMember, m_Name);
- else
- p = new ClassDelegateHandler <T> (m_pthis, m_classMember);
- return p;
- }
- const T * GetClassInstance (void) const
- {
- return m_pthis;
- }
- T * GetClassInstance (void)
- {
- return m_pthis;
- }
- protected:
- classMember m_classMember;
- T * m_pthis;
- };
|