16,548
社区成员




class Delegate \
{ \
public: \
Delegate() { }; \
~Delegate() \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
delete (*i); \
m_pFuncs.clear(); \
} \
/* Template function for removing member function callbacks */ \
/* Calls all the callbacks in the callback list */ \
void Invoke parmdecl \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
(*i)->Invoke parmcall; \
} \
/* Calls all the callbacks in the callback list */ \
void operator ()parmdecl { Invoke parmcall; }////////就是这里的Invoke \
\
private: \
/* List of callback functions */ \
std::vector<Functor*> m_pFuncs; \
/* typedef'd iterator */ \
typedef std::vector<Functor*>::iterator vit; \
}; \
}
#define DECLARE_DELEGATE(name, parmdecl, parmcall) \
namespace name##Delegate \
{ \
class Delegate; /* Forward declaration */ \
/* A function object base class for this parameter list */ \
class Functor \
{ \
public: \
virtual void Invoke parmdecl = 0; \
virtual bool isMethod() = 0; \
virtual bool operator ==(Functor *pf) = 0; \
}; \
\
/* Template class derived from Functor for \
making class-specific function objects */ \
template<class C> \
class TFunctor : public Functor \
{ \
public: \
/* Only constructor - stores the given data */ \
TFunctor(C* pObj, void (C::*pFunc)parmdecl) \
{ \
m_pObj = pObj; \
m_pFunc = pFunc; \
} \
bool isMethod() { return true; } \
bool operator ==(Functor *pf) \
{ \
if(!pf->isMethod()) \
return false; \
TFunctor<C> *pf1 = (TFunctor<C>*)pf; \
if((pf1->m_pObj == m_pObj) && (pf1->m_pFunc == m_pFunc)) \
return true; \
else \
return false; \
} \
\
/* Invokes the function object with the given parameters */ \
void Invoke parmdecl { (m_pObj->*m_pFunc)parmcall; } \
private: \
C *m_pObj; /* Object pointer */ \
void (C::*m_pFunc)parmdecl; /* Method pointer */ \
}; \
\
/* Class for function function objects */ \
class FFunctor : public Functor \
{ \
public: \
/* Only constructor - stores the given data */ \
FFunctor(void (*pFunc)parmdecl) { m_pFunc = pFunc; } \
bool isMethod() { return false; } \
bool operator ==(Functor *pf) \
{ \
if(pf->isMethod()) \
return false; \
FFunctor *pf1 = (FFunctor*)pf; \
if(pf1->m_pFunc == m_pFunc) \
return true; \
else \
return false; \
} \
\
/* Invokes the function object with the given parameters */ \
void Invoke parmdecl { m_pFunc parmcall; } \
private: \
void (*m_pFunc)parmdecl; /* Function pointer */ \
}; \
\
/* Delegate class definition */ \
class Delegate \
{ \
public: \
Delegate() { }; \
~Delegate() \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
delete (*i); \
m_pFuncs.clear(); \
} \
\
/* Template function for adding member function callbacks */ \
template<class C> \
void Add(C *pObj, void (C::*pFunc)parmdecl) \
{ \
m_pFuncs.push_back(new TFunctor<C>(pObj, pFunc)); \
} \
/* Add a non-member (or static member) callback function */ \
void Add(void (*pFunc)parmdecl) \
{ \
m_pFuncs.push_back(new FFunctor(pFunc)); \
} \
/* Template function for removing member function callbacks */ \
template<class C> \
void Remove(C *pObj, void (C::*pFunc)parmdecl) \
{ \
TFunctor<C> f(pObj, pFunc); \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
{ \
if(*(*i) == &f) \
{ \
delete *i; \
m_pFuncs.erase(i); \
break; \
} \
} \
} \
/* Removes a non-member (or static member) callback function */ \
void Remove(void (*pFunc)parmdecl) \
{ \
FFunctor f(pFunc); \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
{ \
if(*(*i) == &f) \
{ \
delete *i; \
m_pFuncs.erase(i); \
break; \
} \
} \
} \
\
/* Addition operators */ \
void operator +=(Functor *pFunc) \
{ \
m_pFuncs.push_back(pFunc); \
} \
void operator +=(void (*pFunc)parmdecl) \
{ \
m_pFuncs.push_back(new FFunctor(pFunc)); \
} \
/* Subtraction operators */ \
void operator -=(Functor *pFunc) \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
{ \
if(*(*i) == pFunc) \
{ \
delete *i; \
m_pFuncs.erase(i); \
break; \
} \
} \
delete pFunc; \
} \
void operator -=(void (*pFunc)parmdecl) \
{ \
Remove(pFunc); \
} \
\
/* Calls all the callbacks in the callback list */ \
void Invoke parmdecl \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
(*i)->Invoke parmcall; \
} \
/* Calls all the callbacks in the callback list */ \
void operator ()parmdecl { Invoke parmcall; } \
\
private: \
/* List of callback functions */ \
std::vector<Functor*> m_pFuncs; \
/* typedef'd iterator */ \
typedef std::vector<Functor*>::iterator vit; \
}; \
} \
\
template<class C> \
name##Delegate::TFunctor<C> *name##Handler(C *pObj, void (C::*pFunc)parmdecl) \
{ \
return new name##Delegate::TFunctor<C>(pObj, pFunc); \
}
void Invoke parmdecl \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
(*i)->Invoke parmcall; \
}
void Invoke parmdecl \
{ \
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++) \
(*i)->Invoke parmcall; \
}
被展开为: void Invoke (int p1, float p2)
{
for(vit i=m_pFuncs.begin(); i!=m_pFuncs.end(); i++)
(*i)->Invoke (p1, p2)
}