架構模式--C++類模板實現事件觸發機制
阿新 • • 發佈:2019-01-27
//帶一個引數的事件觸發器 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; };