网络上介绍在C++编译器上实现“委托”的技术文章最著名的就是Don Clugston撰写的《成员函数指针与高性能的C++委托》一文了,它首先解释了成员函数指针在一些常用的编译器中是怎样实现的,然后展示了在多种C++编译器上实现优化而可靠的“委托”。但它过于深入、牵涉到汇编及各种编译期特性、而且其提交的著名代码FastDelegate只实现了单播委托。故国内众多高手也实现一些了稍微简单易懂的C++委托类,流传最广的估计就是jfwan的《一个C#的delegate在C++中的实现》和cpunion的《实现一个高效C++多分派委托类》了。其中jfwan实现的代码精简短小、接口完整,合理的运用宏定义实现了支持不同参数的模板委托类。而cpunion的实现利用模板特化对引用类型返回值提供了支持,而且支持绑定仿函数和=、+=、-=等操作符。想到自己02年刚入行时,为了实现C++回调通知而焦头烂额的样子。看来有必要在众位高手的基础上重构一个新的C++委托类,以加深对C++的理解。
介绍
网络上介绍在C++编译器上实现“委托”的技术文章最著名的就是Don Clugston撰写的《成员函数指针与高性能的C++委托》一文了,它首先解释了成员函数指针在一些常用的编译器中是怎样实现的,然后展示了在多种C++编译器上实现优化而可靠的“委托”。但它过于深入、牵涉到汇编及各种编译期特性、而且其提交的著名代码FastDelegate只实现了单播委托。故国内众多高手也实现一些了稍微简单易懂的C++委托类,流传最广的估计就是jfwan的《一个C#的delegate在C++中的实现》和cpunion的《实现一个高效C++多分派委托类》了。其中jfwan实现的代码精简短小、接口完整,合理的运用宏定义实现了支持不同参数的模板委托类。而cpunion的实现利用模板特化对引用类型返回值提供了支持,而且支持绑定仿函数和=、+=、-=等操作符。想到自己02年刚入行时,为了实现C++回调通知而焦头烂额的样子。看来有必要在众位高手的基础上重构一个新的C++委托类,以加深对C++的理解。
框架
其实与其说是重构,不过说是将jfwan和cpunion两者代码进行合并,各取其优点。总的框架也采用jfwan的宏定义方式。但jfwan的架构是基于Composite模式,将对象组合成树形结构以表示“部分-整体”的层次结构,使得对单个ICallback对象和delegate组合对象的使用具有一致性。见下图:
而新的实现采用聚合方式组织回调类,并且抛弃了用原始指针保存回调链的做法,采用STL的list容器保存回调链,使得
添加、删除委托函数的代码大大精简。并优化了在多播回调时对返回值的处理。架构见下图:
特定参数的实现举例

