]> git.lyx.org Git - lyx.git/blob - boost/boost/signals/connection.hpp
update to boost 1.32.0
[lyx.git] / boost / boost / signals / connection.hpp
1 // Boost.Signals library
2
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)
7
8 // For more information, see http://www.boost.org
9
10 #ifndef BOOST_SIGNALS_CONNECTION_HPP
11 #define BOOST_SIGNALS_CONNECTION_HPP
12
13 #include <boost/signals/detail/signals_common.hpp>
14 #include <boost/smart_ptr.hpp>
15 #include <boost/operators.hpp>
16 #include <boost/any.hpp>
17 #include <list>
18 #include <cassert>
19 #include <utility>
20
21 #ifdef BOOST_HAS_ABI_HEADERS
22 #  include BOOST_ABI_PREFIX
23 #endif
24
25 namespace boost {
26   namespace BOOST_SIGNALS_NAMESPACE {
27     class trackable;
28
29     namespace detail {
30       // Represents an object that has been bound as part of a slot, and how
31       // to notify that object of a disconnect
32       struct bound_object {
33         void* obj;
34         void* data;
35         void (*disconnect)(void*, void*);
36
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; }
41
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); }
47       };
48
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 {
53         void* signal;
54         void* signal_data;
55         void (*signal_disconnect)(void*, void*);
56
57         std::list<bound_object> bound_objects;
58       };
59     } // end namespace detail
60
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>
66     {
67     public:
68       connection() : con(), controlling_connection(false) {}
69       connection(const connection&);
70       ~connection();
71
72       // Disconnect the signal and slot, if they are connected
73       void disconnect() const;
74
75       // Returns true if the signal and slot are connected
76       bool connected() const { return con.get() && con->signal_disconnect; }
77
78       // Comparison of connections
79       bool operator==(const connection& other) const;
80       bool operator<(const connection& other) const;
81
82       // Connection assignment
83       connection& operator=(const connection& other) ;
84
85       // Swap connections
86       void swap(connection& other);
87
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; }
92
93       shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection>
94       get_connection() const 
95       { return con; }
96
97     private:
98       friend class detail::signal_base_impl;
99       friend class detail::slot_base;
100       friend class trackable;
101
102       // Reset this connection to refer to a different actual connection
103       void reset(BOOST_SIGNALS_NAMESPACE::detail::basic_connection*);
104
105       // Add a bound object to this connection (not for users)
106       void add_bound_object(const BOOST_SIGNALS_NAMESPACE::detail::bound_object& b);
107
108       friend class BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor;
109
110       // Pointer to the actual contents of the connection
111       shared_ptr<BOOST_SIGNALS_NAMESPACE::detail::basic_connection> con;
112
113       // True if the destruction of this connection object should disconnect
114       bool controlling_connection;
115     };
116
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 {
120     public:
121       scoped_connection() : connection(), released(false) {}
122       scoped_connection(const connection&);
123       scoped_connection(const scoped_connection&);
124       ~scoped_connection();
125
126       connection release();
127
128       inline void swap(scoped_connection&);
129
130       scoped_connection& operator=(const connection&);
131       scoped_connection& operator=(const scoped_connection&);
132
133     private:
134       bool released;
135     };
136
137     namespace detail {
138       struct connection_slot_pair {
139         connection first;
140         any second;
141
142         connection_slot_pair() {}
143
144         connection_slot_pair(const connection& c, const any& a)
145           : first(c), second(a)
146         {
147         }
148
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;}
152       };
153
154       // Determines if the underlying connection is disconnected
155       struct is_disconnected {
156         typedef connection_slot_pair argument_type;
157         typedef bool result_type;
158
159         inline bool operator()(const argument_type& c) const
160         {
161           return !c.first.connected();
162         }
163       };
164
165       // Autodisconnects the bound object when it is destroyed unless the
166       // release method is invoked.
167       class auto_disconnect_bound_object {
168       public:
169         auto_disconnect_bound_object(const bound_object& b) :
170           binding(b), auto_disconnect(true)
171         {
172         }
173
174         ~auto_disconnect_bound_object()
175         {
176           if (auto_disconnect)
177             binding.disconnect(binding.obj, binding.data);
178         }
179
180         void release() { auto_disconnect = false; }
181
182       private:
183         bound_object binding;
184         bool auto_disconnect;
185       };
186     } // end namespace detail
187   } // end namespace BOOST_SIGNALS_NAMESPACE
188 } // end namespace boost
189
190 #ifdef BOOST_HAS_ABI_HEADERS
191 #  include BOOST_ABI_SUFFIX
192 #endif
193
194 #endif // BOOST_SIGNALS_CONNECTION_HPP