4

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 Answers 2

3

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.

3
  • 1
    Would you also need to add a disconnect method call in order to actually disconnect the signal/slot relationship before removing it from the map? Or would that happen automatically (i.e., that's handled in the destructor of the boost::signals2::connection object)?
    – Jason
    Jun 7, 2011 at 15:00
  • You should use scoped_connection.
    – ronag
    Jun 7, 2011 at 15:24
  • Thanks, I've implemented it very much like this now, except there's no copy constructor for scoped_connection, so I've still got my ConnectionList class that disconnects and deletes all connections on destruction. I will update my question when I've done some testing.
    – Jonatan
    Jun 7, 2011 at 18:13
0

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.

1
  • The connection object itself occupies some memory, even if disconnected. So if the signal is destroyed, the connection-object is set to disconnected, but not deallocated. When PeopleBrowser is destroyed all its connection-objects will be deallocated, but PeopleBrowser may be long lived (in this case for the entire lifespan of the process). And the signal is not always destroyed, it could be that PersonBrowser just don't care about that particular person anymore, and should not receive any signals from it anymore.
    – Jonatan
    Jun 7, 2011 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.