First public contribution.
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*);
58 std::list<bound_object> bound_objects;
60 } // end namespace detail
62 // The user may freely pass around the "connection" object and terminate
63 // the connection at any time using disconnect().
64 class BOOST_SIGNALS_DECL connection :
65 private less_than_comparable1<connection>,
66 private equality_comparable1<connection>
69 connection() : con(), controlling_connection(false) {}
70 connection(const connection&);
73 // Block he connection: if the connection is still active, there
74 // will be no notification
75 void block(bool should_block = true) { con->blocked_ = should_block; }
76 void unblock() { con->blocked_ = false; }
77 bool blocked() const { return !connected() || con->blocked_; }
79 // Disconnect the signal and slot, if they are connected
80 void disconnect() const;
82 // Returns true if the signal and slot are connected
83 bool connected() const { return con.get() && con->signal_disconnect; }
85 // Comparison of connections
86 bool operator==(const connection& other) const;
87 bool operator<(const connection& other) const;
89 // Connection assignment
90 connection& operator=(const connection& other) ;
93 void swap(connection& other);
95 public: // TBD: CHANGE THIS
96 // Set whether this connection object is controlling or not
97 void set_controlling(bool control = true)
98 { controlling_connection = control; }
100 shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection>
101 get_connection() const
105 friend class detail::signal_base_impl;
106 friend class detail::slot_base;
107 friend class trackable;
109 // Reset this connection to refer to a different actual connection
110 void reset(BOOST_SIGNALS_NAMESPACE::detail::basic_connection*);
112 // Add a bound object to this connection (not for users)
113 void add_bound_object(const BOOST_SIGNALS_NAMESPACE::detail::bound_object& b);
115 friend class BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor;
117 // Pointer to the actual contents of the connection
118 shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection> con;
120 // True if the destruction of this connection object should disconnect
121 bool controlling_connection;
124 // Similar to connection, but will disconnect the connection when it is
125 // destroyed unless release() has been called.
126 class BOOST_SIGNALS_DECL scoped_connection : public connection {
128 scoped_connection() : connection(), released(false) {}
129 scoped_connection(const connection&);
130 scoped_connection(const scoped_connection&);
131 ~scoped_connection();
133 connection release();
135 inline void swap(scoped_connection&);
137 scoped_connection& operator=(const connection&);
138 scoped_connection& operator=(const scoped_connection&);
145 struct connection_slot_pair {
149 connection_slot_pair() {}
151 connection_slot_pair(const connection& c, const any& a)
152 : first(c), second(a)
156 // Dummys to allow explicit instantiation to work
157 bool operator==(const connection_slot_pair&) const { return false; }
158 bool operator<(const connection_slot_pair&) const { return false;}
161 // Determines if the underlying connection is disconnected
162 struct is_disconnected {
163 typedef connection_slot_pair argument_type;
164 typedef bool result_type;
166 inline bool operator()(const argument_type& c) const
168 return !c.first.connected();
172 // Determines if the underlying connection is callable, ie if
173 // it is connected and not blocked
175 typedef connection_slot_pair argument_type;
176 typedef bool result_type;
178 inline bool operator()(const argument_type& c) const
180 return c.first.connected() && !c.first.blocked() ;
184 // Autodisconnects the bound object when it is destroyed unless the
185 // release method is invoked.
186 class auto_disconnect_bound_object {
188 auto_disconnect_bound_object(const bound_object& b) :
189 binding(b), auto_disconnect(true)
193 ~auto_disconnect_bound_object()
196 binding.disconnect(binding.obj, binding.data);
199 void release() { auto_disconnect = false; }
202 bound_object binding;
203 bool auto_disconnect;
205 } // end namespace detail
206 } // end namespace BOOST_SIGNALS_NAMESPACE
207 } // end namespace boost
209 #ifdef BOOST_HAS_ABI_HEADERS
210 # include BOOST_ABI_SUFFIX
213 #endif // BOOST_SIGNALS_CONNECTION_HPP