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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
* Copyright 2003, 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++/functors/slot_base.h>
namespace
{
// Used by slot_rep::notify() and slot_base::operator=(). They must be
// notified, if the slot_rep is deleted when they call disconnect().
struct destroy_notify_struct
{
destroy_notify_struct() noexcept : deleted_(false) { }
static void* notify(void* data) noexcept
{
auto self_ = reinterpret_cast<destroy_notify_struct*>(data);
self_->deleted_ = true;
return nullptr;
}
bool deleted_;
};
// Used by slot_base::set_parent() when a slot_base without a rep_ is assigned a parent.
class dummy_slot_rep : public sigc::internal::slot_rep
{
public:
dummy_slot_rep() : slot_rep(nullptr, nullptr, &clone) {}
static void* clone(void*) { return new dummy_slot_rep(); }
};
} // anonymous namespace
namespace sigc
{
namespace internal
{
// only MSVC needs this to guarantee that all new/delete are executed from the DLL module
#ifdef SIGC_NEW_DELETE_IN_LIBRARY_ONLY
void* slot_rep::operator new(size_t size_)
{
return malloc(size_);
}
void slot_rep::operator delete(void* p)
{
free(p);
}
#endif
void slot_rep::disconnect()
{
// Invalidate the slot.
// _Must_ be done here because parent_ might defer the actual
// destruction of the slot_rep and try to invoke it before that point.
// Must be done also for a slot without a parent, according to
// https://bugzilla.gnome.org/show_bug.cgi?id=311057
// See also https://bugzilla.gnome.org/show_bug.cgi?id=738602
call_ = nullptr;
if (parent_)
{
auto data_ = parent_;
parent_ = nullptr; // Just a precaution.
(cleanup_)(data_); // Notify the parent (might lead to destruction of this!).
}
}
//static
void* slot_rep::notify(void* data)
{
auto self_ = reinterpret_cast<slot_rep*>(data);
self_->call_ = nullptr; // Invalidate the slot.
// Make sure we are notified if disconnect() deletes self_, which is trackable.
destroy_notify_struct notifier;
self_->add_destroy_notify_callback(¬ifier, destroy_notify_struct::notify);
self_->disconnect(); // Disconnect the slot (might lead to deletion of self_!).
// If self_ has been deleted, the destructor has called destroy().
if (!notifier.deleted_)
{
self_->remove_destroy_notify_callback(¬ifier);
self_->destroy(); // Detach the stored functor from the other referred trackables and destroy it.
// destroy() might lead to deletion of self_. Bug #564005.
}
return nullptr;
}
} // namespace internal
slot_base::slot_base() noexcept
: rep_(nullptr),
blocked_(false)
{}
slot_base::slot_base(rep_type* rep) noexcept
: rep_(rep),
blocked_(false)
{}
slot_base::slot_base(const slot_base& src)
: rep_(nullptr),
blocked_(src.blocked_)
{
if (src.rep_)
{
//Check call_ so we can ignore invalidated slots.
//Otherwise, destroyed bound reference parameters (whose destruction caused the slot's invalidation) may be used during dup().
//Note: I'd prefer to check somewhere during dup(). murrayc.
if (src.rep_->call_)
rep_ = src.rep_->dup();
else
{
*this = slot_base(); //Return the default invalid slot.
}
}
}
slot_base::slot_base(slot_base&& src)
: rep_(nullptr),
blocked_(src.blocked_)
{
if (src.rep_)
{
if (src.rep_->parent_)
{
// src is connected to a parent, e.g. a sigc::signal.
// Copy, don't move! See https://bugzilla.gnome.org/show_bug.cgi?id=756484
//Check call_ so we can ignore invalidated slots.
//Otherwise, destroyed bound reference parameters (whose destruction
//caused the slot's invalidation) may be used during dup().
if (src.rep_->call_)
rep_ = src.rep_->dup();
else
blocked_ = false; //Return the default invalid slot.
}
else
{
// src is not connected. Really move src.rep_.
src.rep_->notify_callbacks();
rep_ = src.rep_;
//Wipe src:
src.rep_ = nullptr;
src.blocked_ = false;
}
}
}
slot_base::~slot_base()
{
if (rep_)
delete rep_;
}
slot_base::operator bool() const noexcept
{
return rep_ != nullptr;
}
void slot_base::delete_rep_with_check()
{
if (!rep_)
return;
// Make sure we are notified if disconnect() deletes rep_, which is trackable.
// Compare slot_rep::notify().
destroy_notify_struct notifier;
rep_->add_destroy_notify_callback(¬ifier, destroy_notify_struct::notify);
rep_->disconnect(); // Disconnect the slot (might lead to deletion of rep_!).
// If rep_ has been deleted, don't try to delete it again.
// If it has been deleted, this slot_base has probably also been deleted, so
// don't clear the rep_ pointer. It's the responsibility of the code that
// deletes rep_ to either clear the rep_ pointer or delete this slot_base.
if (!notifier.deleted_)
{
rep_->remove_destroy_notify_callback(¬ifier);
delete rep_; // Detach the stored functor from the other referred trackables and destroy it.
rep_ = nullptr;
}
}
slot_base& slot_base::operator=(const slot_base& src)
{
if (src.rep_ == rep_)
{
blocked_ = src.blocked_;
return *this;
}
if (src.empty())
{
delete_rep_with_check();
return *this;
}
auto new_rep_ = src.rep_->dup();
if (rep_) // Silently exchange the slot_rep.
{
new_rep_->set_parent(rep_->parent_, rep_->cleanup_);
delete rep_; // Calls destroy(), but does not call disconnect().
}
rep_ = new_rep_;
blocked_ = src.blocked_;
return *this;
}
slot_base& slot_base::operator=(slot_base&& src)
{
if (src.rep_ == rep_)
{
blocked_ = src.blocked_;
return *this;
}
if (src.empty())
{
delete_rep_with_check();
return *this;
}
blocked_ = src.blocked_;
internal::slot_rep* new_rep_ = nullptr;
if (src.rep_->parent_)
{
// src is connected to a parent, e.g. a sigc::signal.
// Copy, don't move! See https://bugzilla.gnome.org/show_bug.cgi?id=756484
new_rep_ = src.rep_->dup();
}
else
{
// src is not connected. Really move src.rep_.
src.rep_->notify_callbacks();
new_rep_ = src.rep_;
//Wipe src:
src.rep_ = nullptr;
src.blocked_ = false;
}
if (rep_) // Silently exchange the slot_rep.
{
new_rep_->set_parent(rep_->parent_, rep_->cleanup_);
delete rep_; // Calls destroy(), but does not call disconnect().
}
rep_ = new_rep_;
return *this;
}
void slot_base::set_parent(void* parent, void* (*cleanup)(void*)) const noexcept
{
if (!rep_)
rep_ = new dummy_slot_rep();
rep_->set_parent(parent, cleanup);
}
void slot_base::add_destroy_notify_callback(void* data, func_destroy_notify func) const
{
if (rep_)
rep_->add_destroy_notify_callback(data, func);
}
void slot_base::remove_destroy_notify_callback(void* data) const
{
if (rep_)
rep_->remove_destroy_notify_callback(data);
}
bool slot_base::block(bool should_block) noexcept
{
bool old = blocked_;
blocked_ = should_block;
return old;
}
bool slot_base::unblock() noexcept
{
return block(false);
}
void slot_base::disconnect()
{
if (rep_)
rep_->disconnect();
}
/*bool slot_base::empty() const // having this function not inline is killing performance !!!
{
if (rep_ && !rep_->call_)
{
delete rep_; // This is not strictly necessary here. I'm convinced that it is
rep_ = nullptr; // safe to wait for the destructor to delete the slot_rep. Martin.
}
return (rep_ == nullptr);
}*/
} //namespace sigc
|