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 #define BOOST_SIGNALS_SOURCE
12 #include <boost/signals/detail/signal_base.hpp>
16 namespace BOOST_SIGNALS_NAMESPACE {
18 signal_base_impl::signal_base_impl(const compare_type& comp,
24 flags.delayed_disconnect = false;
25 flags.clearing = false;
28 signal_base_impl::~signal_base_impl()
30 // Set the "clearing" flag to ignore extraneous disconnect requests,
31 // because all slots will be disconnected on destruction anyway.
32 flags.clearing = true;
35 void signal_base_impl::disconnect_all_slots()
37 // Do nothing if we're already clearing the slot list
41 if (call_depth == 0) {
42 // Clearing the slot list will disconnect all slots automatically
43 temporarily_set_clearing set_clearing(this);
47 // We can't actually remove elements from the slot list because there
48 // are still iterators into the slot list that must not be
49 // invalidated by this operation. So just disconnect each slot
50 // without removing it from the slot list. When the call depth does
51 // reach zero, the call list will be cleared.
52 flags.delayed_disconnect = true;
53 temporarily_set_clearing set_clearing(this);
54 for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
55 i->first.disconnect();
62 connect_slot(const any& slot_,
64 shared_ptr<slot_base::data_t> data,
67 // Transfer the burden of ownership to a local, scoped
69 data->watch_bound_objects.set_controlling(false);
70 scoped_connection safe_connection(data->watch_bound_objects);
72 // Allocate storage for an iterator that will hold the point of
73 // insertion of the slot into the list. This is used to later remove
74 // the slot when it is disconnected.
75 std::auto_ptr<iterator> saved_iter(new iterator);
77 // Add the slot to the list.
79 slots_.insert(name, data->watch_bound_objects, slot_, at);
81 // The assignment operation here absolutely must not throw, which
82 // intuitively makes sense (because any container's insert method
83 // becomes impossible to use in an exception-safe manner without this
84 // assumption), but doesn't appear to be mentioned in the standard.
87 // Fill out the connection object appropriately. None of these
88 // operations can throw
89 data->watch_bound_objects.get_connection()->signal = this;
90 data->watch_bound_objects.get_connection()->signal_data =
92 data->watch_bound_objects.get_connection()->signal_disconnect =
93 &signal_base_impl::slot_disconnected;
95 // Make the copy of the connection in the list disconnect when it is
96 // destroyed. The local, scoped connection is then released
97 // because ownership has been transferred.
98 pos->first.set_controlling();
99 return safe_connection.release();
102 bool signal_base_impl::empty() const
104 // Disconnected slots may still be in the list of slots if
105 // a) this is called while slots are being invoked (call_depth > 0)
106 // b) an exception was thrown in remove_disconnected_slots
107 for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
108 if (i->first.connected())
115 std::size_t signal_base_impl::num_slots() const
117 // Disconnected slots may still be in the list of slots if
118 // a) this is called while slots are being invoked (call_depth > 0)
119 // b) an exception was thrown in remove_disconnected_slots
120 std::size_t count = 0;
121 for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
122 if (i->first.connected())
128 void signal_base_impl::disconnect(const any& group)
129 { slots_.disconnect(group); }
131 void signal_base_impl::slot_disconnected(void* obj, void* data)
133 signal_base_impl* self = reinterpret_cast<signal_base_impl*>(obj);
135 // We won't need the slot iterator after this
136 std::auto_ptr<iterator> slot(reinterpret_cast<iterator*>(data));
138 // If we're flags.clearing, we don't bother updating the list of slots
139 if (!self->flags.clearing) {
140 // If we're in a call, note the fact that a slot has been deleted so
141 // we can come back later to remove the iterator
142 if (self->call_depth > 0) {
143 self->flags.delayed_disconnect = true;
146 // Just remove the slot now, it's safe
147 self->slots_.erase(*slot);
152 void signal_base_impl::remove_disconnected_slots() const
153 { slots_.remove_disconnected_slots(); }
156 call_notification(const shared_ptr<signal_base_impl>& b) :
159 // A call will be made, so increment the call depth as a notification
163 call_notification::~call_notification()
167 // If the call depth is zero and we have some slots that have been
168 // disconnected during the calls, remove those slots from the list
169 if (impl->call_depth == 0 &&
170 impl->flags.delayed_disconnect) {
171 impl->remove_disconnected_slots();
172 impl->flags.delayed_disconnect = false;
176 signal_base::signal_base(const compare_type& comp, const any& combiner)
179 impl.reset(new signal_base_impl(comp, combiner));
182 signal_base::~signal_base()
186 } // namespace detail
187 } // namespace BOOST_SIGNALS_NAMESPACE