// 忽略警告
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <assert.h>
#include <iostream>
#include <boost/signals2/signal.hpp>
#include <boost/signals2/shared_connection_block.hpp>
using namespace std;
#include <stdio.h>
#include <numeric>
#include <boost/shared_ptr.hpp>
void slots1()
{
cout << "slots1 call:" << endl;
}
int slots2(int &a)
{
cout << "slots2 call: " << ++a << " ok" << endl;
return 2222;
}
int slots3(int &a)
{
cout << "slots3 call: " << a++ << " ok" << endl;
return 3333;
}
struct Hello
{
void operator()() const
{
std::cout << "Hello" << endl;
}
};
// 合并器
template<typename T>
class Combiner1
{
T v;
public:
typedef std::pair<T, T> result_type;
Combiner1(T t = T()) :v(t){}
template<typename InputIterator>
result_type operator()(InputIterator begin, InputIterator end) const
{
if (begin == end){ return result_type(); }
vector<T> vec(begin, end);
T sum = std::accumulate(vec.begin(), vec.end(), v); // 求和
T max = *std::max_element(vec.begin(), vec.end()); // 最大值
return result_type(sum, max);// retuen pair
}
};
// 满足值大于30则终止迭代,不再调用剩余插槽
template<typename T>
class Combiner2
{
T v;
public:
typedef void result_type;
Combiner2(T t = T()) :v(t){}
template<typename InputIterator>
result_type operator()(InputIterator begin, InputIterator end) const
{
while (begin != end)
{
if (*begin > 30)
return;
else
++begin;
}
return;
}
};
template<int N>
struct soltss
{
int operator()(int x)
{
cout << "slotss" << N << endl;
return x * N;
}
};
int main()
{
boost::signals2::signal<void()> sig;// <> 中void为返回值类型,()中是参数类型
sig.connect(&slots1);// 绑定函数sig.connect(slots1);
sig.connect(Hello());
sig();// 触发函数 slots1 call:/Hello
boost::signals2::signal<int(int&)> sig1;
// 参数1:组编号 可以缺省 num值 和 调用顺序
// 参数3:插入到该组的位置
boost::signals2::connection s1 = sig1.connect(0, &slots2, boost::signals2::at_back); // 5 2
boost::signals2::connection s2 = sig1.connect(0, &slots3, boost::signals2::at_front); // 7 1
boost::signals2::connection s3 = sig1.connect(1, &slots2, boost::signals2::at_front); // 8 3
boost::signals2::connection s4 = sig1.connect(1, &slots3, boost::signals2::at_back); // 8 4
// 没有组号为 8 8 6 8 3 2 1 4
int num = 5;
cout << *sig1(num) << endl;// 输出 最后一个信号绑定的函数的返回值 3333
{
boost::signals2::shared_connection_block block(s1);// {}暂时阻塞信号
cout << "信号数量:" << sig1.num_slots() << endl;// 数量为4
s1.blocked() ? cout << "s1被阻塞" << endl : cout << "s1未被阻塞" << endl;//s1被阻塞
num = 5;
cout << *sig1(num) << endl;// 少s1的输出 577 3333
block.unblock();// 重新启用信号
s1.blocked() ? cout << "s1被阻塞" << endl : cout << "s1未被阻塞" << endl;//s1未被阻塞
num = 5;
cout << *sig1(num) << endl;// 正常输出
}
num = 5;
cout << *sig1(num) << endl;
s1.connected() ? cout << "s1已连接!" << endl : cout << "s1未连接!" << endl;// s1已连接!
s1.disconnect();// 断开信号s1
s1.connected() ? cout << "s1已连接!" << endl : cout << "s1未连接!" << endl;//s1未连接!
cout << "信号数量:" << sig1.num_slots() << endl;// 3
sig1.disconnect(0);// 断开组0所有信号
cout << "信号数量:" << sig1.num_slots() << endl;// 2
sig1.disconnect(&slots2);
cout << "信号数量:" << sig1.num_slots() << endl;// 1
sig1.disconnect_all_slots();// 断开所有信号
cout << "信号数量:" << sig1.num_slots() << endl;// 0
cout << sig1.empty() << endl;// true
boost::signals2::signal<int(int&)> sig2;
sig2.connect(&slots2);
cout << "信号数量:" << sig2.num_slots() << endl;// 1
{
boost::signals2::scoped_connection sc = sig2.connect(&slots3);
//sc.disconnect();
sc.blocked() ? cout << "sc被阻塞" << endl : cout << "sc未被阻塞" << endl;//sc未被阻塞
sc.connected() ? cout << "sc已连接!" << endl : cout << "sc未连接!" << endl;//sc已连接!
cout << "信号数量:" << sig2.num_slots() << endl;// 2
num = 5;
cout << *sig2(num) << endl; // 6 6
// 离开作用域 sc与sig2的连接自动断开
}
cout << "信号数量:" << sig2.num_slots() << endl;// 1
// 合并器(得到和、最大值)
// 默认合并器boost::signals2::optional_last_value<int>
typedef boost::signals2::signal<int(int), Combiner2<int>> signal_t;
signal_t sig3;
sig3.connect(soltss<10>());
boost::shared_ptr<soltss<100>> ps(new soltss<100>);
// 自动管理插槽的连接
sig3.connect(signal_t::slot_type(boost::ref(*ps)).track(ps));
assert(sig3.num_slots() == 2);
ps.reset();// 销毁插槽, 该槽自动断开
assert(sig3.num_slots() == 1);
sig3.connect(soltss<20>());
sig3.connect(soltss<50>());
sig3(2);// slotss10 20 50
//cout << res.first << "," << res.second << endl;// 160,100
// slot - function - bind
typedef boost::signals2::signal<int(int&)> signal_t1;
signal_t1 sig4;
// 直接跟踪function
boost::function<int(int&)> funcc = boost::ref(slots2);
//boost::function<int(int&)> funcc = boost::bind(&slots2, _1);
sig4.connect(funcc);
// bind绑定
sig4.connect(signal_t1::slot_type(&slots2,_1));// 类成员函数则 (类::函数名,对象/对象指针,占位符)
num = 5;
sig4(num);// 6 7
getchar();
}