您的位置:首页 > 编程语言 > C语言/C++

Boost.Signals2:在C++中实现事件处理

2012-10-29 13:48 99 查看
#include <iostream>
#include <boost/signals2/signal.hpp>

using std::cout;
using std::endl;

struct A
{
	void f() {
		int a = 2;
		event(1, &a);
	}
	boost::signals2::signal<void (int, int*)> event;
};

void h5(int i, int* p) {cout << "h5\t" << i << " " << *p << endl;}

struct B
{
	A a;
	void g() {
		a.event.connect(std::bind(&B::h, this, std::placeholders::_1, std::placeholders::_2));
		a.event.connect(std::bind(&B::h, this, std::placeholders::_1, std::placeholders::_2));
		a.event.connect(std::bind(&B::h1, this, std::placeholders::_2, std::placeholders::_1));
		a.event.connect(std::bind(&B::h2, this, 2, std::placeholders::_1, std::placeholders::_2));
		a.event.connect(std::bind(&B::h3, this, std::placeholders::_2));
		a.event.connect(std::bind(&B::h4, this));
		a.event.connect(h5);
		a.event.connect([](int i, int* p) {cout << "h6\t" << i << " " << *p << endl;});
		a.f();
	}
	void h(int i, int* p) {cout << "h\t" << i << " " << *p << endl;}
	void h1(int* p, int i) {cout << "h1\t" << i << " " << *p << endl;}
	void h2(int i, int j, int* p) {cout << "h2\t" << i << " " << j << " " << *p << endl;}
	void h3(int* p) {cout << "h3\t" << *p << endl;}
	void h4() {cout << "h4" << endl;}
};

int main()
{
	B b;
	b.g();

	return 0;
}

/*
h       1 2
h       1 2
h1      1 2
h2      2 1 2
h3      2
h4
h5      1 2
h6      1 2
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: