Learning c++ and trying to get familiar with some patterns. The signals2 doc clearly has a vast array of things I can do with slots and signals. What I don\'t understand is
Boost.Signals2
is not just "an array of callbacks", it has a lot of added value. IMO, the most important points are:
connection
and scoped_connection
handles that allow disconnection without having direct access to the signal
. Note that this is the only way to disconnect incomparable slots, like boost::function
(or std::function
).Automatic slot lifespan tracking: a signal disconnects automatically from "expired" slots. Consider the situation when a slot is a binder referencing a non-copyable object managed by shared_ptr
s:
shared_ptr<listener> l = listener::create();
auto slot = bind(&listener::listen, l.get()); // we don't want aSignal_ to affect `listener` lifespan
aSignal_.connect(your_signal_type::slot_type(slot).track(l)); // but do want to disconnect automatically when it gets destroyed
Certainly, one can re-implement all the above functionality on his own "using a vector of functions and calling each one in a loop" etc, but the question is how it would be better than Boost.Signals2
. Re-inventing the wheel is rarely a good idea.