2 * Copyright 2002, The libsigc++ Development Team
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _SIGC_SIGNAL_BASE_H_
20 #define _SIGC_SIGNAL_BASE_H_
24 #include <sigc++config.h>
25 #include <sigc++/type_traits.h>
26 #include <sigc++/trackable.h>
27 #include <sigc++/functors/slot.h>
28 #include <sigc++/functors/mem_fun.h>
30 /** The libsigc++ namespace.
38 /** Implementation of the signal interface.
39 * signal_impl manages a list of slots. When a slot becomes
40 * invalid (because some referred object dies), notify() is executed.
41 * notify() either calls slots_.erase() directly or defers the execution of
42 * erase() to sweep() when the signal is being emitted. sweep() removes all
43 * invalid slots from the list.
45 struct SIGC_API signal_impl : public notifiable
47 using size_type = std::size_t;
48 using slot_list = std::list<slot_base>;
49 using iterator_type = slot_list::iterator;
50 using const_iterator_type = slot_list::const_iterator;
55 signal_impl(const signal_impl& src) = delete;
56 signal_impl& operator=(const signal_impl& src) = delete;
58 signal_impl(signal_impl&& src) = delete;
59 signal_impl& operator=(signal_impl&& src) = delete;
61 // only MSVC needs this to guarantee that all new/delete are executed from the DLL module
62 #ifdef SIGC_NEW_DELETE_IN_LIBRARY_ONLY
63 void* operator new(size_t size_);
64 void operator delete(void* p);
67 /// Increments the reference counter.
68 inline void reference() noexcept
71 /// Increments the reference and execution counter.
72 inline void reference_exec() noexcept
73 { ++ref_count_; ++exec_count_; }
75 /** Decrements the reference counter.
76 * The object is deleted when the reference counter reaches zero.
78 inline void unreference()
79 { if (!(--ref_count_)) delete this; }
81 /** Decrements the reference and execution counter.
82 * Invokes sweep() if the execution counter reaches zero and the
83 * removal of one or more slots has been deferred.
85 inline void unreference_exec()
87 if (!(--ref_count_)) delete this;
88 else if (!(--exec_count_) && deferred_) sweep();
91 /** Returns whether the list of slots is empty.
92 * @return @p true if the list of slots is empty.
94 inline bool empty() const noexcept
95 { return slots_.empty(); }
97 /// Empties the list of slots.
100 /** Returns the number of slots in the list.
101 * @return The number of slots in the list.
103 size_type size() const noexcept;
105 /** Returns whether all slots in the list are blocked.
106 * @return @p true if all slots are blocked or the list is empty.
110 bool blocked() const noexcept;
112 /** Sets the blocking state of all slots in the list.
113 * If @e should_block is @p true then the blocking state is set.
114 * Subsequent emissions of the signal don't invoke the functors
115 * contained in the slots until block() with @e should_block = @p false is called.
116 * sigc::slot_base::block() and sigc::slot_base::unblock() can change the
117 * blocking state of individual slots.
118 * @param should_block Indicates whether the blocking state should be set or unset.
122 void block(bool should_block = true) noexcept;
124 /** Adds a slot at the bottom of the list of slots.
125 * @param slot_ The slot to add to the list of slots.
126 * @return An iterator pointing to the new slot in the list.
128 iterator_type connect(const slot_base& slot_);
130 /** Adds a slot at the bottom of the list of slots.
131 * @param slot_ The slot to add to the list of slots.
132 * @return An iterator pointing to the new slot in the list.
136 iterator_type connect(slot_base&& slot_);
138 /** Adds a slot at the given position into the list of slots.
139 * @param i An iterator indicating the position where @p slot_ should be inserted.
140 * @param slot_ The slot to add to the list of slots.
141 * @return An iterator pointing to the new slot in the list.
143 iterator_type insert(iterator_type i, const slot_base& slot_);
145 /** Adds a slot at the given position into the list of slots.
146 * @param i An iterator indicating the position where @p slot_ should be inserted.
147 * @param slot_ The slot to add to the list of slots.
148 * @return An iterator pointing to the new slot in the list.
152 iterator_type insert(iterator_type i, slot_base&& slot_);
154 /** Removes the slot at the given position from the list of slots.
155 * @param i An iterator pointing to the slot to be removed.
156 * @return An iterator pointing to the slot in the list after the one removed.
158 iterator_type erase(iterator_type i);
160 /// Removes invalid slots from the list of slots.
163 /** Callback that is executed when some slot becomes invalid.
164 * This callback is registered in every slot when inserted into
165 * the list of slots. It is executed when a slot becomes invalid
166 * because of some referred object being destroyed.
167 * It either calls slots_.erase() directly or defers the execution of
168 * erase() to sweep() when the signal is being emitted.
169 * @param d A local structure, created in insert().
171 static void notify(notifiable* d);
173 /** Reference counter.
174 * The object is destroyed when @em ref_count_ reaches zero.
178 /** Execution counter.
179 * Indicates whether the signal is being emitted.
183 /// Indicates whether the execution of sweep() is being deferred.
186 /// The list of slots.
187 std::list<slot_base> slots_;
190 /// Exception safe sweeper for cleaning up invalid slots on the slot list.
191 struct SIGC_API signal_exec
193 /// The parent sigc::signal_impl object.
196 /** Increments the reference and execution counter of the parent sigc::signal_impl object.
197 * @param sig The parent sigc::signal_impl object.
199 inline signal_exec(const signal_impl* sig) noexcept
200 : sig_(const_cast<signal_impl*>(sig) )
201 { sig_->reference_exec(); }
203 /// Decrements the reference and execution counter of the parent sigc::signal_impl object.
204 inline ~signal_exec()
205 { sig_->unreference_exec(); }
208 /** Temporary slot list used during signal emission.
209 * Through evolution this class is slightly misnamed. It is now
210 * an index into the slot_list passed into it. It simply keeps track
211 * of where the end of this list was at construction, and pretends that's
212 * the end of your list. This way you may connect during emission without
213 * inadvertently entering an infinite loop, as well as make other
214 * modifications to the slot_list at your own risk.
216 struct temp_slot_list
218 using slot_list = signal_impl::slot_list;
219 using iterator = signal_impl::iterator_type;
220 using const_iterator = signal_impl::const_iterator_type;
222 temp_slot_list(slot_list &slots) : slots_(slots)
224 placeholder = slots_.insert(slots_.end(), slot_base());
229 slots_.erase(placeholder);
232 iterator begin() { return slots_.begin(); }
233 iterator end() { return placeholder; }
234 const_iterator begin() const { return slots_.begin(); }
235 const_iterator end() const { return placeholder; }
239 slot_list::iterator placeholder;
242 } /* namespace internal */
245 /** @defgroup signal Signals
246 * Use sigc::signal::connect() with sigc::mem_fun() and sigc::ptr_fun() to connect a method or function with a signal.
249 * signal_clicked.connect( sigc::mem_fun(*this, &MyWindow::on_clicked) );
252 * When the signal is emitted your method will be called.
254 * signal::connect() returns a connection, which you can later use to disconnect your method.
255 * If the type of your object inherits from sigc::trackable the method is disconnected
256 * automatically when your object is destroyed.
258 * When signals are copied they share the underlying information,
259 * so you can have a protected/private sigc::signal member and a public accessor method.
260 * A sigc::signal is a kind of reference-counting pointer. It's similar to
261 * std::shared_ptr<>, although sigc::signal is restricted to holding a pointer to
262 * a sigc::internal::signal_impl object that contains the implementation of the signal.
268 * using MySignalType = sigc::signal<void()>;
269 * MySignalType get_my_signal() { return m_my_signal; }
271 * MySignalType m_my_signal;
275 * signal and slot objects provide the core functionality of this
276 * library. A slot is a container for an arbitrary functor.
277 * A signal is a list of slots that are executed on emission.
278 * For compile time type safety a list of template arguments
279 * must be provided for the signal template that determines the
280 * parameter list for emission. Functors and closures are converted
281 * into slots implicitly on connection, triggering compiler errors
282 * if the given functor or closure cannot be invoked with the
283 * parameter list of the signal to connect to.
285 * Almost any functor with the correct signature can be converted to a sigc::slot
286 * and connected to a signal. See @ref slot "Slots" and sigc::signal::connect().
289 /** Base class for the sigc::signal# templates.
290 * signal_base integrates most of the interface of the derived sigc::signal#
291 * templates. The implementation, however, resides in sigc::internal::signal_impl.
292 * A sigc::internal::signal_impl object is dynamically allocated from signal_base
293 * when first connecting a slot to the signal. This ensures that empty signals
294 * don't waste memory.
296 * sigc::internal::signal_impl is reference-counted. When a sigc::signal# object
297 * is copied, the reference count of its sigc::internal::signal_impl object is
298 * incremented. Both sigc::signal# objects then refer to the same
299 * sigc::internal::signal_impl object.
303 struct SIGC_API signal_base : public trackable
305 using size_type = std::size_t;
307 signal_base() noexcept;
309 signal_base(const signal_base& src) noexcept;
311 signal_base(signal_base&& src);
315 signal_base& operator=(const signal_base& src);
317 signal_base& operator=(signal_base&& src);
319 /** Returns whether the list of slots is empty.
320 * @return @p true if the list of slots is empty.
322 inline bool empty() const noexcept
323 { return (!impl_ || impl_->empty()); }
325 /// Empties the list of slots.
328 /** Returns the number of slots in the list.
329 * @return The number of slots in the list.
331 size_type size() const noexcept;
333 /** Returns whether all slots in the list are blocked.
334 * @return @p true if all slots are blocked or the list is empty.
338 bool blocked() const noexcept;
340 /** Sets the blocking state of all slots in the list.
341 * If @e should_block is @p true then the blocking state is set.
342 * Subsequent emissions of the signal don't invoke the functors
343 * contained in the slots until unblock() or block() with
344 * @e should_block = @p false is called.
345 * sigc::slot_base::block() and sigc::slot_base::unblock() can change the
346 * blocking state of individual slots.
347 * @param should_block Indicates whether the blocking state should be set or unset.
351 void block(bool should_block = true) noexcept;
353 /** Unsets the blocking state of all slots in the list.
357 void unblock() noexcept;
360 using iterator_type = internal::signal_impl::iterator_type;
362 /** Adds a slot at the end of the list of slots.
363 * With connect(), slots can also be added during signal emission.
364 * In this case, they won't be executed until the next emission occurs.
365 * @param slot_ The slot to add to the list of slots.
366 * @return An iterator pointing to the new slot in the list.
368 iterator_type connect(const slot_base& slot_);
370 /** Adds a slot at the end of the list of slots.
371 * With connect(), slots can also be added during signal emission.
372 * In this case, they won't be executed until the next emission occurs.
373 * @param slot_ The slot to add to the list of slots.
374 * @return An iterator pointing to the new slot in the list.
378 iterator_type connect(slot_base&& slot_);
380 /** Adds a slot at the given position into the list of slots.
381 * Note that this function does not work during signal emission!
382 * @param i An iterator indicating the position where @e slot_ should be inserted.
383 * @param slot_ The slot to add to the list of slots.
384 * @return An iterator pointing to the new slot in the list.
386 iterator_type insert(iterator_type i, const slot_base& slot_);
388 /** Adds a slot at the given position into the list of slots.
389 * Note that this function does not work during signal emission!
390 * @param i An iterator indicating the position where @e slot_ should be inserted.
391 * @param slot_ The slot to add to the list of slots.
392 * @return An iterator pointing to the new slot in the list.
396 iterator_type insert(iterator_type i, slot_base&& slot_);
398 /** Removes the slot at the given position from the list of slots.
399 * Note that this function does not work during signal emission!
400 * @param i An iterator pointing to the slot to be removed.
401 * @return An iterator pointing to the slot in the list after the one removed.
403 iterator_type erase(iterator_type i);
405 /** Returns the signal_impl object encapsulating the list of slots.
406 * @return The signal_impl object encapsulating the list of slots.
408 internal::signal_impl* impl() const;
410 /// The signal_impl object encapsulating the slot list.
411 mutable internal::signal_impl* impl_;
416 #endif /* _SIGC_SIGNAL_BASE_H_ */