]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/signals/src/signal_base.cpp
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_
,
63 const stored_group
& name
,
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.
76 #if defined(BOOST_NO_CXX11_SMART_PTR)
78 std::auto_ptr
<iterator
> saved_iter(new iterator
);
82 std::unique_ptr
<iterator
> saved_iter(new iterator
);
86 // Add the slot to the list.
88 slots_
.insert(name
, data
->watch_bound_objects
, slot_
, at
);
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.
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
;
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();
111 bool signal_base_impl::empty() const
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())
124 std::size_t signal_base_impl::num_slots() const
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())
137 void signal_base_impl::disconnect(const stored_group
& group
)
138 { slots_
.disconnect(group
); }
140 void signal_base_impl::slot_disconnected(void* obj
, void* data
)
142 signal_base_impl
* self
= reinterpret_cast<signal_base_impl
*>(obj
);
144 // We won't need the slot iterator after this
146 #if defined(BOOST_NO_CXX11_SMART_PTR)
148 std::auto_ptr
<iterator
> slot(reinterpret_cast<iterator
*>(data
));
152 std::unique_ptr
<iterator
> slot(reinterpret_cast<iterator
*>(data
));
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;
164 // Just remove the slot now, it's safe
165 self
->slots_
.erase(*slot
);
170 void signal_base_impl::remove_disconnected_slots() const
171 { slots_
.remove_disconnected_slots(); }
174 call_notification(const shared_ptr
<signal_base_impl
>& b
) :
177 // A call will be made, so increment the call depth as a notification
181 call_notification::~call_notification()
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;
194 signal_base::signal_base(const compare_type
& comp
, const any
& combiner
)
197 impl
.reset(new signal_base_impl(comp
, combiner
));
200 signal_base::~signal_base()
204 } // namespace detail
205 } // namespace BOOST_SIGNALS_NAMESPACE