summaryrefslogtreecommitdiff
path: root/sigc++/signal_base.cc
blob: c92a09138dd6b5a97d46eda3cfba6244d62c4194 (plain)
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
/*
 * Copyright 2003 - 2016, 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++/signal_base.h>
#include <memory> // std::unique_ptr

namespace sigc
{
namespace internal
{

// Data sent from signal_impl::insert() to slot_rep::set_parent() when a slot is
// connected, and then sent from slot_rep::disconnect() to
// signal_impl::notify_self_and_iter_of_invalidated_slot()
// when the slot is disconnected. Bug 167714.
struct self_and_iter : public notifiable
{
  const std::weak_ptr<signal_impl> self_;
  const signal_impl::iterator_type iter_;

  self_and_iter(const std::weak_ptr<signal_impl>& self, const signal_impl::iterator_type& iter)
  : self_(self), iter_(iter)
  {
  }
};

signal_impl::signal_impl() : exec_count_(0), deferred_(false) {}

signal_impl::~signal_impl()
{
  // Disconnect all slots before *this is deleted.
  clear();
}

// only MSVC needs this to guarantee that all new/delete are executed from the DLL module
#ifdef SIGC_NEW_DELETE_IN_LIBRARY_ONLY
void*
signal_impl::operator new(size_t size_)
{
  return malloc(size_);
}

void
signal_impl::operator delete(void* p)
{
  free(p);
}
#endif

void
signal_impl::clear()
{
  // Don't let signal_impl::notify_self_and_iter_of_invalidated_slot() erase the slots.
  // It would invalidate the iterator in the following loop.
  // Don't call shared_from_this() here. clear() is called from the destructor.
  // When the destructor is executing, shared_ptr's use_count has reached 0,
  // and it's no longer possible to get a shared_ptr to this.
  const bool during_signal_emission = exec_count_ > 0;
  const bool saved_deferred = deferred_;
  signal_impl_exec_holder exec(this);

  // Disconnect all connected slots before they are deleted.
  // signal_impl::notify_self_and_iter_of_invalidated_slot() will be called and
  // delete the self_and_iter structs.
  for (auto& slot : slots_)
    slot.disconnect();

  // Don't clear slots_ during signal emission. Provided deferred_ is true,
  // sweep() will be called from ~signal_impl_holder() after signal emission,
  // and it will erase all disconnected slots.
  // https://bugzilla.gnome.org/show_bug.cgi?id=784550
  if (!during_signal_emission)
  {
    deferred_ = saved_deferred;
    slots_.clear();
  }
}

signal_impl::size_type
signal_impl::size() const noexcept
{
  return slots_.size();
}

bool
signal_impl::blocked() const noexcept
{
  for (const auto& slot : const_cast<const std::list<slot_base>&>(slots_))
  {
    if (!slot.blocked())
      return false;
  }
  return true;
}

void
signal_impl::block(bool should_block) noexcept
{
  for (auto& slot : slots_)
  {
    slot.block(should_block);
  }
}

signal_impl::iterator_type
signal_impl::connect(const slot_base& slot_)
{
  return insert(slots_.end(), slot_);
}

signal_impl::iterator_type
signal_impl::connect(slot_base&& slot_)
{
  return insert(slots_.end(), std::move(slot_));
}

void
signal_impl::add_notification_to_iter(const signal_impl::iterator_type& iter)
{
  auto si = new self_and_iter(shared_from_this(), iter);
  iter->set_parent(si, &signal_impl::notify_self_and_iter_of_invalidated_slot);
}

signal_impl::iterator_type
signal_impl::insert(signal_impl::iterator_type i, const slot_base& slot_)
{
  auto iter = slots_.insert(i, slot_);
  add_notification_to_iter(iter);
  return iter;
}

signal_impl::iterator_type
signal_impl::insert(signal_impl::iterator_type i, slot_base&& slot_)
{
  auto iter = slots_.insert(i, std::move(slot_));
  add_notification_to_iter(iter);
  return iter;
}

void
signal_impl::sweep()
{
  // The deletion of a slot may cause the deletion of a signal_base,
  // a decrementation of ref_count_, and the deletion of this.
  // In that case, the deletion of this is deferred to ~signal_impl_holder().
  signal_impl_holder exec(shared_from_this());

  deferred_ = false;
  auto i = slots_.begin();
  while (i != slots_.end())
  {
    if ((*i).empty())
      i = slots_.erase(i);
    else
      ++i;
  }
}

// static
void
signal_impl::notify_self_and_iter_of_invalidated_slot(notifiable* d)
{
  std::unique_ptr<self_and_iter> si(static_cast<self_and_iter*>(d));
  auto self = si->self_.lock();
  if (!self)
  {
    // The signal_impl object is being deleted. The use_count has reached 0.
    // Nothing to do here. exec_count_ > 0 and clear() will restore deferred_.
    return;
  }

  if (self->exec_count_ == 0)
  {
    // The deletion of a slot may cause the deletion of a signal_base,
    // a decrementation of si->self_->ref_count_, and the deletion of si->self_.
    // In that case, the deletion of si->self_ is deferred to ~signal_impl_holder().
    // https://bugzilla.gnome.org/show_bug.cgi?id=564005#c24
    signal_impl_holder exec(self);
    self->slots_.erase(si->iter_);
  }
  else
  {
    // This is occurring during signal emission or slot erasure.
    // => sweep() will be called from ~signal_impl_holder() after signal emission.
    // This is safer because we don't have to care about our
    // iterators in emit() and clear().
    self->deferred_ = true;
  }
}

} /* namespace internal */

signal_base::signal_base() noexcept {}

signal_base::signal_base(const signal_base& src) noexcept : impl_(src.impl()) {}

signal_base::signal_base(signal_base&& src) : impl_(std::move(src.impl_))
{
  src.impl_ = nullptr;
}

signal_base::~signal_base() {}

void
signal_base::clear()
{
  if (impl_)
    impl_->clear();
}

signal_base::size_type
signal_base::size() const noexcept
{
  return (impl_ ? impl_->size() : 0);
}

bool
signal_base::blocked() const noexcept
{
  return (impl_ ? impl_->blocked() : true);
}

void
signal_base::block(bool should_block) noexcept
{
  if (impl_)
    impl_->block(should_block);
}

void
signal_base::unblock() noexcept
{
  if (impl_)
    impl_->block(false);
}

signal_base::iterator_type
signal_base::connect(const slot_base& slot_)
{
  return impl()->connect(slot_);
}

signal_base::iterator_type
signal_base::connect(slot_base&& slot_)
{
  return impl()->connect(std::move(slot_));
}

signal_base::iterator_type
signal_base::insert(iterator_type i, const slot_base& slot_)
{
  return impl()->insert(i, slot_);
}

signal_base::iterator_type
signal_base::insert(iterator_type i, slot_base&& slot_)
{
  return impl()->insert(i, std::move(slot_));
}

signal_base&
signal_base::operator=(const signal_base& src)
{
  if (src.impl_ == impl_)
    return *this;

  impl_ = src.impl();
  return *this;
}

signal_base&
signal_base::operator=(signal_base&& src)
{
  if (src.impl_ == impl_)
    return *this;

  impl_ = src.impl_;
  src.impl_ = nullptr;

  return *this;
}

std::shared_ptr<internal::signal_impl>
signal_base::impl() const
{
  if (!impl_)
  {
    impl_ = std::make_shared<internal::signal_impl>();
  }
  return impl_;
}

} /* sigc */