//带一个参数的事件触发器
template<class PTR>
class CEvent
{
private:
typedef void (*Handle)(PTR);
public:
CEvent(Handle handle)
{
_handleVec.push_back(handle);
}
CEvent(){ };
void Reset()
{
_handleVec.clear();
}
CEvent& operator+=(Handle handle)
{
this->_handleVec.push_back(handle);
return *this;
}
CEvent& operator-=(Handle handle)
{
vector<Handle>::iterator it;
for (it=_handleVec.begin(); it!=_handleVec.end(); it++)
{
if ((*(it._Ptr))==handle)
{
this->_handleVec.erase(it);
break;
}
}
return *this;
}
void operator()(PTR pam)
{
Execute(pam);
}
private:
//触发执行事件
void Execute(PTR pam)
{
vector<Handle>::iterator it;
for (it=_handleVec.begin(); it!=_handleVec.end(); it++)
{
(*(it._Ptr))(pam);
}
}
//存储处理程序
vector<Handle> _handleVec;
};
架构模式--C++类模板实现事件触发机制
最新推荐文章于 2024-05-07 10:06:20 发布