struct HelloWorld { void operator()() const { std::cout << "Hello, World!" << std::endl; } };
boost::signal<void ()>sig;
int main(int argc, const char * argv[]) {
HelloWorld helloworld;
sig.connect(helloworld);
sig();
return 0;
}
上面的demo实现的是一个方法的连接,下面是一个signal连接多个方法,然后当调用 sig时同时执行连接的几个方法。
struct Hello{
void operator()()const{
std::cout<<"Hello ";
}
};
struct World{
void operator()()const{
std::cout<<"World"<<std::endl;
}
};
boost::signal<void ()>sig;
int main(int argc, const char * argv[]) {
sig.connect(Hello());
sig.connect(World());
sig();
return 0;
}
上面的代码最终输出的World \n hello
struct Hello{
void operator()()const{
std::cout<<"Hello ";
}
};
struct World{
void operator()()const{
std::cout<<"World"<<std::endl;
}
};
struct GoodMorning
{
void operator()() const
{
std::cout << "... and good morning!" << std::endl;
}
};
boost::signal<void ()>sig;
int main(int argc, const char * argv[]) {
sig.connect(0,Hello());
sig.connect(1,World());
sig.connect(GoodMorning());
sig();
return 0;
}
除了上面的关联外还可以连接时传入参数,传入的参数会自动传送给所有连接的方法中。
/
// main.cpp
// signals2
//
// Created by wenchangshou on 15/11/17.
// Copyright ? 2015年 wenchangshou. All rights reserved.
//
#include <iostream>
#include <boost/signal.hpp>
void print_args(float x, float y)
{
std::cout << "The arguments are " << x << " and " << y << std::endl;
}
void print_sum(float x, float y)
{
std::cout << "The sum is " << x + y << std::endl;
}
void print_product(float x, float y)
{
std::cout << "The product is " << x * y << std::endl;
}
void print_difference(float x, float y)
{
std::cout << "The difference is " << x - y << std::endl;
}
void print_quotient(float x, float y)
{
std::cout << "The quotient is " << x / y << std::endl;
}
boost::signal<void (float,float)>sig;
int main(int argc, const char * argv[]) {
sig.connect(&print_args);
sig.connect(&print_sum);
sig.connect(&print_product);
sig.connect(&print_difference);
sig.connect(&print_quotient);
sig(5., 3.);
return 0;
}
我们不仅可以传入参数也可以根据传入的参数的返回结果。
#include <iostream>
#include <boost/signal.hpp>
float product(float x, float y) { return x * y; }
float quotient(float x, float y) { return x / y; }
float sum(float x, float y) { return x + y; }
float difference(float x, float y) { return x - y; }
boost::signal<float (float,float)>sig;
int main(int argc, const char * argv[]) {
sig.connect(&product);
sig.connect("ient);
sig.connect(&sum);
sig.connect(&difference);
std::cout<<sig(5., 3.)<<std::endl;
return 0;
}
下面的例子是返回其中最大的数:
template<typename T>
struct maximum{
typedef T result_type;
template<typename InputIterator>
T operator()(InputIterator first,InputIterator last) const{
if(first==last)return T();
T max_value=*first++;
while(first!=last){
if(max_value<*first)
max_value=*first;
++first;
}
return max_value;
}
};
boost::signal<float (float,float),maximum<float>>sig;
有的时候我们希望将所有槽的的返回结果放入一个组合当中
template<typename Container>
struct aggregate_values{
typedef Container result_type;
template<typename InputIterator>
Container operator()(InputIterator first,InputIterator last) const{
Container values;
while(first!=last){
values.push_back(*first);
first++;
}
return values;
}
};
std::vector<float> results=sig(5,3);
std::copy(results.begin(), results.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << "\n";
下面的是模拟一个按照的操作,我们定义了一个原型void(int x,int y),当我们操作doOnClick时传入符合的函数签名时会进行连接,当调用 OnClick会操作绑定的方法
class Button
{
typedef boost::signals2::signal<void (int x, int y)> OnClick;
public:
typedef OnClick::slot_type OnClickSlotType;
// 事件绑定,传入一个事件自动进行关系,传入的函数的定义OnClick;
boost::signals2::connection doOnClick(const OnClickSlotType & slot);
// 模拟用户的点击操作,输入坐标 52,38
void simulateClick();
private:
OnClick onClick;
};
boost::signals2::connection Button::doOnClick(const OnClickSlotType & slot)
{
return onClick.connect(slot);
}
void Button::simulateClick()
{
onClick(52, 38);
}
void printCoordinates(long x, long y)
{
std::cout << "(" << x << ", " << y << ")\n";
}
//boost::signal<float (float,float),aggregate_values<vector<float>>>sig;
int main(int argc, const char * argv[]) {
Button button;
button.doOnClick(&printCoordinates);
button.simulateClick();
}
原文:http://my.oschina.net/u/215677/blog/531586