...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::signals2::slot — Pass slots as function arguments, and associate tracked objects with a slot.
// In header: <boost/signals2/slot.hpp> template<typename Signature, typename SlotFunction = boost::function<R (T1, T2, ..., TN)> > class slot : public boost::signals2::slot_base { public: // types typedef R result_type; typedef T1 argument_type; // Exists iff arity == 1 typedef T1 first_argument_type; // Exists iff arity == 2 typedef T2 second_argument_type; // Exists iff arity == 2 typedef Signature signature_type; typedef SlotFunction slot_function_type; // static constants static const int arity = N; // The number of arguments taken by the slot. // member classes/structs/unions template<unsigned n> class arg { public: // types typedef Tn type; // The type of the slot's (n+1)th argument }; // construct/copy/destruct template<typename Slot> slot(const Slot &); template<typename OtherSignature, typename OtherSlotFunction> slot(const slot<OtherSignature, OtherSlotFunction> &); template<typename Func, typename Arg1, typename Arg2, ..., typename ArgN> slot(const Func &, const Arg1 &, const Arg2 &, ..., const ArgN &); // invocation result_type operator()(arg<0>::type, arg<1>::_type, ..., arg<N-1>::type); result_type operator()(arg<0>::type, arg<1>::_type, ..., arg<N-1>::type) const; // tracking slot & track(const weak_ptr<void> &); slot & track(const signals2::signal_base &); slot & track(const signals2::slot_base &); template<typename ForeignWeakPtr> slot & track_foreign(const ForeignWeakPtr &, typename weak_ptr_traits<ForeignWeakPtr>::shared_type * = 0); template<typename ForeignSharedPtr> slot & track_foreign(const ForeignSharedPtr &, typename shared_ptr_traits<ForeignSharedPtr>::weak_type * = 0); // slot function access slot_function_type & slot_function(); const slot_function_type & slot_function() const; };
A slot consists of a polymorphic function wrapper (boost::function by default)
plus a container of weak_ptr
s which identify the slot's "tracked objects". If any of the
tracked objects expire, the slot will automatically disable itself. That is, the slot's function
call operator will throw an exception instead of forwarding the function call to the slot's
polymorphic function wrapper. Additionally, a slot will automatically lock all the tracked objects
as shared_ptr
during invocation, to prevent any of them from expiring while
the polymorphic function wrapper is being run.
The slot constructor will search for signals2::signal and signals2::trackable inside incoming function objects and automatically track them. It does so by applying a visitor to the incoming functors with boost::visit_each.
slot
public
construct/copy/destructtemplate<typename Slot> slot(const Slot & target);
Effects: |
Initializes the In this special case where the template type parameter |
template<typename OtherSignature, typename OtherSlotFunction> slot(const slot<OtherSignature, OtherSlotFunction> & other_slot);
Effects: |
Initializes |
template<typename Func, typename Arg1, typename Arg2, ..., typename ArgN> slot(const Func & f, const Arg1 & a1, const Arg2 & a2, ..., const ArgN & aN);
Effects: |
Syntactic sugar for |
slot
invocationresult_type operator()(arg<0>::type a1, arg<1>::_type a2, ..., arg<N-1>::type aN); result_type operator()(arg<0>::type a1, arg<1>::_type a2, ..., arg<N-1>::type aN) const;
Effects: |
Calls the slot's |
Returns: |
The result returned by the slot's |
Throws: |
Any exceptions thrown by the slot's |
Notes: |
If you have already used lock to insure the
tracked objects are valid, it is slightly more efficient to use the
slot_function() method
and call the slot's |
slot
trackingslot & track(const weak_ptr<void> & tracked_object); slot & track(const signals2::signal_base & tracked_signal); slot & track(const signals2::slot_base & tracked_slot);
Effects: |
Adds object(s) to the slot's tracked object list. Should any of the
tracked objects expire, then subsequent attempts to call the slot's When tracking a signal, a shared_ptr
internal to the signal class is used for tracking. The signal does not
need to be owned by an external
In the case of passing another slot as the argument to |
Returns: |
|
template<typename ForeignWeakPtr> slot & track_foreign(const ForeignWeakPtr & tracked_object, typename weak_ptr_traits<ForeignWeakPtr>::shared_type * SFINAE = 0); template<typename ForeignSharedPtr> slot & track_foreign(const ForeignSharedPtr & tracked_object, typename shared_ptr_traits<ForeignSharedPtr>::weak_type * SFINAE = 0);
Effects: |
The
In order to use a particular The second argument "SFINAE" may be ignored, it is used to resolve the overload between
either |
Returns: |
|