1、slot:函数
(1)slot:
void helloworld()
{
std::cout << "Hello, World!" << std::endl;
}
(2)使用:
{
boost::signal<void ()>sig;
sig.connect(&helloworld);
sig(); //发送信号,激活槽
}
2、slot:自定义对象
(1)slot:
struct Hello
{
void operator() () const
{
std::cout << "Hello, ";
}
};
struct World
{
void operator() () const
{
std::cout << "World!" << std::endl;
}
};
(2)使用:两种方式
{
boost::signal<void ()> sig;
Hello hello;
sig.connect(hello);
sig.connect(World());
sig();
}
输出:
Hello, World!
【注】默认情况下,slot的调用顺序与connect顺序一致,用户可以指定slot的启动顺序,如:
{
boost::signal<void ()> sig;
sig.connect(1,World());
sig.connect(0,Hello());
sig();
}
输出:
Hello, World!
3、slot:有参数
(1)slot:
void printName(const std::string& user)
{
std::cout << user << " say: Hello World!\n";
}
void printAge(const int& nAge)
{
std::cout << "Age: " << nAge << "\n";
}
(2)使用:
{
boost::signal<void (const std::string&)> sig;
sig.connect(&printName);
sig("Tom"); //方式1
boost::signal<void ()> sig2;
sig2.connect(boost::bind(printAge, 20));//方式2
sig2();
}
【注】当signal传给所有slot的参数完全一致时,选择第一种方式;否则选择第二种。
4、slot:有返回值(1)slot:
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; }
(2)使用:
{
sig.connect(&product);
sig.connect("ient);
sig.connect(&sum);
sig.connect(&difference);
std::cout << sig(5, 3) << std::endl;
}
输出:
2
【注】默认返回最后一个被调用slot的返回值
5、自定义slot返回值处理器,如获取所有slot返回值中的最大值:
template<typename T>
struct maximum
{
typedef T result_type;
template<typename InputIterator>
T operator()(InputIterator first, InputIterator last) const
{
// If there are no slots to call, just return the
// default-constructed value
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 x, float y), maximum<float> > sig;
sig.connect("ient);
sig.connect(&product);
sig.connect(&sum);
sig.connect(&difference);
std::cout << sig(5, 3) << std::endl;
}
输出:
15
6、自定义slot返回值收集器,如将所有slot的返回值存储到vector中:
template<typename Container>
struct aggregate_values
{
typedef Container result_type;
template<typename InputIterator>
Container operator()(InputIterator first, InputIterator last) const
{
return Container(first, last);
}
};
使用:
{
boost::signal<float (float x, float y), aggregate_values<std::vector<float> > > sig;
sig.connect("ient);
sig.connect(&product);
sig.connect(&sum);
sig.connect(&difference);
std::vector<float> results = sig(5,3);
std::copy(results.begin(), results.end(), std::ostream_iterator<float>(std::cout, " "));
std::cout<<std::endl;
}
输出:
1.66667 15 8 2
7、connection管理
struct HelloStruct
{
void operator() () const
{
std::cout << "Hello, Struct!" << std::endl;
}
};
void HelloFunc()
{
cout << "Hello, Func!" << endl;
}
(1)disconnect slots
1> boost::signal 只能disconnect函数类型的slot。
2> boost::signals::connection 可以disconnect所有类型的slot。
如:
{
boost::signal<void ()> sig;
boost::signals::connection c = sig.connect(HelloStruct());//c connect 结构体对象
if (c.connected())
{
sig(); //输出:Hello, Struct!
}
c.disconnect(); // disconnect 结构体对象
assert(!c.connected()); //c 不再处于connected状态
sig(); //无操作
HelloStruct hello;
sig.connect(hello);
sig();
sig.disconnect(&hello); //disconnect 无效
sig();
sig.disconnect_all_slots(); //disconnect 有效
sig();
}
(2)block slots
如:
boost::signals::connection c = sig.connect(HelloWorld());
sig(); // Prints "Hello, World!"
c.block(); // block the slot
assert(c.blocked());
sig(); // No output: the slot is blocked
c.unblock(); // unblock the slot
sig(); // Prints "Hello, World!"
(3)scope connect
如:
{
boost::signals::scoped_connection c = sig.connect(ShortLived());
sig(); // will call ShortLived function object
}
sig(); // ShortLived function object no longer connected to sig
(4)automatic connect
使用对象作为slot时,可使该对象继承自boost::signals::trackable,解决对象被删除后仍使用的问题。