Handle connection/disconnection of many signals/slots with boost::signals2

霸气de小男生 提交于 2019-12-09 03:38:37

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6265797/handle-connection-disconnection-of-many-signals-slots-with-boostsignals2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!