1 // Boost.Signals library
3 // Copyright (C) 2001 Doug Gregor (gregod@cs.rpi.edu)
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.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
16 #include <boost/signals/detail/signal_base.hpp>
22 signal_base_impl::signal_base_impl(const compare_type& comp) :
26 flags.delayed_disconnect = false;
27 flags.clearing = false;
30 signal_base_impl::~signal_base_impl()
32 // Set the "clearing" flag to ignore extraneous disconnect requests,
33 // because all slots will be disconnected on destruction anyway.
34 flags.clearing = true;
37 void signal_base_impl::disconnect_all_slots()
39 // Do nothing if we're already clearing the slot list
43 if (call_depth == 0) {
44 // Clearing the slot list will disconnect all slots automatically
45 temporarily_set_clearing set_clearing(this);
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();
64 connect_slot(const any& slot,
66 const std::vector<const trackable*>& bound_objects)
68 // Allocate storage for a new basic_connection object to represent the
70 basic_connection* con = new basic_connection();
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);
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());
83 // Add the slot to the list.
86 slots.insert(stored_slot_type(name,
87 connection_slot_pair(slot_connection,
90 // Make the copy of the connection in the list disconnect when it is
92 pos->second.first.set_controlling();
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.
100 // Fill out the connection object appropriately. None of these
101 // operations can throw
103 con->signal_data = saved_iter.release();
104 con->signal_disconnect = &signal_base_impl::slot_disconnected;
106 // If an exception is thrown the connection will automatically be
108 scoped_connection safe_connection = slot_connection;
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();
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);
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);
127 // Add the binding to the list of bindings for the connection.
128 con->bound_objects.push_back(binding);
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();
136 // No exceptions will be thrown past this point, and we must not
137 // disconnect the connection now
138 safe_connection.release();
140 return slot_connection;
143 bool signal_base_impl::empty() const
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())
156 void signal_base_impl::disconnect(const any& group)
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;
164 group_slots.first->second.first.disconnect();
165 group_slots.first = next;
169 void signal_base_impl::slot_disconnected(void* obj, void* data)
171 signal_base_impl* self = reinterpret_cast<signal_base_impl*>(obj);
173 // We won't need the slot iterator after this
174 std::auto_ptr<slot_iterator> slot(
175 reinterpret_cast<slot_iterator*>(data));
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;
185 // Just remove the slot now, it's safe
186 self->slots.erase(*slot);
191 void signal_base_impl::remove_disconnected_slots() const
193 // Remove any disconnected slots
194 for (slot_iterator i = slots.begin(); i != slots.end(); /* none */) {
195 if (!i->second.first.connected())
203 call_notification(const shared_ptr<signal_base_impl>& b) :
206 // A call will be made, so increment the call depth as a notification
210 call_notification::~call_notification()
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;
223 signal_base::~signal_base()
226 } // namespace detail
227 } // namespace signals
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::signals::detail::connection_slot_pair,
235 boost::function2<bool, boost::any, boost::any> >;