]> git.lyx.org Git - lyx.git/blob - boost/libs/signals/src/signal_base.cpp
update boost
[lyx.git] / boost / libs / signals / src / signal_base.cpp
1 // Boost.Signals library
2 //
3 // Copyright (C) 2001 Doug Gregor (gregod@cs.rpi.edu)
4 //
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
10 //
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
13
14 // For more information, see http://www.boost.org
15
16 #include <boost/signals/detail/signal_base.hpp>
17 #include <cassert>
18
19 namespace boost {
20   namespace BOOST_SIGNALS_NAMESPACE {
21     namespace detail {
22       signal_base_impl::signal_base_impl(const compare_type& comp) :
23         call_depth(0),
24         slots_(comp)
25       {
26         flags.delayed_disconnect = false;
27         flags.clearing = false;
28       }
29
30       signal_base_impl::~signal_base_impl()
31       {
32         // Set the "clearing" flag to ignore extraneous disconnect requests,
33         // because all slots will be disconnected on destruction anyway.
34         flags.clearing = true;
35       }
36
37       void signal_base_impl::disconnect_all_slots()
38       {
39         // Do nothing if we're already clearing the slot list
40         if (flags.clearing)
41           return;
42
43         if (call_depth == 0) {
44           // Clearing the slot list will disconnect all slots automatically
45           temporarily_set_clearing set_clearing(this);
46           slots_.clear();
47         }
48         else {
49           // We can't actually remove elements from the slot list because there
50           // are still iterators into the slot list that must not be
51           // invalidated by this operation. So just disconnect each slot
52           // without removing it from the slot list. When the call depth does
53           // reach zero, the call list will be cleared.
54           flags.delayed_disconnect = true;
55           temporarily_set_clearing set_clearing(this);
56           for (slot_iterator i = slots_.begin(); i != slots_.end(); ++i) {
57             i->second.first.disconnect();
58           }
59         }
60       }
61
62       connection
63       signal_base_impl::
64         connect_slot(const any& slot,
65                      const any& name,
66                      const std::vector<const trackable*>& bound_objects)
67       {
68         // Allocate storage for a new basic_connection object to represent the
69         // connection
70         basic_connection* con = new basic_connection();
71
72         // Create a new connection handle object and place the basic_connection
73         // object we just created under its control. Note that the "reset"
74         // routine will delete con if allocation throws.
75         connection slot_connection;
76         slot_connection.reset(con);
77
78         // Allocate storage for an iterator that will hold the point of
79         // insertion of the slot into the list. This is used to later remove
80         // the slot when it is disconnected.
81         std::auto_ptr<slot_iterator> saved_iter(new slot_iterator());
82
83         // Add the slot to the list.
84
85         slot_iterator pos =
86           slots_.insert(stored_slot_type(name,
87                                         connection_slot_pair(slot_connection,
88                                                              slot)));
89
90         // Make the copy of the connection in the list disconnect when it is
91         // destroyed
92         pos->second.first.set_controlling();
93
94         // The assignment operation here absolutely must not throw, which
95         // intuitively makes sense (because any container's insert method
96         // becomes impossible to use in an exception-safe manner without this
97         // assumption), but doesn't appear to be mentioned in the standard.
98         *saved_iter = pos;
99
100         // Fill out the connection object appropriately. None of these
101         // operations can throw
102         con->signal = this;
103         con->signal_data = saved_iter.release();
104         con->signal_disconnect = &signal_base_impl::slot_disconnected;
105
106         // If an exception is thrown the connection will automatically be
107         // disconnected.
108         scoped_connection safe_connection = slot_connection;
109
110         // Connect each of the bound objects
111         for(std::vector<const trackable*>::const_iterator i =
112               bound_objects.begin();
113             i != bound_objects.end();
114             ++i) {
115           // Notify the object that the signal is connecting to it by passing
116           // it a copy of the connection. If the connection
117           // should throw, the scoped connection safe_connection will
118           // disconnect the connection completely.
119           bound_object binding;
120           (*i)->signal_connected(slot_connection, binding);
121
122           // This will notify the bound object that the connection just made
123           // should be disconnected if an exception is thrown before the
124           // end of this iteration
125           auto_disconnect_bound_object disconnector(binding);
126
127           // Add the binding to the list of bindings for the connection.
128           con->bound_objects.push_back(binding);
129
130           // The connection object now knows about the bound object, so if an
131           // exception is thrown later the connection object will notify the
132           // bound object of the disconnection automatically
133           disconnector.release();
134         }
135
136         // No exceptions will be thrown past this point, and we must not
137         // disconnect the connection now
138         safe_connection.release();
139
140         return slot_connection;
141       }
142
143       bool signal_base_impl::empty() const
144       {
145         // Disconnected slots may still be in the list of slots if
146         //   a) this is called while slots are being invoked (call_depth > 0)
147         //   b) an exception was thrown in remove_disconnected_slots
148         for (slot_iterator i = slots_.begin(); i != slots_.end(); ++i) {
149           if (i->second.first.connected())
150             return false;
151         }
152
153         return true;
154       }
155
156       void signal_base_impl::disconnect(const any& group)
157       {
158         std::pair<slot_iterator, slot_iterator> group_slots =
159           slots_.equal_range(group);
160         while (group_slots.first != group_slots.second) {
161           slot_iterator next = group_slots.first;
162           ++next;
163
164           group_slots.first->second.first.disconnect();
165           group_slots.first = next;
166         }
167       }
168
169       void signal_base_impl::slot_disconnected(void* obj, void* data)
170       {
171         signal_base_impl* self = reinterpret_cast<signal_base_impl*>(obj);
172
173         // We won't need the slot iterator after this
174         std::auto_ptr<slot_iterator> slot(
175                                       reinterpret_cast<slot_iterator*>(data));
176
177         // If we're flags.clearing, we don't bother updating the list of slots
178         if (!self->flags.clearing) {
179           // If we're in a call, note the fact that a slot has been deleted so
180           // we can come back later to remove the iterator
181           if (self->call_depth > 0) {
182             self->flags.delayed_disconnect = true;
183           }
184           else {
185             // Just remove the slot now, it's safe
186             self->slots_.erase(*slot);
187           }
188         }
189       }
190
191       void signal_base_impl::remove_disconnected_slots() const
192       {
193         // Remove any disconnected slots
194         for (slot_iterator i = slots_.begin(); i != slots_.end(); /* none */) {
195           if (!i->second.first.connected())
196             slots_.erase(i++);
197           else
198             ++i;
199         }
200       }
201
202       call_notification::
203         call_notification(const shared_ptr<signal_base_impl>& b) :
204           impl(b)
205       {
206         // A call will be made, so increment the call depth as a notification
207         impl->call_depth++;
208       }
209
210       call_notification::~call_notification()
211       {
212         impl->call_depth--;
213
214         // If the call depth is zero and we have some slots that have been
215         // disconnected during the calls, remove those slots from the list
216         if (impl->call_depth == 0 &&
217             impl->flags.delayed_disconnect) {
218           impl->remove_disconnected_slots();
219           impl->flags.delayed_disconnect = false;
220         }
221       }
222
223       signal_base::~signal_base()
224       {
225       }
226     } // namespace detail
227   } // namespace BOOST_SIGNALS_NAMESPACE
228 } // namespace boost
229
230 #ifndef BOOST_MSVC
231 // Explicit instantiations to keep in the library
232 template class boost::function2<bool, boost::any, boost::any>;
233 template class std::multimap<boost::any,
234                              boost::BOOST_SIGNALS_NAMESPACE::detail::connection_slot_pair,
235                              boost::function2<bool, boost::any, boost::any> >;
236 #endif