boost::function 和boost::bind
2015-09-17 16:37
435 查看
#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:
CPlayer(){}
CPlayer(CButton &b)
{//默认消息
Func f1 = { boost::bind(&CPlayer::Ok, this, _1),"Ok"};
b.AddSignal(f1);
}
bool play(int i){ cout << "play" << i << endl; }
bool playFast(int i){ cout << "playFast" << i << endl; }
bool stop(int i){ cout << "stop" << i << endl; }
bool Ok(int i){ cout << "Ok" << i << endl; }
};
class CPlayerBack
{
public:
bool freshBack(int i)
{
cout << "freshBack: " << i << endl;
}
};
#endif
#include"BaseClass.h"
int main()
{
//生成一个按钮对象
CButton OnPlay;
//这是两个界面,当按钮有按下“click”等消息时,这两个界面可能同时改变
CPlayer play(OnPlay);
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("Ok", 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;
}
#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:
CPlayer(){}
CPlayer(CButton &b)
{//默认消息
Func f1 = { boost::bind(&CPlayer::Ok, this, _1),"Ok"};
b.AddSignal(f1);
}
bool play(int i){ cout << "play" << i << endl; }
bool playFast(int i){ cout << "playFast" << i << endl; }
bool stop(int i){ cout << "stop" << i << endl; }
bool Ok(int i){ cout << "Ok" << i << endl; }
};
class CPlayerBack
{
public:
bool freshBack(int i)
{
cout << "freshBack: " << i << endl;
}
};
#endif
#include"BaseClass.h"
int main()
{
//生成一个按钮对象
CButton OnPlay;
//这是两个界面,当按钮有按下“click”等消息时,这两个界面可能同时改变
CPlayer play(OnPlay);
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("Ok", 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;
}
相关文章推荐
- python2.7使用ansible
- 既使用maven编译,又使用lib下的Jar包
- 时间字符串
- mysql远程连接 Host * is not allowed to connect to this MySQL server
- 使用myeclipse web browser 中文显示乱码
- Android手表商场项目总结
- 整理:Android apk 框架 布局 集锦
- Fiddler (四) 实现手机的抓包 - 小坦克
- 经典C++库 http://hereson.iteye.com/blog/200799
- 关于单行、多行文本超出显示省略号... css
- C# 策略模式
- move_uploaded_file文件上传失败 上传之后文件名是乱码
- 利用Div设计TabControl效果
- 用Python学习统计学基础-3
- sudoers文件解析
- java反射介绍
- Method Swizzling 和 AOP 实践(转)
- 上海博通Bk芯片
- win7通过VNC远程桌面连接Ubuntu14.04
- JavaScript基础知识点系列一