os/ossrv/ossrv_pub/boost_apis/boost/signals/connection.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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_CONNECTION_HPP
sl@0
    11
#define BOOST_SIGNALS_CONNECTION_HPP
sl@0
    12
sl@0
    13
#include <boost/signals/detail/signals_common.hpp>
sl@0
    14
#include <boost/smart_ptr.hpp>
sl@0
    15
#include <boost/operators.hpp>
sl@0
    16
#include <boost/any.hpp>
sl@0
    17
#include <list>
sl@0
    18
#include <cassert>
sl@0
    19
#include <utility>
sl@0
    20
sl@0
    21
#ifdef BOOST_HAS_ABI_HEADERS
sl@0
    22
#  include BOOST_ABI_PREFIX
sl@0
    23
#endif
sl@0
    24
sl@0
    25
namespace boost {
sl@0
    26
  namespace BOOST_SIGNALS_NAMESPACE {
sl@0
    27
    class trackable;
sl@0
    28
sl@0
    29
    namespace detail {
sl@0
    30
      // Represents an object that has been bound as part of a slot, and how
sl@0
    31
      // to notify that object of a disconnect
sl@0
    32
      struct bound_object {
sl@0
    33
        void* obj;
sl@0
    34
        void* data;
sl@0
    35
        void (*disconnect)(void*, void*);
sl@0
    36
sl@0
    37
        bool operator==(const bound_object& other) const
sl@0
    38
          { return obj == other.obj && data == other.data; }
sl@0
    39
        bool operator<(const bound_object& other) const
sl@0
    40
          { return obj < other.obj; }
sl@0
    41
sl@0
    42
        // To support intel 80 compiler, 2004/03/18 (Mark Rodgers)
sl@0
    43
        bool operator!=(const bound_object& other) const
sl@0
    44
        { return !(*this==other); }
sl@0
    45
        bool operator>(const bound_object& other) const
sl@0
    46
        { return !(*this < other); }
sl@0
    47
      };
sl@0
    48
sl@0
    49
      // Describes the connection between a signal and the objects that are
sl@0
    50
      // bound for a specific slot. Enables notification of the signal and the
sl@0
    51
      // slots when a disconnect is requested.
sl@0
    52
      struct basic_connection {
sl@0
    53
        void* signal;
sl@0
    54
        void* signal_data;
sl@0
    55
        void (*signal_disconnect)(void*, void*);
sl@0
    56
        bool blocked_;
sl@0
    57
sl@0
    58
        std::list<bound_object> bound_objects;
sl@0
    59
      };
sl@0
    60
    } // end namespace detail
sl@0
    61
sl@0
    62
    // The user may freely pass around the "connection" object and terminate
sl@0
    63
    // the connection at any time using disconnect().
sl@0
    64
    class BOOST_SIGNALS_DECL connection :
sl@0
    65
      private less_than_comparable1<connection>,
sl@0
    66
      private equality_comparable1<connection>
sl@0
    67
    {
sl@0
    68
    public:
sl@0
    69
      connection() : con(), controlling_connection(false) {}
sl@0
    70
      connection(const connection&);
sl@0
    71
      ~connection();
sl@0
    72
sl@0
    73
      // Block he connection: if the connection is still active, there
sl@0
    74
      // will be no notification
sl@0
    75
      void block(bool should_block = true) { con->blocked_ = should_block; }
sl@0
    76
      void unblock() { con->blocked_ = false; }
sl@0
    77
      bool blocked() const { return !connected() || con->blocked_; }
sl@0
    78
sl@0
    79
      // Disconnect the signal and slot, if they are connected
sl@0
    80
      void disconnect() const;
sl@0
    81
sl@0
    82
      // Returns true if the signal and slot are connected
sl@0
    83
      bool connected() const { return con.get() && con->signal_disconnect; }
sl@0
    84
sl@0
    85
      // Comparison of connections
sl@0
    86
      bool operator==(const connection& other) const;
sl@0
    87
      bool operator<(const connection& other) const;
sl@0
    88
sl@0
    89
      // Connection assignment
sl@0
    90
      connection& operator=(const connection& other) ;
sl@0
    91
sl@0
    92
      // Swap connections
sl@0
    93
      void swap(connection& other);
sl@0
    94
sl@0
    95
    public: // TBD: CHANGE THIS
sl@0
    96
      // Set whether this connection object is controlling or not
sl@0
    97
      void set_controlling(bool control = true)
sl@0
    98
      { controlling_connection = control; }
sl@0
    99
sl@0
   100
      shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection>
sl@0
   101
      get_connection() const
sl@0
   102
      { return con; }
sl@0
   103
sl@0
   104
    private:
sl@0
   105
      friend class detail::signal_base_impl;
sl@0
   106
      friend class detail::slot_base;
sl@0
   107
      friend class trackable;
sl@0
   108
sl@0
   109
      // Reset this connection to refer to a different actual connection
sl@0
   110
      void reset(BOOST_SIGNALS_NAMESPACE::detail::basic_connection*);
sl@0
   111
sl@0
   112
      // Add a bound object to this connection (not for users)
sl@0
   113
      void add_bound_object(const BOOST_SIGNALS_NAMESPACE::detail::bound_object& b);
sl@0
   114
sl@0
   115
      friend class BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor;
sl@0
   116
sl@0
   117
      // Pointer to the actual contents of the connection
sl@0
   118
      shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection> con;
sl@0
   119
sl@0
   120
      // True if the destruction of this connection object should disconnect
sl@0
   121
      bool controlling_connection;
sl@0
   122
    };
sl@0
   123
sl@0
   124
    // Similar to connection, but will disconnect the connection when it is
sl@0
   125
    // destroyed unless release() has been called.
sl@0
   126
    class BOOST_SIGNALS_DECL scoped_connection : public connection {
sl@0
   127
    public:
sl@0
   128
      scoped_connection() : connection(), released(false) {}
sl@0
   129
      scoped_connection(const connection&);
sl@0
   130
      scoped_connection(const scoped_connection&);
sl@0
   131
      ~scoped_connection();
sl@0
   132
sl@0
   133
      connection release();
sl@0
   134
sl@0
   135
      inline void swap(scoped_connection&);
sl@0
   136
sl@0
   137
      scoped_connection& operator=(const connection&);
sl@0
   138
      scoped_connection& operator=(const scoped_connection&);
sl@0
   139
sl@0
   140
    private:
sl@0
   141
      bool released;
sl@0
   142
    };
sl@0
   143
sl@0
   144
    namespace detail {
sl@0
   145
      struct connection_slot_pair {
sl@0
   146
        connection first;
sl@0
   147
        any second;
sl@0
   148
sl@0
   149
        connection_slot_pair() {}
sl@0
   150
sl@0
   151
        connection_slot_pair(const connection& c, const any& a)
sl@0
   152
          : first(c), second(a)
sl@0
   153
        {
sl@0
   154
        }
sl@0
   155
sl@0
   156
        // Dummys to allow explicit instantiation to work
sl@0
   157
        bool operator==(const connection_slot_pair&) const { return false; }
sl@0
   158
        bool operator<(const connection_slot_pair&) const { return false;}
sl@0
   159
      };
sl@0
   160
sl@0
   161
      // Determines if the underlying connection is disconnected
sl@0
   162
      struct is_disconnected {
sl@0
   163
        typedef connection_slot_pair argument_type;
sl@0
   164
        typedef bool result_type;
sl@0
   165
sl@0
   166
        inline bool operator()(const argument_type& c) const
sl@0
   167
        {
sl@0
   168
          return !c.first.connected();
sl@0
   169
        }
sl@0
   170
      };
sl@0
   171
sl@0
   172
      // Determines if the underlying connection is callable, ie if
sl@0
   173
      // it is connected and not blocked
sl@0
   174
      struct is_callable {
sl@0
   175
        typedef connection_slot_pair argument_type;
sl@0
   176
        typedef bool result_type;
sl@0
   177
sl@0
   178
        inline bool operator()(const argument_type& c) const
sl@0
   179
        {
sl@0
   180
          return c.first.connected() && !c.first.blocked() ;
sl@0
   181
        }
sl@0
   182
      };
sl@0
   183
sl@0
   184
      // Autodisconnects the bound object when it is destroyed unless the
sl@0
   185
      // release method is invoked.
sl@0
   186
      class auto_disconnect_bound_object {
sl@0
   187
      public:
sl@0
   188
        auto_disconnect_bound_object(const bound_object& b) :
sl@0
   189
          binding(b), auto_disconnect(true)
sl@0
   190
        {
sl@0
   191
        }
sl@0
   192
sl@0
   193
        ~auto_disconnect_bound_object()
sl@0
   194
        {
sl@0
   195
          if (auto_disconnect)
sl@0
   196
            binding.disconnect(binding.obj, binding.data);
sl@0
   197
        }
sl@0
   198
sl@0
   199
        void release() { auto_disconnect = false; }
sl@0
   200
sl@0
   201
      private:
sl@0
   202
        bound_object binding;
sl@0
   203
        bool auto_disconnect;
sl@0
   204
      };
sl@0
   205
    } // end namespace detail
sl@0
   206
  } // end namespace BOOST_SIGNALS_NAMESPACE
sl@0
   207
} // end namespace boost
sl@0
   208
sl@0
   209
#ifdef BOOST_HAS_ABI_HEADERS
sl@0
   210
#  include BOOST_ABI_SUFFIX
sl@0
   211
#endif
sl@0
   212
sl@0
   213
#endif // BOOST_SIGNALS_CONNECTION_HPP