1 // Boost.Signals library
3 // Copyright Douglas Gregor 2001-2004. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org
10 #ifndef BOOST_SIGNALS_CONNECTION_HPP
11 #define BOOST_SIGNALS_CONNECTION_HPP
13 #include <boost/signals/detail/signals_common.hpp>
14 #include <boost/smart_ptr.hpp>
15 #include <boost/operators.hpp>
16 #include <boost/any.hpp>
21 #ifdef BOOST_HAS_ABI_HEADERS
22 # include BOOST_ABI_PREFIX
26 namespace BOOST_SIGNALS_NAMESPACE {
30 // Represents an object that has been bound as part of a slot, and how
31 // to notify that object of a disconnect
35 void (*disconnect)(void*, void*);
37 bool operator==(const bound_object& other) const
38 { return obj == other.obj && data == other.data; }
39 bool operator<(const bound_object& other) const
40 { return obj < other.obj; }
42 // To support intel 80 compiler, 2004/03/18 (Mark Rodgers)
43 bool operator!=(const bound_object& other) const
44 { return !(*this==other); }
45 bool operator>(const bound_object& other) const
46 { return !(*this < other); }
49 // Describes the connection between a signal and the objects that are
50 // bound for a specific slot. Enables notification of the signal and the
51 // slots when a disconnect is requested.
52 struct basic_connection {
55 void (*signal_disconnect)(void*, void*);
57 std::list<bound_object> bound_objects;
59 } // end namespace detail
61 // The user may freely pass around the "connection" object and terminate
62 // the connection at any time using disconnect().
63 class BOOST_SIGNALS_DECL connection :
64 private less_than_comparable1<connection>,
65 private equality_comparable1<connection>
68 connection() : con(), controlling_connection(false) {}
69 connection(const connection&);
72 // Disconnect the signal and slot, if they are connected
73 void disconnect() const;
75 // Returns true if the signal and slot are connected
76 bool connected() const { return con.get() && con->signal_disconnect; }
78 // Comparison of connections
79 bool operator==(const connection& other) const;
80 bool operator<(const connection& other) const;
82 // Connection assignment
83 connection& operator=(const connection& other) ;
86 void swap(connection& other);
88 public: // TBD: CHANGE THIS
89 // Set whether this connection object is controlling or not
90 void set_controlling(bool control = true)
91 { controlling_connection = control; }
93 shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection>
94 get_connection() const
98 friend class detail::signal_base_impl;
99 friend class detail::slot_base;
100 friend class trackable;
102 // Reset this connection to refer to a different actual connection
103 void reset(BOOST_SIGNALS_NAMESPACE::detail::basic_connection*);
105 // Add a bound object to this connection (not for users)
106 void add_bound_object(const BOOST_SIGNALS_NAMESPACE::detail::bound_object& b);
108 friend class BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor;
110 // Pointer to the actual contents of the connection
111 shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection> con;
113 // True if the destruction of this connection object should disconnect
114 bool controlling_connection;
117 // Similar to connection, but will disconnect the connection when it is
118 // destroyed unless release() has been called.
119 class BOOST_SIGNALS_DECL scoped_connection : public connection {
121 scoped_connection() : connection(), released(false) {}
122 scoped_connection(const connection&);
123 scoped_connection(const scoped_connection&);
124 ~scoped_connection();
126 connection release();
128 inline void swap(scoped_connection&);
130 scoped_connection& operator=(const connection&);
131 scoped_connection& operator=(const scoped_connection&);
138 struct connection_slot_pair {
142 connection_slot_pair() {}
144 connection_slot_pair(const connection& c, const any& a)
145 : first(c), second(a)
149 // Dummys to allow explicit instantiation to work
150 bool operator==(const connection_slot_pair&) const { return false; }
151 bool operator<(const connection_slot_pair&) const { return false;}
154 // Determines if the underlying connection is disconnected
155 struct is_disconnected {
156 typedef connection_slot_pair argument_type;
157 typedef bool result_type;
159 inline bool operator()(const argument_type& c) const
161 return !c.first.connected();
165 // Autodisconnects the bound object when it is destroyed unless the
166 // release method is invoked.
167 class auto_disconnect_bound_object {
169 auto_disconnect_bound_object(const bound_object& b) :
170 binding(b), auto_disconnect(true)
174 ~auto_disconnect_bound_object()
177 binding.disconnect(binding.obj, binding.data);
180 void release() { auto_disconnect = false; }
183 bound_object binding;
184 bool auto_disconnect;
186 } // end namespace detail
187 } // end namespace BOOST_SIGNALS_NAMESPACE
188 } // end namespace boost
190 #ifdef BOOST_HAS_ABI_HEADERS
191 # include BOOST_ABI_SUFFIX
194 #endif // BOOST_SIGNALS_CONNECTION_HPP