]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals/src/signal_base.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / signals / src / signal_base.cpp
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 #define BOOST_SIGNALS_SOURCE
11
12 #include <boost/signals/detail/signal_base.hpp>
13 #include <cassert>
14
15 namespace boost {
16 namespace BOOST_SIGNALS_NAMESPACE {
17 namespace detail {
18 signal_base_impl::signal_base_impl(const compare_type& comp,
19 const any& combiner)
20 : call_depth(0),
21 slots_(comp),
22 combiner_(combiner)
23 {
24 flags.delayed_disconnect = false;
25 flags.clearing = false;
26 }
27
28 signal_base_impl::~signal_base_impl()
29 {
30 // Set the "clearing" flag to ignore extraneous disconnect requests,
31 // because all slots will be disconnected on destruction anyway.
32 flags.clearing = true;
33 }
34
35 void signal_base_impl::disconnect_all_slots()
36 {
37 // Do nothing if we're already clearing the slot list
38 if (flags.clearing)
39 return;
40
41 if (call_depth == 0) {
42 // Clearing the slot list will disconnect all slots automatically
43 temporarily_set_clearing set_clearing(this);
44 slots_.clear();
45 }
46 else {
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();
56 }
57 }
58 }
59
60 connection
61 signal_base_impl::
62 connect_slot(const any& slot_,
63 const stored_group& name,
64 shared_ptr<slot_base::data_t> data,
65 connect_position at)
66 {
67 // Transfer the burden of ownership to a local, scoped
68 // connection.
69 data->watch_bound_objects.set_controlling(false);
70 scoped_connection safe_connection(data->watch_bound_objects);
71
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
76 #if defined(BOOST_NO_CXX11_SMART_PTR)
77
78 std::auto_ptr<iterator> saved_iter(new iterator);
79
80 #else
81
82 std::unique_ptr<iterator> saved_iter(new iterator);
83
84 #endif
85
86 // Add the slot to the list.
87 iterator pos =
88 slots_.insert(name, data->watch_bound_objects, slot_, at);
89
90 // The assignment operation here absolutely must not throw, which
91 // intuitively makes sense (because any container's insert method
92 // becomes impossible to use in an exception-safe manner without this
93 // assumption), but doesn't appear to be mentioned in the standard.
94 *saved_iter = pos;
95
96 // Fill out the connection object appropriately. None of these
97 // operations can throw
98 data->watch_bound_objects.get_connection()->signal = this;
99 data->watch_bound_objects.get_connection()->signal_data =
100 saved_iter.release();
101 data->watch_bound_objects.get_connection()->signal_disconnect =
102 &signal_base_impl::slot_disconnected;
103
104 // Make the copy of the connection in the list disconnect when it is
105 // destroyed. The local, scoped connection is then released
106 // because ownership has been transferred.
107 pos->first.set_controlling();
108 return safe_connection.release();
109 }
110
111 bool signal_base_impl::empty() const
112 {
113 // Disconnected slots may still be in the list of slots if
114 // a) this is called while slots are being invoked (call_depth > 0)
115 // b) an exception was thrown in remove_disconnected_slots
116 for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
117 if (i->first.connected())
118 return false;
119 }
120
121 return true;
122 }
123
124 std::size_t signal_base_impl::num_slots() const
125 {
126 // Disconnected slots may still be in the list of slots if
127 // a) this is called while slots are being invoked (call_depth > 0)
128 // b) an exception was thrown in remove_disconnected_slots
129 std::size_t count = 0;
130 for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
131 if (i->first.connected())
132 ++count;
133 }
134 return count;
135 }
136
137 void signal_base_impl::disconnect(const stored_group& group)
138 { slots_.disconnect(group); }
139
140 void signal_base_impl::slot_disconnected(void* obj, void* data)
141 {
142 signal_base_impl* self = reinterpret_cast<signal_base_impl*>(obj);
143
144 // We won't need the slot iterator after this
145
146 #if defined(BOOST_NO_CXX11_SMART_PTR)
147
148 std::auto_ptr<iterator> slot(reinterpret_cast<iterator*>(data));
149
150 #else
151
152 std::unique_ptr<iterator> slot(reinterpret_cast<iterator*>(data));
153
154 #endif
155
156 // If we're flags.clearing, we don't bother updating the list of slots
157 if (!self->flags.clearing) {
158 // If we're in a call, note the fact that a slot has been deleted so
159 // we can come back later to remove the iterator
160 if (self->call_depth > 0) {
161 self->flags.delayed_disconnect = true;
162 }
163 else {
164 // Just remove the slot now, it's safe
165 self->slots_.erase(*slot);
166 }
167 }
168 }
169
170 void signal_base_impl::remove_disconnected_slots() const
171 { slots_.remove_disconnected_slots(); }
172
173 call_notification::
174 call_notification(const shared_ptr<signal_base_impl>& b) :
175 impl(b)
176 {
177 // A call will be made, so increment the call depth as a notification
178 impl->call_depth++;
179 }
180
181 call_notification::~call_notification()
182 {
183 impl->call_depth--;
184
185 // If the call depth is zero and we have some slots that have been
186 // disconnected during the calls, remove those slots from the list
187 if (impl->call_depth == 0 &&
188 impl->flags.delayed_disconnect) {
189 impl->remove_disconnected_slots();
190 impl->flags.delayed_disconnect = false;
191 }
192 }
193
194 signal_base::signal_base(const compare_type& comp, const any& combiner)
195 : impl()
196 {
197 impl.reset(new signal_base_impl(comp, combiner));
198 }
199
200 signal_base::~signal_base()
201 {
202 }
203
204 } // namespace detail
205 } // namespace BOOST_SIGNALS_NAMESPACE
206 } // namespace boost
207