架构模式--C++类模板实现事件触发机制

    xiaoxiao2021-03-25  147

    //带一个参数的事件触发器 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; };
    转载请注明原文地址: https://ju.6miu.com/read-8063.html

    最新回复(0)