C++仿c#的delegate的实现源码
2016-11-25 11:29
323 查看
最近写了一个把回调函数和回调对象成员函数统一的类,并实现了一个仿c#的delegate,仅供参考,有兴趣改进的请回复并附源码,一起进步,谢谢。
类之间的继承关系见下图:
下面附源码和测试程序
Delegate.hpp
DelegateTest.cpp
类之间的继承关系见下图:
下面附源码和测试程序
Delegate.hpp
/* * Delegate.hpp * * Created on: Aug 31, 2016 * Author: root */ #ifndef DELEGATE_HPP_ #define DELEGATE_HPP_ #include <vector> template <typename... Types> class CallBackBase { public: CallBackBase() {} virtual ~CallBackBase() {}; virtual void operator () (Types... args) = 0; }; template <typename... Types> class CallBackFunction : public CallBackBase<Types...> { public: typedef void (*FPCallBack)(Types... args); CallBackFunction(){} CallBackFunction(FPCallBack pFunc) { if (pFunc != nullptr) { m_fpCallBack = pFunc; } } virtual ~CallBackFunction() { } virtual void operator () (Types... args) { if (m_fpCallBack != nullptr) m_fpCallBack(args...); //throw std::exception("null function address is called"); return ; } private: FPCallBack m_fpCallBack = nullptr; }; template <typename T, typename... Types> class CallBackMethod : public CallBackBase<Types...> { public: typedef void (T::*FPClassCallBack)(Types... args); CallBackMethod(){} CallBackMethod(T *pThis, FPClassCallBack pFunc) { if (pThis != nullptr && pFunc != nullptr) { m_pThis = pThis; m_fpCallBack = pFunc; } } virtual ~CallBackMethod() { } virtual void operator () (Types... args) { if (m_pThis != nullptr && m_fpCallBack != nullptr) { (m_pThis->*m_fpCallBack)(args...); } //throw std::exception("null function address is called"); return ; } private: T *m_pThis = nullptr; FPClassCallBack m_fpCallBack = nullptr; }; template <typename... Types> class Delegate { using CallBackType = CallBackBase<Types...>; public: Delegate(){}; Delegate(CallBackType *callBack) { _vecCallBack.clear(); _vecCallBack.push_back(callBack); } virtual ~Delegate() {}; void operator () (Types... args) { for (auto item : _vecCallBack) { (*item)(args...); } } virtual void operator += (CallBackType *pFunc) { if (pFunc != nullptr) { _vecCallBack.push_back(pFunc); } } virtual void operator -= (CallBackType *pFunc) { if (pFunc != nullptr) { auto it = _vecCallBack.begin(); for (; it != _vecCallBack.end(); ++it) { if (*it == pFunc) { _vecCallBack.erase(it); break; } } //auto it = std::find(_vecCallBack.rbegin(), _vecCallBack.rend(), pFunc); //if (it != _vecCallBack.cend()) // _vecCallBack.erase(it); } } private: std::vector<CallBackType*> _vecCallBack; }; #endif /* DELEGATE_HPP_ */
DelegateTest.cpp
//============================================================================ // Name : DelegateTest.cpp // Author : Zhangjf // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include "Delegate.hpp" using namespace std; void print1(std::string strP); class Test1 { public: void print1(std::string str1) { printf("%s, %d: %s\n", __func__, __LINE__, str1.c_str()); return ; } }; int main() { CallBackFunction<std::string> callBack1(print1); Delegate<std::string> d1(&callBack1); d1("test1"); Test1 t; CallBackMethod<Test1, std::string> callBack2(&t, &Test1::print1); d1 += &callBack2; d1("test2"); d1 -= &callBack1; d1("test3"); return 0; } void print1(std::string strP) { printf("%s, %d, %s\n", __func__, __LINE__, strP.c_str()); return ; }
相关文章推荐
- 用 C++ 实现 C# 中的 委托/事件 (1-delegateEMU.cpp)
- 在C++中实现C#的delegate机制
- 【C++】C++简易实现C#的delegate
- 用 C++ 实现 C# 中的 委托/事件 (2-delegate event functor)
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- JS中简单的实现像C#中using功能(有源码下载)
- 在C++中实现delegate
- 用JScript实现VB.Net,C#的[委托Delegate]:
- 用 C++ 实现 C# 中的 委托/事件 (5-functor2)
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- 较高人工智能的人机博弈程序实现(多个算法结合)含C++源码
- c#实现的俄罗斯方块程序(源码)
- 用C++实现C#中的委托/事件(标准C++之升级版)
- 一组实现邮件发送功能的c++封装类(附源码)
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- 根据经纬度求两点间距离实现源码(C#)
- C++'s Function pointer and C#'s delegate
- 一个C++ delegate的实现
- 用C#实现生成PDF文档(附源码)
- C#实现更改IP功能源码