我是靠谱客的博主 典雅航空,这篇文章主要介绍架构模式--C++类模板实现事件触发机制,现在分享给大家,希望可以做个参考。

//带一个参数的事件触发器
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++类模板实现事件触发机制的全部内容,更多相关架构模式--C++类模板实现事件触发机制内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(137)

评论列表共有 0 条评论

立即
投稿
返回
顶部