Code
#define TEMPLATE_LIST_1 ,typename T0
#define TEMPLATE_LIST TEMPLATE_LIST_1
#define TYPE_LIST_1 T0
#define TYPE_LIST TYPE_LIST_1
#define TYPE_PARAM_LIST_1 T0 t0
#define TYPE_PARAM_LIST TYPE_PARAM_LIST_1
#define PARAM_LIST_1 t0
#define PARAM_LIST PARAM_LIST_1
struct SingleThreadTag {};
struct MultiThreadTag {};
template <typename LockType> class ThreadingModel;
template <typename, typename LockType = void> class Delegate;
template <typename> class InvokeBase;
template <typename> class FunctionInvoke;
template <typename, typename> class MemberInvoke;
template <typename, typename> class ConstMemberInvoke;
/**************************************
ThreadingModel
并发策略类
**************************************/
template <typename LockType>
class ThreadingModel
{
public:
typedef MultiThreadTag ThreadTag;
friend class Lock;
class Lock
{
public:
explicit Lock(const ThreadingModel* host)
: m_host(host)
{
m_host->m_mutex.lock();
}
~Lock()
{
m_host->m_mutex.unlock();
}
private:
Lock(const Lock&);
void operator=(const Lock&);
private:
const ThreadingModel* m_host;
};
ThreadingModel() {}
private:
ThreadingModel(const ThreadingModel&);
ThreadingModel& operator=(const ThreadingModel&);
private:
mutable LockType m_mutex;
};
/**************************************
ThreadingModel
并发策略类特化,用于单线程环境
**************************************/
template <>
class ThreadingModel<void>
{
public:
typedef SingleThreadTag ThreadTag;
struct Lock
{
explicit Lock(const ThreadingModel*) {}
};
ThreadingModel() {}
private:
ThreadingModel(const ThreadingModel&);
ThreadingModel& operator=(const ThreadingModel&);
};
/**************************************
InvokeBase
回调基类
**************************************/
template <typename ReturnType TEMPLATE_LIST>
class InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SelfType;
public:
virtual ~InvokeBase() = 0 {};
virtual ReturnType invoke(TYPE_LIST) const = 0;
virtual bool equals(const SelfType* pSelfType) const = 0;
virtual SelfType* clone() const = 0;
};
/**************************************
FunctionInvoke
普通函数(包括静态成员函数)回调类
**************************************/
template <typename ReturnType TEMPLATE_LIST>
class FunctionInvoke<ReturnType(TYPE_LIST)>
: public InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SuperType;
typedef FunctionInvoke<ReturnType(TYPE_LIST)> SelfType;
typedef ReturnType (*FuncPtr)(TYPE_LIST);
public:
explicit FunctionInvoke(FuncPtr ptr)
: m_funcPtr(ptr)
{}
FunctionInvoke(const SelfType& rhs)
: InvokeBase<ReturnType(TYPE_LIST)>(rhs)
, m_funcPtr(rhs.m_funcPtr)
{}
ReturnType invoke(TYPE_PARAM_LIST) const
{
return (*m_funcPtr)(PARAM_LIST);
}
bool equals(const SuperType* pSuperType) const
{
const SelfType* pRhs = dynamic_cast<const SelfType*>(pSuperType);
if (pRhs == 0)
return false;
if (m_funcPtr != pRhs->m_funcPtr)
return false;
return true;
}
SelfType* clone() const
{
return new SelfType(*this);
}
private:
FuncPtr m_funcPtr;
};
/**************************************
MemberInvoke
非静态成员函数回调类
**************************************/
template <typename ObjectType, typename ReturnType TEMPLATE_LIST>
class MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>
: public InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SuperType;
typedef MemberInvoke<ObjectType, ReturnType(TYPE_LIST)> SelfType;
typedef ReturnType (ObjectType::*FuncPtr)(TYPE_LIST);
public:
explicit MemberInvoke(const ObjectType* pObj, FuncPtr ptr)
: m_pObject(pObj)
, m_funcPtr(ptr)
{}
MemberInvoke(const SelfType& rhs)
: InvokeBase<ReturnType(TYPE_LIST)>(rhs)
, m_pObject(rhs.m_pObject)
, m_funcPtr(rhs.m_funcPtr)
{}
ReturnType invoke(TYPE_PARAM_LIST) const
{
return ((ObjectType*)m_pObject->*m_funcPtr)(PARAM_LIST);
}
bool equals(const SuperType* pSuperType) const
{
const SelfType* pRhs = dynamic_cast<const SelfType*>(pSuperType);
if (pRhs == 0)
return false;
if (m_funcPtr != pRhs->m_funcPtr
|| m_pObject != pRhs->m_pObject)
return false;
return true;
}
SelfType* clone() const
{
return new SelfType(*this);
}
private:
const ObjectType* m_pObject;
FuncPtr m_funcPtr;
};
/**************************************
ConstMemberInvoke
非静态常成员函数回调类
**************************************/
template <typename ObjectType, typename ReturnType TEMPLATE_LIST>
class ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)>
: public InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SuperType;
typedef ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)> SelfType;
typedef ReturnType (ObjectType::*ConstFuncPtr)(TYPE_LIST) const;
public:
explicit ConstMemberInvoke(const ObjectType* pObj, ConstFuncPtr ptr)
: m_pObject(pObj)
, m_constFuncPtr(ptr)
{}
ConstMemberInvoke(const SelfType& rhs)
: InvokeBase<ReturnType(TYPE_LIST)>(rhs)
, m_pObject(rhs.m_pObject)
, m_constFuncPtr(rhs.m_constFuncPtr)
{}
ReturnType invoke(TYPE_PARAM_LIST) const
{
return (m_pObject->*m_constFuncPtr)(PARAM_LIST);
}
bool equals(const SuperType* pSuperType) const
{
const SelfType* pRhs = dynamic_cast<const SelfType*>(pSuperType);
if (pRhs == 0)
return false;
if (m_constFuncPtr != pRhs->m_constFuncPtr
|| m_pObject != pRhs->m_pObject)
return false;
return true;
}
SelfType* clone() const
{
return new SelfType(*this);
}
private:
const ObjectType* m_pObject;
ConstFuncPtr m_constFuncPtr;
};
/**************************************
Delegate
委托类
**************************************/
template <typename ReturnType TEMPLATE_LIST, typename LockType>
class Delegate<ReturnType(TYPE_LIST), LockType>
: public ThreadingModel<LockType>
{
typedef Delegate<ReturnType(TYPE_LIST), LockType> SelfType;
typedef ThreadingModel<LockType> ThreadingModelType;
typedef InvokeBase<ReturnType(TYPE_LIST)> InvokeType;
public:
Delegate() {}
Delegate(const SelfType& rhs)
{
this->add(rhs);
}
// 普通函数
explicit Delegate(ReturnType (*ptr)(TYPE_LIST))
{
this->add(ptr);
}
// 成员函数
template <typename ObjectType>
explicit Delegate(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
this->add(pObj, ptr);
}
// 常成员函数指针
template <typename ObjectType>
explicit Delegate(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
this->add(pObj, ptr);
}
// 回调类
explicit Delegate(InvokeType* pInvoke)
{
this->add(pInvoke);
}
// 仿函数
template <typename ObjectType>
explicit Delegate(const ObjectType* pFunctor)
{
this->add(pFunctor, &ObjectType::operator());
}
~Delegate()
{
this->clear();
}
SelfType& operator=(const SelfType& rhs)
{
this->clear();
this->add(rhs);
return *this;
}
SelfType& operator=(ReturnType (*ptr)(TYPE_LIST))
{
this->clear();
this->add(ptr);
return *this;
}
SelfType& operator=(InvokeType* pInvoke)
{
this->clear();
this->add(pInvoke);
return *this;
}
template <typename ObjectType>
SelfType& operator=(const ObjectType* pFunctor)
{
this->clear();
this->add(pFunctor, &ObjectType::operator());
return *this;
}
SelfType& operator+=(const SelfType& rhs)
{
this->add(rhs);
return *this;
}
SelfType& operator+=(ReturnType (*ptr)(TYPE_LIST))
{
this->add(ptr);
return *this;
}
SelfType& operator+=(InvokeType* pInvoke)
{
this->add(pInvoke);
return *this;
}
template <typename ObjectType>
SelfType& operator+=(const ObjectType* pFunctor)
{
this->add(pFunctor, &ObjectType::operator());
return *this;
}
SelfType& operator-=(const SelfType& rhs)
{
this->remove(rhs);
return *this;
}
SelfType& operator-=(ReturnType (*ptr)(TYPE_LIST))
{
this->remove(ptr);
return *this;
}
SelfType& operator-=(InvokeType* pInvoke)
{
this->remove(pInvoke);
return *this;
}
template <typename ObjectType>
SelfType& operator-=(const ObjectType* pFunctor)
{
this->remove(pFunctor, &ObjectType::operator());
return *this;
}
// 遍历回调类列表,调用委托的函数
ReturnType operator()(TYPE_PARAM_LIST)
{
ThreadingModelType::Lock guard(this);
if (m_listInvoke.empty())
return ReturnType();
list<InvokeType*>::iterator iteEnd = m_listInvoke.end();
iteEnd--;
for (list<InvokeType*>::iterator ite = m_listInvoke.begin(); ite != iteEnd; ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
pInvoke->invoke(PARAM_LIST);
}
return ((InvokeType*)(*iteEnd))->invoke(PARAM_LIST);
}
bool equals(const SelfType* pSelfType) const
{
if (pSelfType == 0)
return false;
if (pSelfType == this)
return true;
ThreadingModelType::Lock guard(this);
if (m_listInvoke.size() != pSelfType->m_listInvoke.size())
return false;
for (list<InvokeType*>::const_iterator ite1 = m_listInvoke.begin(), ite2 = pSelfType->m_listInvoke.begin();
ite1 != m_listInvoke.end(); ite1++, ite2++)
{
InvokeType* pInvoke1 = (InvokeType*)*ite1;
InvokeType* pInvoke2 = (InvokeType*)*ite2;
if (!pInvoke1->equals(pInvoke2))
return false;
}
return true;
}
SelfType* clone() const
{
SelfType* pClone = new SelfType();
ThreadingModelType::Lock guard(this);
for (list<InvokeType*>::const_iterator ite = m_listInvoke.begin();
ite != m_listInvoke.end(); ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
pClone->m_listInvoke.push_back(pInvoke->clone());
}
return pClone;
}
void clear()
{
ThreadingModelType::Lock guard(this);
InvokeType* pInvoke = 0;
while (!m_listInvoke.empty())
{
pInvoke = m_listInvoke.front();
m_listInvoke.pop_front();
delete pInvoke;
}
}
private:
void add(const SelfType& rhs)
{
if (rhs.m_listInvoke.empty())
return;
for (list<InvokeType*>::const_iterator ite = rhs.m_listInvoke.begin();
ite != rhs.m_listInvoke.end(); ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
this->add(pInvoke->clone());
}
}
void add(ReturnType (*ptr)(TYPE_LIST))
{
this->add(new FunctionInvoke<ReturnType(TYPE_LIST)>(ptr));
}
template <typename ObjectType>
void add(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
this->add(new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
template <typename ObjectType>
void add(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
this->add(new ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
void add(InvokeType* pInvoke)
{
ThreadingModelType::Lock guard(this);
m_listInvoke.push_back(pInvoke);
}
void remove(const SelfType& rhs)
{
if (rhs.m_listInvoke.empty())
return;
for (list<InvokeType*>::const_iterator ite = rhs.m_listInvoke.begin();
ite != rhs.m_listInvoke.end(); ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
this->remove(pInvoke);
}
}
void remove(ReturnType (*ptr)(TYPE_LIST))
{
this->remove(new FunctionInvoke<ReturnType(TYPE_LIST)>(ptr));
}
template <typename ObjectType>
void remove(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
this->remove(new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
template <typename ObjectType>
void remove(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
this->remove(new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
void remove(InvokeType* pInvoke)
{
ThreadingModelType::Lock guard(this);
for (list<InvokeType*>::iterator ite = m_listInvoke.begin();
ite != m_listInvoke.end(); ite++)
{
InvokeType* pInvoke2 = (InvokeType*)*ite;
if (pInvoke2->equals(pInvoke))
{
m_listInvoke.erase(ite);
delete pInvoke2;
return;
}
}
}
private:
list<InvokeType*> m_listInvoke;
};
// 将成员函数构建为回调类,用于Delegate::operator =()和Delegate::operator +=()
template<typename ObjectType, typename ReturnType TEMPLATE_LIST>
inline InvokeBase<ReturnType(TYPE_LIST)>* makeInvoke(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
return new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr);
}
// 将常成员函数构建为回调类,用于Delegate::operator =()和Delegate::operator +=()
template<typename ObjectType, typename ReturnType TEMPLATE_LIST>
inline InvokeBase<ReturnType(TYPE_LIST)>* makeInvoke(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
return new ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr);
完整的实现下载
#define TEMPLATE_LIST_1 ,typename T0
#define TEMPLATE_LIST TEMPLATE_LIST_1
#define TYPE_LIST_1 T0
#define TYPE_LIST TYPE_LIST_1
#define TYPE_PARAM_LIST_1 T0 t0
#define TYPE_PARAM_LIST TYPE_PARAM_LIST_1
#define PARAM_LIST_1 t0
#define PARAM_LIST PARAM_LIST_1
struct SingleThreadTag {};
struct MultiThreadTag {};
template <typename LockType> class ThreadingModel;
template <typename, typename LockType = void> class Delegate;
template <typename> class InvokeBase;
template <typename> class FunctionInvoke;
template <typename, typename> class MemberInvoke;
template <typename, typename> class ConstMemberInvoke;
/**************************************
ThreadingModel
并发策略类
**************************************/
template <typename LockType>
class ThreadingModel
{
public:
typedef MultiThreadTag ThreadTag;
friend class Lock;
class Lock
{
public:
explicit Lock(const ThreadingModel* host)
: m_host(host)
{
m_host->m_mutex.lock();
}
~Lock()
{
m_host->m_mutex.unlock();
}
private:
Lock(const Lock&);
void operator=(const Lock&);
private:
const ThreadingModel* m_host;
};
ThreadingModel() {}
private:
ThreadingModel(const ThreadingModel&);
ThreadingModel& operator=(const ThreadingModel&);
private:
mutable LockType m_mutex;
};
/**************************************
ThreadingModel
并发策略类特化,用于单线程环境
**************************************/
template <>
class ThreadingModel<void>
{
public:
typedef SingleThreadTag ThreadTag;
struct Lock
{
explicit Lock(const ThreadingModel*) {}
};
ThreadingModel() {}
private:
ThreadingModel(const ThreadingModel&);
ThreadingModel& operator=(const ThreadingModel&);
};
/**************************************
InvokeBase
回调基类
**************************************/
template <typename ReturnType TEMPLATE_LIST>
class InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SelfType;
public:
virtual ~InvokeBase() = 0 {};
virtual ReturnType invoke(TYPE_LIST) const = 0;
virtual bool equals(const SelfType* pSelfType) const = 0;
virtual SelfType* clone() const = 0;
};
/**************************************
FunctionInvoke
普通函数(包括静态成员函数)回调类
**************************************/
template <typename ReturnType TEMPLATE_LIST>
class FunctionInvoke<ReturnType(TYPE_LIST)>
: public InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SuperType;
typedef FunctionInvoke<ReturnType(TYPE_LIST)> SelfType;
typedef ReturnType (*FuncPtr)(TYPE_LIST);
public:
explicit FunctionInvoke(FuncPtr ptr)
: m_funcPtr(ptr)
{}
FunctionInvoke(const SelfType& rhs)
: InvokeBase<ReturnType(TYPE_LIST)>(rhs)
, m_funcPtr(rhs.m_funcPtr)
{}
ReturnType invoke(TYPE_PARAM_LIST) const
{
return (*m_funcPtr)(PARAM_LIST);
}
bool equals(const SuperType* pSuperType) const
{
const SelfType* pRhs = dynamic_cast<const SelfType*>(pSuperType);
if (pRhs == 0)
return false;
if (m_funcPtr != pRhs->m_funcPtr)
return false;
return true;
}
SelfType* clone() const
{
return new SelfType(*this);
}
private:
FuncPtr m_funcPtr;
};
/**************************************
MemberInvoke
非静态成员函数回调类
**************************************/
template <typename ObjectType, typename ReturnType TEMPLATE_LIST>
class MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>
: public InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SuperType;
typedef MemberInvoke<ObjectType, ReturnType(TYPE_LIST)> SelfType;
typedef ReturnType (ObjectType::*FuncPtr)(TYPE_LIST);
public:
explicit MemberInvoke(const ObjectType* pObj, FuncPtr ptr)
: m_pObject(pObj)
, m_funcPtr(ptr)
{}
MemberInvoke(const SelfType& rhs)
: InvokeBase<ReturnType(TYPE_LIST)>(rhs)
, m_pObject(rhs.m_pObject)
, m_funcPtr(rhs.m_funcPtr)
{}
ReturnType invoke(TYPE_PARAM_LIST) const
{
return ((ObjectType*)m_pObject->*m_funcPtr)(PARAM_LIST);
}
bool equals(const SuperType* pSuperType) const
{
const SelfType* pRhs = dynamic_cast<const SelfType*>(pSuperType);
if (pRhs == 0)
return false;
if (m_funcPtr != pRhs->m_funcPtr
|| m_pObject != pRhs->m_pObject)
return false;
return true;
}
SelfType* clone() const
{
return new SelfType(*this);
}
private:
const ObjectType* m_pObject;
FuncPtr m_funcPtr;
};
/**************************************
ConstMemberInvoke
非静态常成员函数回调类
**************************************/
template <typename ObjectType, typename ReturnType TEMPLATE_LIST>
class ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)>
: public InvokeBase<ReturnType(TYPE_LIST)>
{
typedef InvokeBase<ReturnType(TYPE_LIST)> SuperType;
typedef ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)> SelfType;
typedef ReturnType (ObjectType::*ConstFuncPtr)(TYPE_LIST) const;
public:
explicit ConstMemberInvoke(const ObjectType* pObj, ConstFuncPtr ptr)
: m_pObject(pObj)
, m_constFuncPtr(ptr)
{}
ConstMemberInvoke(const SelfType& rhs)
: InvokeBase<ReturnType(TYPE_LIST)>(rhs)
, m_pObject(rhs.m_pObject)
, m_constFuncPtr(rhs.m_constFuncPtr)
{}
ReturnType invoke(TYPE_PARAM_LIST) const
{
return (m_pObject->*m_constFuncPtr)(PARAM_LIST);
}
bool equals(const SuperType* pSuperType) const
{
const SelfType* pRhs = dynamic_cast<const SelfType*>(pSuperType);
if (pRhs == 0)
return false;
if (m_constFuncPtr != pRhs->m_constFuncPtr
|| m_pObject != pRhs->m_pObject)
return false;
return true;
}
SelfType* clone() const
{
return new SelfType(*this);
}
private:
const ObjectType* m_pObject;
ConstFuncPtr m_constFuncPtr;
};
/**************************************
Delegate
委托类
**************************************/
template <typename ReturnType TEMPLATE_LIST, typename LockType>
class Delegate<ReturnType(TYPE_LIST), LockType>
: public ThreadingModel<LockType>
{
typedef Delegate<ReturnType(TYPE_LIST), LockType> SelfType;
typedef ThreadingModel<LockType> ThreadingModelType;
typedef InvokeBase<ReturnType(TYPE_LIST)> InvokeType;
public:
Delegate() {}
Delegate(const SelfType& rhs)
{
this->add(rhs);
}
// 普通函数
explicit Delegate(ReturnType (*ptr)(TYPE_LIST))
{
this->add(ptr);
}
// 成员函数
template <typename ObjectType>
explicit Delegate(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
this->add(pObj, ptr);
}
// 常成员函数指针
template <typename ObjectType>
explicit Delegate(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
this->add(pObj, ptr);
}
// 回调类
explicit Delegate(InvokeType* pInvoke)
{
this->add(pInvoke);
}
// 仿函数
template <typename ObjectType>
explicit Delegate(const ObjectType* pFunctor)
{
this->add(pFunctor, &ObjectType::operator());
}
~Delegate()
{
this->clear();
}
SelfType& operator=(const SelfType& rhs)
{
this->clear();
this->add(rhs);
return *this;
}
SelfType& operator=(ReturnType (*ptr)(TYPE_LIST))
{
this->clear();
this->add(ptr);
return *this;
}
SelfType& operator=(InvokeType* pInvoke)
{
this->clear();
this->add(pInvoke);
return *this;
}
template <typename ObjectType>
SelfType& operator=(const ObjectType* pFunctor)
{
this->clear();
this->add(pFunctor, &ObjectType::operator());
return *this;
}
SelfType& operator+=(const SelfType& rhs)
{
this->add(rhs);
return *this;
}
SelfType& operator+=(ReturnType (*ptr)(TYPE_LIST))
{
this->add(ptr);
return *this;
}
SelfType& operator+=(InvokeType* pInvoke)
{
this->add(pInvoke);
return *this;
}
template <typename ObjectType>
SelfType& operator+=(const ObjectType* pFunctor)
{
this->add(pFunctor, &ObjectType::operator());
return *this;
}
SelfType& operator-=(const SelfType& rhs)
{
this->remove(rhs);
return *this;
}
SelfType& operator-=(ReturnType (*ptr)(TYPE_LIST))
{
this->remove(ptr);
return *this;
}
SelfType& operator-=(InvokeType* pInvoke)
{
this->remove(pInvoke);
return *this;
}
template <typename ObjectType>
SelfType& operator-=(const ObjectType* pFunctor)
{
this->remove(pFunctor, &ObjectType::operator());
return *this;
}
// 遍历回调类列表,调用委托的函数
ReturnType operator()(TYPE_PARAM_LIST)
{
ThreadingModelType::Lock guard(this);
if (m_listInvoke.empty())
return ReturnType();
list<InvokeType*>::iterator iteEnd = m_listInvoke.end();
iteEnd--;
for (list<InvokeType*>::iterator ite = m_listInvoke.begin(); ite != iteEnd; ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
pInvoke->invoke(PARAM_LIST);
}
return ((InvokeType*)(*iteEnd))->invoke(PARAM_LIST);
}
bool equals(const SelfType* pSelfType) const
{
if (pSelfType == 0)
return false;
if (pSelfType == this)
return true;
ThreadingModelType::Lock guard(this);
if (m_listInvoke.size() != pSelfType->m_listInvoke.size())
return false;
for (list<InvokeType*>::const_iterator ite1 = m_listInvoke.begin(), ite2 = pSelfType->m_listInvoke.begin();
ite1 != m_listInvoke.end(); ite1++, ite2++)
{
InvokeType* pInvoke1 = (InvokeType*)*ite1;
InvokeType* pInvoke2 = (InvokeType*)*ite2;
if (!pInvoke1->equals(pInvoke2))
return false;
}
return true;
}
SelfType* clone() const
{
SelfType* pClone = new SelfType();
ThreadingModelType::Lock guard(this);
for (list<InvokeType*>::const_iterator ite = m_listInvoke.begin();
ite != m_listInvoke.end(); ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
pClone->m_listInvoke.push_back(pInvoke->clone());
}
return pClone;
}
void clear()
{
ThreadingModelType::Lock guard(this);
InvokeType* pInvoke = 0;
while (!m_listInvoke.empty())
{
pInvoke = m_listInvoke.front();
m_listInvoke.pop_front();
delete pInvoke;
}
}
private:
void add(const SelfType& rhs)
{
if (rhs.m_listInvoke.empty())
return;
for (list<InvokeType*>::const_iterator ite = rhs.m_listInvoke.begin();
ite != rhs.m_listInvoke.end(); ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
this->add(pInvoke->clone());
}
}
void add(ReturnType (*ptr)(TYPE_LIST))
{
this->add(new FunctionInvoke<ReturnType(TYPE_LIST)>(ptr));
}
template <typename ObjectType>
void add(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
this->add(new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
template <typename ObjectType>
void add(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
this->add(new ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
void add(InvokeType* pInvoke)
{
ThreadingModelType::Lock guard(this);
m_listInvoke.push_back(pInvoke);
}
void remove(const SelfType& rhs)
{
if (rhs.m_listInvoke.empty())
return;
for (list<InvokeType*>::const_iterator ite = rhs.m_listInvoke.begin();
ite != rhs.m_listInvoke.end(); ite++)
{
InvokeType* pInvoke = (InvokeType*)*ite;
this->remove(pInvoke);
}
}
void remove(ReturnType (*ptr)(TYPE_LIST))
{
this->remove(new FunctionInvoke<ReturnType(TYPE_LIST)>(ptr));
}
template <typename ObjectType>
void remove(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
this->remove(new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
template <typename ObjectType>
void remove(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
this->remove(new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr));
}
void remove(InvokeType* pInvoke)
{
ThreadingModelType::Lock guard(this);
for (list<InvokeType*>::iterator ite = m_listInvoke.begin();
ite != m_listInvoke.end(); ite++)
{
InvokeType* pInvoke2 = (InvokeType*)*ite;
if (pInvoke2->equals(pInvoke))
{
m_listInvoke.erase(ite);
delete pInvoke2;
return;
}
}
}
private:
list<InvokeType*> m_listInvoke;
};
// 将成员函数构建为回调类,用于Delegate::operator =()和Delegate::operator +=()
template<typename ObjectType, typename ReturnType TEMPLATE_LIST>
inline InvokeBase<ReturnType(TYPE_LIST)>* makeInvoke(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST))
{
return new MemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr);
}
// 将常成员函数构建为回调类,用于Delegate::operator =()和Delegate::operator +=()
template<typename ObjectType, typename ReturnType TEMPLATE_LIST>
inline InvokeBase<ReturnType(TYPE_LIST)>* makeInvoke(const ObjectType* pObj, ReturnType (ObjectType::*ptr)(TYPE_LIST) const)
{
return new ConstMemberInvoke<ObjectType, ReturnType(TYPE_LIST)>(pObj, ptr);
浙公网安备 33010602011771号