使用Boost :: Signals for C ++ Eventing的完整示例 [英] Complete example using Boost::Signals for C++ Eventing

查看:127
本文介绍了使用Boost :: Signals for C ++ Eventing的完整示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在boost.org的教程解决这个问题:
Boost.org信号教程,但示例不完整,有点过于简化。

I’m aware of the tutorial at boost.org addressing this: Boost.org Signals Tutorial, but the examples are not complete and somewhat over simplified. The examples there don’t show the include files and some sections of the code are a little vague.

这里是我需要的: br>
ClassA引发多个事件/信号

ClassB订阅这些事件(多个类可以订阅)

Here is what I need:
ClassA raises multiple events/signals
ClassB subscribes to those events (Multiple classes may subscribe)

在我的项目中一个较低级别的消息处理程序类,它将事件引发到对这些消息进行一些处理并通知UI(wxFrames)的业务类。

In my project I have a lower-level message handler class that raises events to a business class that does some processing of those messages and notifies the UI (wxFrames). I need to know how these all might get wired up (what order, who calls who, etc).

推荐答案

下面的代码是您要求的最小工作示例。 ClassA 发出两个信号; SigA 发送(并接受)没有参数, SigB 发送 int 当每个函数被调用时,ClassB 有两个函数将输出到 cout 。在该示例中,有 ClassA a )和 ClassB b b2 )。 main 用于连接和触发信号。值得注意的是, ClassA ClassB 彼此不知道(即它们不是编译时绑定)。

The code below is a minimal working example of what you requested. ClassA emits two signals; SigA sends (and accepts) no parameters, SigB sends an int. ClassB has two functions which will output to cout when each function is called. In the example there is one instance of ClassA (a) and two of ClassB (b and b2). main is used to connect and fire the signals. It's worth noting that ClassA and ClassB know nothing of each other (ie they're not compile-time bound).

#include <boost/signal.hpp>
#include <boost/bind.hpp>
#include <iostream>

using namespace boost;
using namespace std;

struct ClassA
{
    signal<void ()>    SigA;
    signal<void (int)> SigB;
};

struct ClassB
{
    void PrintFoo()      { cout << "Foo" << endl; }
    void PrintInt(int i) { cout << "Bar: " << i << endl; }
};

int main()
{
    ClassA a;
    ClassB b, b2;

    a.SigA.connect(bind(&ClassB::PrintFoo, &b));
    a.SigB.connect(bind(&ClassB::PrintInt, &b,  _1));
    a.SigB.connect(bind(&ClassB::PrintInt, &b2, _1));

    a.SigA();
    a.SigB(4);
}

输出:


Foo
Bar: 4
Bar: 4

ve采取一些你通常不会在生产代码中使用的快捷方式(特别是访问控制是宽松的,你通常隐藏你的信号注册在一个函数,如KeithB的例子)。

For brevity I've taken some shortcuts that you wouldn't normally use in production code (in particular access control is lax and you'd normally 'hide' your signal registration behind a function like in KeithB's example).

看来, boost :: signal 中的大部分困难是习惯于使用 boost :: bind 。这是有点头脑弯曲起初!对于一个更棘手的例子,你也可以使用 bind 链接 ClassA :: SigA ClassB :: PrintInt 即使 SigA 不会 发出 int

It seems that most of the difficulty in boost::signal is in getting used to using boost::bind. It is a bit mind-bending at first! For a trickier example you could also use bind to hook up ClassA::SigA with ClassB::PrintInt even though SigA does not emit an int:

a.SigA.connect(bind(&ClassB::PrintInt, &b, 10));

希望有所帮助!

这篇关于使用Boost :: Signals for C ++ Eventing的完整示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