2002-05-26 13:02:17 +00:00
|
|
|
// Boost.Signals library
|
2004-02-05 09:14:22 +00:00
|
|
|
|
2004-11-20 09:08:45 +00:00
|
|
|
// Copyright Douglas Gregor 2001-2004. Use, modification and
|
2004-02-05 09:14:22 +00:00
|
|
|
// distribution is subject to the Boost Software License, Version
|
|
|
|
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
2002-05-26 13:02:17 +00:00
|
|
|
// For more information, see http://www.boost.org
|
|
|
|
|
2004-02-05 09:14:22 +00:00
|
|
|
#define BOOST_SIGNALS_SOURCE
|
2002-08-20 21:50:08 +00:00
|
|
|
|
2002-05-26 13:02:17 +00:00
|
|
|
#include <boost/signals/detail/signal_base.hpp>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace boost {
|
2002-06-18 15:39:27 +00:00
|
|
|
namespace BOOST_SIGNALS_NAMESPACE {
|
2002-05-26 13:02:17 +00:00
|
|
|
namespace detail {
|
2004-11-20 09:08:45 +00:00
|
|
|
signal_base_impl::signal_base_impl(const compare_type& comp,
|
|
|
|
const any& combiner)
|
|
|
|
: call_depth(0),
|
|
|
|
slots_(comp),
|
|
|
|
combiner_(combiner)
|
2002-05-26 13:02:17 +00:00
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
flags.delayed_disconnect = false;
|
|
|
|
flags.clearing = false;
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signal_base_impl::~signal_base_impl()
|
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
// Set the "clearing" flag to ignore extraneous disconnect requests,
|
|
|
|
// because all slots will be disconnected on destruction anyway.
|
|
|
|
flags.clearing = true;
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void signal_base_impl::disconnect_all_slots()
|
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
// Do nothing if we're already clearing the slot list
|
|
|
|
if (flags.clearing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (call_depth == 0) {
|
|
|
|
// Clearing the slot list will disconnect all slots automatically
|
|
|
|
temporarily_set_clearing set_clearing(this);
|
|
|
|
slots_.clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We can't actually remove elements from the slot list because there
|
|
|
|
// are still iterators into the slot list that must not be
|
|
|
|
// invalidated by this operation. So just disconnect each slot
|
|
|
|
// without removing it from the slot list. When the call depth does
|
|
|
|
// reach zero, the call list will be cleared.
|
|
|
|
flags.delayed_disconnect = true;
|
|
|
|
temporarily_set_clearing set_clearing(this);
|
2004-11-20 09:08:45 +00:00
|
|
|
for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
|
|
|
|
i->first.disconnect();
|
2002-08-20 21:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 09:14:22 +00:00
|
|
|
connection
|
2002-05-26 13:02:17 +00:00
|
|
|
signal_base_impl::
|
2004-11-20 09:08:45 +00:00
|
|
|
connect_slot(const any& slot_,
|
2006-03-05 18:22:35 +00:00
|
|
|
const stored_group& name,
|
2004-11-20 09:08:45 +00:00
|
|
|
shared_ptr<slot_base::data_t> data,
|
|
|
|
connect_position at)
|
2002-05-26 13:02:17 +00:00
|
|
|
{
|
2004-11-20 09:08:45 +00:00
|
|
|
// Transfer the burden of ownership to a local, scoped
|
|
|
|
// connection.
|
|
|
|
data->watch_bound_objects.set_controlling(false);
|
|
|
|
scoped_connection safe_connection(data->watch_bound_objects);
|
2002-08-20 21:50:08 +00:00
|
|
|
|
|
|
|
// Allocate storage for an iterator that will hold the point of
|
|
|
|
// insertion of the slot into the list. This is used to later remove
|
|
|
|
// the slot when it is disconnected.
|
2004-11-20 09:08:45 +00:00
|
|
|
std::auto_ptr<iterator> saved_iter(new iterator);
|
2002-08-20 21:50:08 +00:00
|
|
|
|
2004-02-05 09:14:22 +00:00
|
|
|
// Add the slot to the list.
|
2006-03-05 18:22:35 +00:00
|
|
|
iterator pos =
|
2004-11-20 09:08:45 +00:00
|
|
|
slots_.insert(name, data->watch_bound_objects, slot_, at);
|
2002-08-20 21:50:08 +00:00
|
|
|
|
2004-02-05 09:14:22 +00:00
|
|
|
// The assignment operation here absolutely must not throw, which
|
|
|
|
// intuitively makes sense (because any container's insert method
|
2002-08-20 21:50:08 +00:00
|
|
|
// becomes impossible to use in an exception-safe manner without this
|
|
|
|
// assumption), but doesn't appear to be mentioned in the standard.
|
|
|
|
*saved_iter = pos;
|
2004-02-05 09:14:22 +00:00
|
|
|
|
2002-08-20 21:50:08 +00:00
|
|
|
// Fill out the connection object appropriately. None of these
|
|
|
|
// operations can throw
|
2004-11-20 09:08:45 +00:00
|
|
|
data->watch_bound_objects.get_connection()->signal = this;
|
2006-03-05 18:22:35 +00:00
|
|
|
data->watch_bound_objects.get_connection()->signal_data =
|
2004-11-20 09:08:45 +00:00
|
|
|
saved_iter.release();
|
2006-03-05 18:22:35 +00:00
|
|
|
data->watch_bound_objects.get_connection()->signal_disconnect =
|
2004-11-20 09:08:45 +00:00
|
|
|
&signal_base_impl::slot_disconnected;
|
2002-08-20 21:50:08 +00:00
|
|
|
|
2004-11-20 09:08:45 +00:00
|
|
|
// Make the copy of the connection in the list disconnect when it is
|
|
|
|
// destroyed. The local, scoped connection is then released
|
|
|
|
// because ownership has been transferred.
|
|
|
|
pos->first.set_controlling();
|
|
|
|
return safe_connection.release();
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool signal_base_impl::empty() const
|
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
// Disconnected slots may still be in the list of slots if
|
|
|
|
// a) this is called while slots are being invoked (call_depth > 0)
|
|
|
|
// b) an exception was thrown in remove_disconnected_slots
|
2004-11-20 09:08:45 +00:00
|
|
|
for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
|
|
|
|
if (i->first.connected())
|
2002-08-20 21:50:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 09:14:22 +00:00
|
|
|
std::size_t signal_base_impl::num_slots() const
|
|
|
|
{
|
|
|
|
// Disconnected slots may still be in the list of slots if
|
|
|
|
// a) this is called while slots are being invoked (call_depth > 0)
|
|
|
|
// b) an exception was thrown in remove_disconnected_slots
|
|
|
|
std::size_t count = 0;
|
2004-11-20 09:08:45 +00:00
|
|
|
for (iterator i = slots_.begin(); i != slots_.end(); ++i) {
|
|
|
|
if (i->first.connected())
|
2004-02-05 09:14:22 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-03-05 18:22:35 +00:00
|
|
|
void signal_base_impl::disconnect(const stored_group& group)
|
2004-11-20 09:08:45 +00:00
|
|
|
{ slots_.disconnect(group); }
|
2002-05-26 13:02:17 +00:00
|
|
|
|
|
|
|
void signal_base_impl::slot_disconnected(void* obj, void* data)
|
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
signal_base_impl* self = reinterpret_cast<signal_base_impl*>(obj);
|
|
|
|
|
|
|
|
// We won't need the slot iterator after this
|
2004-11-20 09:08:45 +00:00
|
|
|
std::auto_ptr<iterator> slot(reinterpret_cast<iterator*>(data));
|
2004-02-05 09:14:22 +00:00
|
|
|
|
2002-08-20 21:50:08 +00:00
|
|
|
// If we're flags.clearing, we don't bother updating the list of slots
|
|
|
|
if (!self->flags.clearing) {
|
|
|
|
// If we're in a call, note the fact that a slot has been deleted so
|
|
|
|
// we can come back later to remove the iterator
|
|
|
|
if (self->call_depth > 0) {
|
|
|
|
self->flags.delayed_disconnect = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Just remove the slot now, it's safe
|
|
|
|
self->slots_.erase(*slot);
|
|
|
|
}
|
|
|
|
}
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void signal_base_impl::remove_disconnected_slots() const
|
2004-11-20 09:08:45 +00:00
|
|
|
{ slots_.remove_disconnected_slots(); }
|
2002-05-26 13:02:17 +00:00
|
|
|
|
|
|
|
call_notification::
|
2002-08-20 21:50:08 +00:00
|
|
|
call_notification(const shared_ptr<signal_base_impl>& b) :
|
|
|
|
impl(b)
|
2002-05-26 13:02:17 +00:00
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
// A call will be made, so increment the call depth as a notification
|
|
|
|
impl->call_depth++;
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
call_notification::~call_notification()
|
|
|
|
{
|
2002-08-20 21:50:08 +00:00
|
|
|
impl->call_depth--;
|
|
|
|
|
|
|
|
// If the call depth is zero and we have some slots that have been
|
|
|
|
// disconnected during the calls, remove those slots from the list
|
2004-02-05 09:14:22 +00:00
|
|
|
if (impl->call_depth == 0 &&
|
2002-08-20 21:50:08 +00:00
|
|
|
impl->flags.delayed_disconnect) {
|
|
|
|
impl->remove_disconnected_slots();
|
|
|
|
impl->flags.delayed_disconnect = false;
|
|
|
|
}
|
2002-05-26 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2004-11-20 09:08:45 +00:00
|
|
|
signal_base::signal_base(const compare_type& comp, const any& combiner)
|
|
|
|
: impl()
|
2004-02-05 09:14:22 +00:00
|
|
|
{
|
2004-11-20 09:08:45 +00:00
|
|
|
impl.reset(new signal_base_impl(comp, combiner));
|
2004-02-05 09:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signal_base::~signal_base()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-05-26 13:02:17 +00:00
|
|
|
} // namespace detail
|
2002-06-18 15:39:27 +00:00
|
|
|
} // namespace BOOST_SIGNALS_NAMESPACE
|
2002-05-26 13:02:17 +00:00
|
|
|
} // namespace boost
|
|
|
|
|