Handle connection/disconnection of many signals/sl

2019-02-24 06:52发布

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:

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;
};

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;
};

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

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

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.

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?

Perhaps libsigc++ handle this better/differently?

2条回答
疯言疯语
2楼-- · 2019-02-24 07:17

I haven't tried it myself but according to boost documentation

When Can Disconnections Occur? (Intermediate)

Signal/slot disconnections occur when any of these conditions occur:

  • The connection is explicitly disconnected via the connection's disconnect method directly, or indirectly via the signal's disconnect method, or scoped_connection's destructor.
  • An object tracked by the slot is destroyed.
  • The signal is destroyed.

Unless you use a scoped_connection the connection between a signal and slot will remain valid until either of them is destroyed. So as far as I understand you don't need to store connection objects in a vector. Just connect your signal to your slot as you are doing now.

When your observed object goes out of scope it will delete the connection by itself.

This is a much simpler design.

查看更多
太酷不给撩
3楼-- · 2019-02-24 07:29

What about:

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;
};

You might also want to take a look at automatic connection management.

查看更多
登录 后发表回答