一个类里可以注册不同类型的消息及boost:function和boost:bind的使用
来源:互联网 发布:情景喜剧排行知乎 编辑:程序博客网 时间:2024/05/22 01:03
/////////////////////////////////////////////////////////////////////////////////////////////////////
//BaseClass.h
#ifndef _BASECLASS_H_
#define _BASECLASS_H_#include "boost/function.hpp"
#include "boost/bind.hpp"
#include<iostream>
#include<string>
#include<list>
using namespace std;
typedef struct Func
{
boost::function<bool(int)> f;
string name;
};
class CButton
{
public:
bool ActionButtion(string signal, int i)
{
list<Func>::iterator it = m_signals.begin();
for(; it != m_signals.end(); ++it)
{
if((*it).name == signal)
{
(*it).f(i);
}
}
return true;
}
bool AddSignal(const Func &f)
{
m_signals.push_back(f);
return true;
}
private:
list<Func> m_signals;
};
class CPlayer
{
public:
bool play(int i){ cout << "play" << i << endl; }
bool playFast(int i){ cout << "playFast" << i << endl; }
bool stop(int i){ cout << "stop" << i << endl; }
};
class CPlayerBack
{
public:
bool freshBack(int i)
{
cout << "freshBack: " << i << endl;
}
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//BaseClass.cpp
#include"BaseClass.h"
int main()
{
//生成一个按钮对象
CButton OnPlay;
//这是两个界面,当按钮有按下“click”等消息时,这两个界面可能同时改变
CPlayer play;
CPlayerBack playback;
//注册的三个消息响应函数
Func f1 = { boost::bind(&CPlayer::play, play, _1),"click"};
Func f3 = { boost::bind(&CPlayer::playFast, play, _1),"double_click"};
Func f4 = { boost::bind(&CPlayerBack::freshBack, playback, _1),"click"};
//在按钮中添加3个响应消息
OnPlay.AddSignal(f1);
OnPlay.AddSignal(f3);
OnPlay.AddSignal(f4);
//按钮在响应click时候,会调用上面两个不同类型的消息响应
OnPlay.ActionButtion("click", 1);
//同一个按钮还可以注册不同的消息
OnPlay.ActionButtion("double_click", 1);
CButton OnStop;
Func f2 = { boost::bind(&CPlayer::stop, play, _1),"click"};
OnStop.AddSignal(f2);
OnStop.ActionButtion("click", 1);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Makefile
a.out:Make
Make:
make -i clean
make target
target:
g++ -o a.out BaseClass.h BaseClass.cpp
clean:
rm a.out
- 一个类里可以注册不同类型的消息及boost:function和boost:bind的使用
- boost::function与boost::bind 的使用
- boost::function/bind 的使用
- boost bind及function的简单实现
- boost::bind和boost::function使用示例
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost的库中bind和function使用实例
- C++ boost bind与 boost function的使用
- 关于boost::function与boost::bind函数的使用心得
- 关于boost::function与boost::bind函数的使用心得
- boost::function 和 boost::bind 的工作原理
- Boost.Bind和Boost.Function的一种搭配使用法
- C++中std::tr1::function和bind 组件的使用 和 以boost::function和boost:bind取代虚函数
- 【boost】boost::bind的使用
- boost::function和boost::bind
- iPhone如何查询序列号 iPhone激活时间查询
- javascript深入理解js闭包
- mac10.10Apache配置笔记
- 锚点的使用(链接到网页中某个位置)
- PayPal Express Checkout开发
- 一个类里可以注册不同类型的消息及boost:function和boost:bind的使用
- java导出jar文件,制作.bat执行文件
- python的log使.
- 如何判断socket是否连接,异常断开拔出网线
- 将Vim配置成为一款强大的编辑工具之 ctags的安装和使用
- CentOS环境使用MonaServer
- 深入理解tomcat
- 电话邦php面试题及答案
- 一种串口高效收发思路及方案