boost之signal

boost里的signal是一个模板类,不区分信号种类,产生信号统一用()调用操作符。

1.回调普通函数代码示例:

#include <iostream>
#include <string>
#include <vector>
#include <boost/signals2.hpp>
using namespace std;
using namespace boost::signals2;

void slots1()
{
	cout << "Hello slots1" <<endl;
};

void slots2()
{
	cout << "Hello slots2" <<endl;
}


int main()
{
	signal<void()> sig;
	sig.connect(&slots1);
	sig.connect(&slots2);
	sig();
	return 0;
}

 2.使用组号和注册位置代码示例,可以使用有名对象,传递的是函数对象。

#include <iostream>
#include <string>
#include <vector>
#include <boost/signals2.hpp>
using namespace std;
using namespace boost::signals2;
template<int N>
struct slots
{
	void operator()()
	{
		cout << "slot" << " "<<N <<endl;
	}
};


int main()
{
	signal<void()> sig;
	//slots<1> test;
	//sig.connect(test,at_back);有名对象
	sig.connect(slots<1>(),at_back);//无名对象
	sig.connect(slots<100>(),at_front);

	//使用组号
	sig.connect(5,slots<51>(),at_back);
	sig.connect(5,slots<55>(),at_front);
	sig();
	return 0;
}

 

posted @ 2013-08-13 18:41  l851654152  阅读(401)  评论(0编辑  收藏  举报