管理不同类型的回调函数
来源:互联网 发布:矩阵加一列 秩不变 编辑:程序博客网 时间:2024/06/05 08:14
当有多个不同参数的回调函数需要管理时我们怎么办呢?它们并不能存放在同一个数组或者容器里面。
首先,我们可以定义一个抽象基类MyFunc,只有一个用来描述类型的成员变量type。然后从基类中派生出一个模板类SubFunc这样就可以存放不同的回调函数了。
由于这些存放回调函数的模板类都是从同一个基类派生,所以我们直接存放它们的父类就可以了,之后可以根据父类
中的类型dynamic_cast为相应的子类。
typedef std::tr1::function<int(const unsigned char*, unsigned int)> TestCallback;class MyFunc{public: int type; virtual ~MyFunc() = 0 { }};template<typename Fty>class SubFunc : public MyFunc{public: SubFunc(int t, std::tr1::function<Fty> f) { type = t; func = f; } std::tr1::function<Fty> func;};void test1(int i){ std::cout << "test1:" << i << std::endl;}void test2(const std::string &str){ std::cout << "test2:" << str << std::endl;}void test3(int i, const std::string &str){ std::cout << "test3:" << i << "," << str << std::endl;}typedef std::tr1::shared_ptr<MyFunc> MyFuncPtr;std::map<int, MyFuncPtr> g_arr;void Add1(std::tr1::function<void(int)> f){ MyFuncPtr ptr(new SubFunc<void(int)>(1, f)); g_arr.insert(std::pair<int, MyFuncPtr>(1111, ptr));}void Add2(std::tr1::function<void(const std::string&)> f){ MyFuncPtr ptr(new SubFunc<void(const std::string&)>(2, f)); g_arr.insert(std::pair<int, MyFuncPtr>(2222, ptr));}void Add3(std::tr1::function<void(int, const std::string&)> f){ MyFuncPtr ptr(new SubFunc<void(int, const std::string&)>(3, f)); g_arr.insert(std::pair<int, MyFuncPtr>(3333, ptr));}void HandleAllCallback(const unsigned char *response){ // 解析response std::map<int, MyFuncPtr>::iterator iter = g_arr.find(3333); if (iter!=g_arr.end()) { MyFunc *p = iter->second.get(); switch (p->type) { case 1: { int i = 1111; dynamic_cast<SubFunc<void(int)>*>(p)->func(i); } break; case 2: { std::string str2 = "str2"; dynamic_cast<SubFunc<void(const std::string&)>*>(p)->func(str2); } break; case 3: { std::string str3 = "str3"; dynamic_cast<SubFunc<void(int, const std::string&)>*>(p)->func(3333, str3); } break; default: break; } }}int _tmain(int argc, _TCHAR* argv[]){ // 接口 Add1(std::tr1::bind(test1, std::tr1::placeholders::_1)); Add2(std::tr1::bind(test2, std::tr1::placeholders::_1)); Add3(std::tr1::bind(test3, std::tr1::placeholders::_1, std::tr1::placeholders::_2)); // OnMessage unsigned char *response = NULL; HandleAllCallback(response); system("pause"); return 0;}
- 管理不同类型的回调函数
- PHP 利用MySQL管理Session的回调函数
- ucgui 窗口管理及回调函数的应用
- 不同类型数的运算
- dojo.Deferred 管理回调函数
- 回调函数的学习
- 回调函数的意义
- 回调函数的介绍
- 回调函数的例子
- 回调函数的例子
- 回调函数的迷失
- 回调函数的使用
- OpenGL的回调函数
- 回调函数的隐患
- 回调函数的开头
- 回调函数的使用
- php的回调函数
- JAVA的回调函数
- 字符串查找--B中是否有元素不在A中
- Codeforces Round #190 (Div. 2) A B C
- 汇编程序hello world
- vmware9 安装centOS5.4 并连接internet
- Spring面向切面编程
- 管理不同类型的回调函数
- 实验室的一段Python脚本,根据用户指定路径来备份文件
- iOS如何实现表格的折叠效果?
- 简单UDP日志程序
- android手势识别
- C++ 与 字符编码格式
- Fedora 17 无法播放mp3 rmvb
- Megaco/H.248, MGCP
- LOG4CXX编译,使用,简单封装