如何使用 signals.boostv2 调用特定插槽

How call o specific slot with signals.boostv2

本文关键字:插槽 调用 boostv2 何使用 signals      更新时间:2023-10-16

我开发过一个依赖于发布者订阅者的应用程序(使用 boost.signalsv2(

这是控制器;

#include "view.hpp"
class Controller
{
boost::signals2::signal<void ()> sig;
public:
Controller() {
}
void subscribe(listener& listener) {
// Signal with no arguments and a void return value
sig.connect(boost::bind(&listener::OnUpdate, &listener));
}
void DoWork() const {
// Call all of the slots
sig();
}


void Update();
};

doWork 函数调用所有订阅槽。

int main() {
Controller c;
View l1, l2;
c.subscribe(l1);
std::cout << "One subscribed:n";
c.DoWork();
c.subscribe(l2);
c.subscribe(l3);
std::cout << "nBoth subscribed:n";
c.DoWork();
}

有多个订户系统。(L1、L2 和 L3( 我想发布特定的一个 (l2( 我该如何检查并执行此操作?

这不是它的意图。如果要区分,请使用不同的信号或将令牌传递给处理程序,以便它们能够知道它们是否对该特定事件感兴趣。

如果您希望在连接的处理程序列表中有一个特定的/position/来获取事件,那么您可以使用替代/自定义组合器:https://www.boost.org/doc/libs/1_73_0/doc/html/signals2/thread-safety.html#id-1.3.36.7.3