os/ossrv/ossrv_pub/boost_apis/boost/signals/slot.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Boost.Signals library
sl@0
     2
sl@0
     3
// Copyright Douglas Gregor 2001-2004. Use, modification and
sl@0
     4
// distribution is subject to the Boost Software License, Version
sl@0
     5
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
sl@0
     8
// For more information, see http://www.boost.org
sl@0
     9
sl@0
    10
#ifndef BOOST_SIGNALS_SLOT_HEADER
sl@0
    11
#define BOOST_SIGNALS_SLOT_HEADER
sl@0
    12
sl@0
    13
#include <boost/signals/detail/signals_common.hpp>
sl@0
    14
#include <boost/signals/connection.hpp>
sl@0
    15
#include <boost/signals/trackable.hpp>
sl@0
    16
#include <boost/visit_each.hpp>
sl@0
    17
#include <boost/shared_ptr.hpp>
sl@0
    18
#include <cassert>
sl@0
    19
sl@0
    20
#ifdef BOOST_HAS_ABI_HEADERS
sl@0
    21
#  include BOOST_ABI_PREFIX
sl@0
    22
#endif
sl@0
    23
sl@0
    24
namespace boost {
sl@0
    25
  namespace BOOST_SIGNALS_NAMESPACE {
sl@0
    26
    namespace detail {
sl@0
    27
      class BOOST_SIGNALS_DECL slot_base {
sl@0
    28
        // We would have to enumerate all of the signalN classes here as
sl@0
    29
        // friends to make this private (as it otherwise should be). We can't
sl@0
    30
        // name all of them because we don't know how many there are.
sl@0
    31
      public:
sl@0
    32
        struct data_t {
sl@0
    33
          std::vector<const trackable*> bound_objects;
sl@0
    34
          connection watch_bound_objects;
sl@0
    35
        };
sl@0
    36
        shared_ptr<data_t> get_data() const { return data; }
sl@0
    37
sl@0
    38
        // Get the set of bound objects
sl@0
    39
        std::vector<const trackable*>& get_bound_objects() const
sl@0
    40
        { return data->bound_objects; }
sl@0
    41
sl@0
    42
        // Determine if this slot is still "active", i.e., all of the bound
sl@0
    43
        // objects still exist
sl@0
    44
        bool is_active() const 
sl@0
    45
        { return data->watch_bound_objects.connected(); }
sl@0
    46
sl@0
    47
      protected:
sl@0
    48
        // Create a connection for this slot
sl@0
    49
        void create_connection();
sl@0
    50
sl@0
    51
        shared_ptr<data_t> data;
sl@0
    52
sl@0
    53
      private:
sl@0
    54
        static void bound_object_destructed(void*, void*) {}
sl@0
    55
      };
sl@0
    56
    } // end namespace detail
sl@0
    57
sl@0
    58
    // Get the slot so that it can be copied
sl@0
    59
    template<typename F>
sl@0
    60
    reference_wrapper<const F>
sl@0
    61
    get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
sl@0
    62
      { return reference_wrapper<const F>(f); }
sl@0
    63
sl@0
    64
    template<typename F>
sl@0
    65
    const F&
sl@0
    66
    get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
sl@0
    67
      { return f; }
sl@0
    68
sl@0
    69
    template<typename F>
sl@0
    70
    const F&
sl@0
    71
    get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
sl@0
    72
      { return f; }
sl@0
    73
sl@0
    74
    // Get the slot so that it can be inspected for trackable objects
sl@0
    75
    template<typename F>
sl@0
    76
    const F&
sl@0
    77
    get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
sl@0
    78
      { return f; }
sl@0
    79
sl@0
    80
    template<typename F>
sl@0
    81
    const F&
sl@0
    82
    get_inspectable_slot(const reference_wrapper<F>& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
sl@0
    83
      { return f.get(); }
sl@0
    84
sl@0
    85
    template<typename F>
sl@0
    86
    const F&
sl@0
    87
    get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
sl@0
    88
      { return f; }
sl@0
    89
sl@0
    90
    // Determines the type of the slot - is it a signal, a reference to a
sl@0
    91
    // slot or just a normal slot.
sl@0
    92
    template<typename F>
sl@0
    93
    typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
sl@0
    94
    tag_type(const F&)
sl@0
    95
    {
sl@0
    96
      typedef typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
sl@0
    97
        the_tag_type;
sl@0
    98
      the_tag_type tag = the_tag_type();
sl@0
    99
      return tag;
sl@0
   100
    }
sl@0
   101
sl@0
   102
  } // end namespace BOOST_SIGNALS_NAMESPACE
sl@0
   103
sl@0
   104
  template<typename SlotFunction>
sl@0
   105
  class slot : public BOOST_SIGNALS_NAMESPACE::detail::slot_base {
sl@0
   106
    typedef BOOST_SIGNALS_NAMESPACE::detail::slot_base inherited;
sl@0
   107
    typedef typename inherited::data_t data_t;
sl@0
   108
sl@0
   109
  public:
sl@0
   110
    template<typename F>
sl@0
   111
    slot(const F& f) : slot_function(BOOST_SIGNALS_NAMESPACE::get_invocable_slot(f, BOOST_SIGNALS_NAMESPACE::tag_type(f)))
sl@0
   112
    {
sl@0
   113
      this->data.reset(new data_t);
sl@0
   114
sl@0
   115
      // Visit each of the bound objects and store them for later use
sl@0
   116
      // An exception thrown here will allow the basic_connection to be
sl@0
   117
      // destroyed when this goes out of scope, and no other connections
sl@0
   118
      // have been made.
sl@0
   119
      BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor 
sl@0
   120
        do_bind(this->data->bound_objects);
sl@0
   121
      visit_each(do_bind, 
sl@0
   122
                 BOOST_SIGNALS_NAMESPACE::get_inspectable_slot
sl@0
   123
                   (f, BOOST_SIGNALS_NAMESPACE::tag_type(f)));
sl@0
   124
      create_connection();
sl@0
   125
    }
sl@0
   126
sl@0
   127
#ifdef __BORLANDC__
sl@0
   128
    template<typename F>
sl@0
   129
    slot(F* f) : slot_function(f)
sl@0
   130
    {
sl@0
   131
      this->data.reset(new data_t);
sl@0
   132
      create_connection();
sl@0
   133
    }
sl@0
   134
#endif // __BORLANDC__
sl@0
   135
sl@0
   136
    // We would have to enumerate all of the signalN classes here as friends
sl@0
   137
    // to make this private (as it otherwise should be). We can't name all of
sl@0
   138
    // them because we don't know how many there are.
sl@0
   139
  public:
sl@0
   140
    // Get the slot function to call the actual slot
sl@0
   141
    const SlotFunction& get_slot_function() const { return slot_function; }
sl@0
   142
sl@0
   143
    void release() const { data->watch_bound_objects.set_controlling(false); }
sl@0
   144
sl@0
   145
  private:
sl@0
   146
    slot(); // no default constructor
sl@0
   147
    slot& operator=(const slot&); // no assignment operator
sl@0
   148
sl@0
   149
    SlotFunction slot_function;
sl@0
   150
  };
sl@0
   151
} // end namespace boost
sl@0
   152
sl@0
   153
#ifdef BOOST_HAS_ABI_HEADERS
sl@0
   154
#  include BOOST_ABI_SUFFIX
sl@0
   155
#endif
sl@0
   156
sl@0
   157
#endif // BOOST_SIGNALS_SLOT_HEADER