Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext

Class template slot

boost::signals2::slot — Pass slots as function arguments, and associate tracked objects with a slot.

Synopsis

// In header: <boost/signals2/slot.hpp>

template<typename Signature,   // Function type R (T1, T2, ..., TN)
         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 &);

  // slot function access
  slot_function_type & slot_function();
  const slot_function_type & slot_function() const;
};

Description

A slot consists of a polymorphic function wrapper (boost::function by default) plus a container of weak_ptrs 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/destruct

  1. template<typename Slot> slot(const Slot & target);

    Effects:

    Initializes the SlotFunction object in this with target, which may be any function object with which a SlotFunction can be constructed.

    In this special case where the template type parameter Slot is a compatible signals2::signal type, the signal will automatically be added to the slot's tracked object list. Otherwise, the slot's tracked object list is initially empty.

  2. template<typename OtherSignature, typename OtherSlotFunction> 
      slot(const slot<OtherSignature, OtherSlotFunction> & other_slot);

    Effects:

    Initializes this with a copy of other_slot's OtherSlotFunction object and tracked object list.

  3. 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 bind() when the constructor is passed more than one argument. As if: slot(boost::bind(f, a1, a2, ..., aN))

slot invocation

  1. result_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 SlotFunction object.

    Returns:

    The result returned by the slot's SlotFunction object.

    Throws:

    Any exceptions thrown by the slot's SlotFunction object. boost::signals2::expired_slot if any object in the tracked object list has expired.

    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 SlotFunction directly.

slot tracking

  1. slot & 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 operator() or lock() methods will throw an signals2::expired_slot exception.

    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 shared_ptr.

    In the case of passing another slot as the argument to track(), only the objects currently in the other slot's tracked object list are added to the tracked object list of this. The other slot object itself is not tracked.

    Returns:

    *this

slot slot function access

  1. slot_function_type & slot_function();
    const slot_function_type & slot_function() const;

    Returns:

    A reference to the slot's underlying SlotFunction object.


PrevUpHomeNext