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?