处理与升压:: signals2连接/断开许多信号/插槽 [英] Handle connection/disconnection of many signals/slots with boost::signals2

查看:143
本文介绍了处理与升压:: signals2连接/断开许多信号/插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用的,而不是我的旧signals- code的boost :: signals2启动。我在与管理虽然多个连接有问题。这里是我的问题:

I've started using boost::signals2 instead of my old signals-code. I'm having a problem with administering multiple connections though. Here's my problem:

我有Person类的许多实例:

I have many instances of the class Person:

class Person {
public:
    void SetName (string new_name)
    {
        name = new_name;
        NameChange (name);
    }

    string name;
    boost::signals2::signal<Person*> NameChange;
};

我也有一个人的浏览器,必须监控所有可用的人更改一个子集。由于人们可以来从子去,我必须有一种方法来处理连接对象,我创建了一个类(ConnectionList)来处理:

I also have a people-browser, that must monitor a subset of all available people for changes. Since people can come and go from that subset I must have a way to handle the connection objects, and I've created a class (ConnectionList) to handle that:

class ConnectionList
{
public:
    virtual ~ConnectionList () // drops all connections in "list"
    void add (boost::signals2::connection& conn); // adds "conn" to "list"
private:
    std::vector<boost::signals2::connection> list;
};

class PeopleBrowser
{
public:
    void AddPerson (Person& p)
    {
        name_change_connections.add (p.NameChange.connect (...));
    }
private:
    ConnectionList name_change_connections;
};

这是全好了,连接时PeopleBrowser被删除,并且有一个很好的方式来添加新的连接下降。

This is all well, the connections are dropped when PeopleBrowser is deleted and there is a nice way to add new connections.

不过,我们需要添加另一种方法,RemovePerson,并且该方法必须删除该人实例的NameChange信号的连接。

However, we need to add another method, RemovePerson, and that method must remove the connections to the NameChange-signal of that Person-instance.

这就是我卡住了。我想我可以做ConnectionList一个模板,并使用保存与该信号的基准,以及连接的结构列表,然后添加下降到该信号的所有连接的方法。

This is where I'm stuck. I guess I could make ConnectionList a template and use a list that holds a struct with a reference to the signal as well as the connection, and then add a method that drops all connections to that signal.

但似乎这是这样一个共同的情况下(在我的世界,至少我有一个20个班在这个单一的应用程序需要此功能),所以我认为必须有一个更好的方式来处理呢?

But it seems that this is such a common case (at least in my world, I have like 20 classes in this single app that needs this functionality), so I think there must be a better way to handle this?

最起码,有没有什么办法让从连接对象的连接信号的参考?

At the very least, is there any way to get a reference to the connected signal from a connection object?

也许libsigc ++处理这更好/不同?

Perhaps libsigc++ handle this better/differently?

推荐答案

什么:

class PeopleBrowser
{
public:
    void AddPerson (Person& p)
    {
        name_change_connections[&p] = p.NameChange.connect(...);
    }
    void RemovePerson(Person& p)
    {
         name_change_connections.erase(&p);
    }

private:
    std::map<Person*, boost::signals2::scoped_connection> name_change_connections;
};

您可能也想看看的自动连接管理

这篇关于处理与升压:: signals2连接/断开许多信号/插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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