一个类里可以注册不同类型的消息及boost:function和boost:bind的使用
2015-09-17 15:25
453 查看
/////////////////////////////////////////////////////////////////////////////////////////////////////
//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
//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
相关文章推荐
- PHP之数组array
- 中国移动OnetNet云平台 使用以太网传输数据流步骤
- mac10.10Apache配置笔记
- C++中对象的常引用、动态建立和释放相关知识讲解
- javascript深入理解js闭包
- 锚点的使用(链接到网页中某个位置)
- Spring与Quartz的整合实现定时任务调度
- PayPal Express Checkout开发
- Jquery 数组操作
- 规范命名
- How does Spring know that ThrowsAdvice.afterThrowing needs to be called?
- sed详解
- 关于多实例redis主从+Keepalived故障切换的解决方法
- Java总结篇系列:Java泛型
- 深入理解C++
- iOS之UITabBar
- Android 手机通讯录的实现源码
- Git入门教程
- 基于Cocos2dx + box2d 愤怒的小鸟的实现Demo
- Ajax清除浏览器js、css、图片缓存的方法