1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* Copyright 2002, The libsigc++ Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <sigc++/trackable.h>
namespace sigc
{
trackable::trackable() noexcept
: callback_list_(nullptr)
{}
/* Don't copy the notification list.
The objects watching src don't need to be notified when the new object dies. */
trackable::trackable(const trackable& /*src*/) noexcept
: callback_list_(nullptr)
{}
// Don't move the notification list.
// The objects watching src don't need to be notified when the new object dies.
// They need to be notified now, because src probably becomes useless.
//
// If trackable's move constructor is modified, check if Glib::Object's
// move constructor should be modified similarly.
trackable::trackable(trackable&& src)
: callback_list_(nullptr)
{
src.notify_callbacks();
}
trackable& trackable::operator=(const trackable& src)
{
if(this != &src)
notify_callbacks(); //Make sure that we have finished with existing stuff before replacing it.
return *this;
}
trackable& trackable::operator=(trackable&& src)
{
if(this != &src)
{
notify_callbacks(); //Make sure that we have finished with existing stuff before replacing it.
src.notify_callbacks(); // src probably becomes useless.
}
return *this;
}
trackable::~trackable()
{
notify_callbacks();
}
void trackable::add_destroy_notify_callback(void* data, func_destroy_notify func) const
{
callback_list()->add_callback(data, func);
}
void trackable::remove_destroy_notify_callback(void* data) const
{
callback_list()->remove_callback(data);
}
void trackable::notify_callbacks()
{
if (callback_list_)
delete callback_list_; //This invokes all of the callbacks.
callback_list_ = nullptr;
}
internal::trackable_callback_list* trackable::callback_list() const
{
if (!callback_list_)
callback_list_ = new internal::trackable_callback_list;
return callback_list_;
}
namespace internal
{
trackable_callback_list::~trackable_callback_list()
{
clearing_ = true;
for (auto& callback : callbacks_)
if (callback.func_)
callback.func_(callback.data_);
}
void trackable_callback_list::add_callback(void* data, func_destroy_notify func)
{
if (!clearing_) // TODO: Is it okay to silently ignore attempts to add dependencies when the list is being cleared?
// I'd consider this a serious application bug, since the app is likely to segfault.
// But then, how should we handle it? Throw an exception? Martin.
callbacks_.push_back(trackable_callback(data, func));
}
void trackable_callback_list::clear()
{
clearing_ = true;
for (auto& callback : callbacks_)
if (callback.func_)
callback.func_(callback.data_);
callbacks_.clear();
clearing_ = false;
}
void trackable_callback_list::remove_callback(void* data)
{
for (callback_list::iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
{
auto& callback = *i;
if (callback.data_ == data && callback.func_ != nullptr)
{
//Don't remove a list element while the list is being cleared.
//It could invalidate the iterator in ~trackable_callback_list() or clear().
//But it may be necessary to invalidate the callback. See bug 589202.
if (clearing_)
callback.func_ = nullptr;
else
callbacks_.erase(i);
return;
}
}
}
} /* namespace internal */
} /* namespace sigc */
|