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
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.
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'sdisconnect
method, orscoped_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.