Debian Codegraph @

src/3rdparty/kdbindings/signal.h

raw symbols SCIP
1 /*
2 This file is part of KDBindings.
3
4 SPDX-FileCopyrightText: 2021-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
5 Author: Sean Harmer <[email protected]>
6
7 SPDX-License-Identifier: MIT
8
9 Contact KDAB at <[email protected]> for commercial licensing options.
10 */
11
12 #pragma once
13
14 #include <assert.h>
15 #include <functional>
16 #include <memory>
17 #include <optional>
18 #include <stdexcept>
19 #include <type_traits>
20 #include <utility>
21
22 #include <kdbindings/genindex_array.h>cxx . . $ `<file>/src/3rdparty/kdbindings/genindex_array.h`/
23 #include <kdbindings/utils.h>cxx . . $ `<file>/src/3rdparty/kdbindings/utils.h`/
24
25 /**
26 * @brief The main namespace of the KDBindings library.
27 *
28 * All public parts of KDBindings are members of this namespace.
29 */
30 namespace KDBindingscxx . . $ KDBindings/ {
31
32 template<typename... Args>
33 class Signalcxx . . $ KDBindings/Signal#;
34
35 namespace Privatecxx . . $ KDBindings/Private/ {
36 //
37 // This class defines a virtual interface, that the Signal this ConnectionHandle refers
38 // to must implement.
39 // It allows ConnectionHandle to refer to this non-template class, which then dispatches
40 // to the template implementation using virtual function calls.
41 // It allows ConnectionHandle to be a non-template class.
42 class SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#
43 {
44 public:
45 SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#SignalImplBase(49f6e7a06ebc5aa8).() = default;
46
47 virtual ~cxx . . $ KDBindings/Private/SignalImplBase#`~SignalImplBase`(49f6e7a06ebc5aa8).SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#() = default;
48
49 virtual void disconnectcxx . . $ KDBindings/Private/SignalImplBase#disconnect(251baa0f1a2def91).(const GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id) = 0;
50 virtual bool blockConnectioncxx . . $ KDBindings/Private/SignalImplBase#blockConnection(927efb9af8bb5c12).(const GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id, bool blocked) = 0;
51 virtual bool isConnectionActivecxx . . $ KDBindings/Private/SignalImplBase#isConnectionActive(bda2978672819f17).(const GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id) const = 0;
52 virtual bool isConnectionBlockedcxx . . $ KDBindings/Private/SignalImplBase#isConnectionBlocked(bda2978672819f17).(const GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id) const = 0;
53 };
54
55 } // namespace Private
56
57 /**
58 * @brief A ConnectionHandle represents the connection of a Signal
59 * to a slot (i.e. a function that is called when the Signal is emitted).
60 *
61 * It is returned from a Signal when a connection is created and used to
62 * manage the connection by disconnecting, (un)blocking it and checking its state.
63 *
64 * To make sure a Connection to an object is disconnected correctly, consider
65 * storing a ScopedConnection to its ConnectionHandle inside the object.
66 **/
67 class ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#
68 {
69 public:
70 /**
71 * A ConnectionHandle can be default constructed.
72 * In this case the ConnectionHandle will not reference any active connection (i.e. isActive() will return false),
73 * and not belong to any Signal.
74 **/
75 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#ConnectionHandle(49f6e7a06ebc5aa8).() = default;
76
77 /**
78 * A ConnectionHandle can be copied.
79 **/
80 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#ConnectionHandle(3ac1fd0ae8579b93).(const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &) = default;
81 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &operatorcxx . . $ KDBindings/ConnectionHandle#`operator=`(6055553a7658cd5b).=(const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &) = default;
82
83 /**
84 * A ConnectionHandle can be moved.
85 **/
86 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#ConnectionHandle(13a0c65a46a31634).(ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &&) = default;
87 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &operatorcxx . . $ KDBindings/ConnectionHandle#`operator=`(ff71c4ae9e774754).=(ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &&) = default;
88
89 /**
90 * Disconnect the slot.
91 *
92 * When this function is called, the function that was passed to Signal::connect
93 * to create this ConnectionHandle will no longer be called when the Signal is emitted.
94 *
95 * If the ConnectionHandle is not active or the connection has already been disconnected,
96 * nothing happens.
97 *
98 * After this call, the ConnectionHandle will be inactive (i.e. isActive() returns false)
99 * and will no longer belong to any Signal (i.e. belongsTo returns false).
100 **/
101 void disconnectcxx . . $ KDBindings/ConnectionHandle#disconnect(49f6e7a06ebc5aa8).()
102 {
103 if (auto shared_impl = checkedLockcxx . . $ KDBindings/ConnectionHandle#checkedLock(5a25885d1ef55a3b).()) {
104 shared_impl->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).disconnectcxx . . $ KDBindings/Private/SignalImplBase#disconnect(251baa0f1a2def91).(m_idcxx . . $ KDBindings/ConnectionHandle#m_id.);
105 }
106 // ConnectionHandle is no longer active;
107 m_signalImplcxx . . $ KDBindings/ConnectionHandle#m_signalImpl..resetcxx . . $ std/__weak_ptr#reset(ced63f7c635d850d).();
108 }
109
110 /**
111 * Check whether the connection of this ConnectionHandle is active.
112 *
113 * @return true if the ConnectionHandle refers to an active Signal
114 * and the connection was not disconnected previously, false otherwise.
115 **/
116 bool isActivecxx . . $ KDBindings/ConnectionHandle#isActive(50ce9a9e25b4a850).() const
117 {
118 return static_cast<bool>(checkedLockcxx . . $ KDBindings/ConnectionHandle#checkedLock(5a25885d1ef55a3b).());
119 }
120
121 /**
122 * Sets the block state of the connection.
123 * If a connection is blocked, emitting the Signal will no longer call this
124 * connections slot, until the connection is unblocked.
125 *
126 * Behaves the same as calling Signal::blockConnection with this
127 * ConnectionHandle as argument.
128 *
129 * To temporarily block a connection, consider using an instance of ConnectionBlocker,
130 * which offers a RAII-style implementation that makes sure the connection is always
131 * returned to its original state.
132 *
133 * @param blocked The new blocked state of the connection.
134 * @return whether the connection was previously blocked.
135 * @throw std::out_of_range Throws if the connection is not active (i.e. isActive() returns false).
136 **/
137 bool blockcxx . . $ KDBindings/ConnectionHandle#block(efaadfb7dc14930a).(bool blocked)
138 {
139 if (auto shared_impl = checkedLockcxx . . $ KDBindings/ConnectionHandle#checkedLock(5a25885d1ef55a3b).()) {
140 return shared_impl->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).blockConnectioncxx . . $ KDBindings/Private/SignalImplBase#blockConnection(927efb9af8bb5c12).(m_idcxx . . $ KDBindings/ConnectionHandle#m_id., blocked);
141 }
142 throw stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range#("Cannot block a non-active connection!");
143 }
144
145 /**
146 * Checks whether the connection is currently blocked.
147 *
148 * To change the blocked state of a connection, call ConnectionHandle::block.
149 *
150 * @return whether the connection is currently blocked.
151 **/
152 bool isBlockedcxx . . $ KDBindings/ConnectionHandle#isBlocked(50ce9a9e25b4a850).() const
153 {
154 if (auto shared_impl = checkedLockcxx . . $ KDBindings/ConnectionHandle#checkedLock(5a25885d1ef55a3b).()) {
155 return shared_impl->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).isConnectionBlockedcxx . . $ KDBindings/Private/SignalImplBase#isConnectionBlocked(bda2978672819f17).(m_idcxx . . $ KDBindings/ConnectionHandle#m_id.);
156 }
157 throw stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range#("Cannot check whether a non-active connection is blocked!");
158 }
159
160 /**
161 * Check whether this ConnectionHandle belongs to the given Signal.
162 *
163 * @return true if this ConnectionHandle refers to a connection within the given Signal
164 **/
165 template<typename... Args>
166 bool belongsTocxx . . $ KDBindings/ConnectionHandle#belongsTo(2a815907173526ab).(const Signalcxx . . $ KDBindings/Signal#<Args...> &signal) const
167 {
168 auto shared_impl = m_signalImplcxx . . $ KDBindings/ConnectionHandle#m_signalImpl..lockcxx . . $ std/weak_ptr#lock(a1b13de252cffcea).();
169 return shared_impl && shared_impl ==cxx . . $ `operator==`(be3220dc0d0013a7). stdcxx . . $ std/::static_pointer_castcxx . . $ std/static_pointer_cast(d1ff2bba4f627176).<Privatecxx . . $ KDBindings/Private/::SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#>(signal.m_implcxx . . $ KDBindings/Signal#m_impl.);
170 }
171
172 private:
173 template<typename...>
174 friend class Signalcxx . . $ KDBindings/Signal#;
175
176 stdcxx . . $ std/::weak_ptrcxx . . $ std/weak_ptr#<Privatecxx . . $ KDBindings/Private/::SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#> m_signalImplcxx . . $ KDBindings/ConnectionHandle#m_signalImpl.;
177 Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# m_idcxx . . $ KDBindings/ConnectionHandle#m_id.;
178
179 // private, so it is only available from Signal
180 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#ConnectionHandle(c41843ee541efd50).(stdcxx . . $ std/::weak_ptrcxx . . $ std/weak_ptr#<Privatecxx . . $ KDBindings/Private/::SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#> signalImpl, Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# id)
181 : m_signalImplcxx . . $ KDBindings/ConnectionHandle#m_signalImpl.{ stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(signalImpl) }, m_idcxx . . $ KDBindings/ConnectionHandle#m_id.{ stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(id) }
182 {
183 }
184
185 // Checks that the weak_ptr can be locked and that the connection is
186 // still active
187 stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<Privatecxx . . $ KDBindings/Private/::SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#> checkedLockcxx . . $ KDBindings/ConnectionHandle#checkedLock(5a25885d1ef55a3b).() const
188 {
189 auto shared_impl = m_signalImplcxx . . $ KDBindings/ConnectionHandle#m_signalImpl..lockcxx . . $ std/weak_ptr#lock(a1b13de252cffcea).();
190 if (shared_impl && shared_impl->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).isConnectionActivecxx . . $ KDBindings/Private/SignalImplBase#isConnectionActive(bda2978672819f17).(m_idcxx . . $ KDBindings/ConnectionHandle#m_id.)) {
191 return shared_implcxx . . $ std/shared_ptr#shared_ptr(e98954a1f10335bf).;
192 }
193 return nullptrcxx . . $ std/shared_ptr#shared_ptr(6d988fd07f070dfc).;
194 }
195 };
196
197 /**
198 * @brief A Signal provides a mechanism for communication between objects.
199 *
200 * KDBindings::Signal recreates the <a href="https://doc.qt.io/qt-5/signalsandslots.html">Qt's Signals & Slots mechanism</a> in pure C++17.
201 * A Signal can be used to notify any number of slots that a certain event has occurred.
202 *
203 * The slot can be almost any callable object, including member functions and lambdas.
204 *
205 * This connection happens in a type-safe manner, as a slot can only be connected to
206 * a Signal when the arguments of the slot match the values the Signal emits.
207 *
208 * The Args type parameter pack describe which value types the Signal will emit.
209 *
210 * Examples:
211 * - @ref 01-simple-connection/main.cpp
212 * - @ref 02-signal-member/main.cpp
213 * - @ref 03-member-arguments/main.cpp
214 * - @ref 07-advanced-connections/main.cpp
215 */
216 template<typename... Args>
217 class Signalcxx . . $ KDBindings/Signal#
218 {
219 static_assert(
220 stdcxx . . $ std/::conjunction<std::negation<std::is_rvalue_reference<Args>>...>::value,
221 "R-value references are not allowed as Signal parameters!");
222
223 // The Signal::Impl class exists, so Signals can be implemented in a PIMPL-like way.
224 // This allows us to easily move Signals without losing their ConnectionHandles, as well as
225 // making an unconnected Signal only sizeof(shared_ptr).
226 class Implcxx . . $ KDBindings/Signal#Impl# : public Privatecxx . . $ KDBindings/Private/::SignalImplBasecxx . . $ KDBindings/Private/SignalImplBase#
227 {
228 public:
229 Implcxx . . $ KDBindings/Signal#Impl#Impl(ced63f7c635d850d).() noexcept { }
230
231 ~cxx . . $ KDBindings/Signal#Impl#`~Impl`(ced63f7c635d850d).Implcxx . . $ KDBindings/Signal#Impl#() noexcept { }
232
233 // Signal::Impls are not copyable
234 Implcxx . . $ KDBindings/Signal#Impl#Impl(c6dcb9c6a2120355).(Implcxx . . $ KDBindings/Signal#Impl# const &other) = delete;
235 Implcxx . . $ KDBindings/Signal#Impl# &operatorcxx . . $ KDBindings/Signal#Impl#`operator=`(ea6a7b5f983fff7c).=(Implcxx . . $ KDBindings/Signal#Impl# const &other) = delete;
236
237 // Signal::Impls are not moveable, this would break the ConnectionHandles
238 Implcxx . . $ KDBindings/Signal#Impl#Impl(60adc2594a7f0811).(Implcxx . . $ KDBindings/Signal#Impl# &&other) = delete;
239 Implcxx . . $ KDBindings/Signal#Impl# &operatorcxx . . $ KDBindings/Signal#Impl#`operator=`(dc94ba915c54cc84).=(Implcxx . . $ KDBindings/Signal#Impl# &&other) = delete;
240
241 // Connects a std::function to the signal. The returned
242 // value can be used to disconnect the function again.
243 Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# connectcxx . . $ KDBindings/Signal#Impl#connect(ea223e9f67ae6cfd).(stdcxx . . $ std/::functioncxx . . $ std/function#<void(Args...)> const &slot)
244 {
245 return m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..insertcxx . . $ KDBindings/Private/GenerationalIndexArray#insert(cd2615d054396bb4).({ slot });
246 }
247
248 // Disconnects a previously connected function
249 void disconnectcxx . . $ KDBindings/Signal#Impl#disconnect(251baa0f1a2def91).(const Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id) override
250 {
251 m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..erasecxx . . $ KDBindings/Private/GenerationalIndexArray#erase(a084f6e2403fc172).(id);
252 }
253
254 // Disconnects all previously connected functions
255 void disconnectAllcxx . . $ KDBindings/Signal#Impl#disconnectAll(49f6e7a06ebc5aa8).()
256 {
257 m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..clearcxx . . $ KDBindings/Private/GenerationalIndexArray#clear(49f6e7a06ebc5aa8).();
258 }
259
260 bool blockConnectioncxx . . $ KDBindings/Signal#Impl#blockConnection(927efb9af8bb5c12).(const Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id, bool blocked) override
261 {
262 Connectioncxx . . $ KDBindings/Signal#Impl#Connection# *connection = m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..getcxx . . $ KDBindings/Private/GenerationalIndexArray#get(5274c9030ae475e0).(id);
263 if (connection) {
264 const bool wasBlocked = connection->blockedcxx . . $ KDBindings/Signal#Impl#Connection#blocked.;
265 connection->blockedcxx . . $ KDBindings/Signal#Impl#Connection#blocked. = blocked;
266 return wasBlocked;
267 } else {
268 throw stdcxx . . $ std/out_of_range#out_of_range(85c52e162fed56f9).::out_of_rangecxx . . $ std/out_of_range#("Provided ConnectionHandle does not match any connection\nLikely the connection was deleted before!");
269 }
270 }
271
272 bool isConnectionActivecxx . . $ KDBindings/Signal#Impl#isConnectionActive(bda2978672819f17).(const Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id) const override
273 {
274 return m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..getcxx . . $ KDBindings/Private/GenerationalIndexArray#get(5274c9030ae475e0).(id);
275 }
276
277 bool isConnectionBlockedcxx . . $ KDBindings/Signal#Impl#isConnectionBlocked(bda2978672819f17).(const Privatecxx . . $ KDBindings/Private/::GenerationalIndexcxx . . $ KDBindings/Private/GenerationalIndex# &id) const override
278 {
279 auto connection = m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..getcxx . . $ KDBindings/Private/GenerationalIndexArray#get(117af085e11bc316).(id);
280 if (connection) {
281 return connection->blocked;
282 } else {
283 throw stdcxx . . $ std/out_of_range#out_of_range(85c52e162fed56f9).::out_of_rangecxx . . $ std/out_of_range#("Provided ConnectionHandle does not match any connection\nLikely the connection was deleted before!");
284 }
285 }
286
287 // Calls all connected functions
288 void emitcxx . . $ KDBindings/Signal#Impl#emit(470c718aa20b1b04).(Args... p) const
289 {
290 const auto numEntries = m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..entriesSizecxx . . $ KDBindings/Private/GenerationalIndexArray#entriesSize(d22c8608b6eae7b5).();
291
292 // This loop can tolerate signal handles being disconnected inside a slot,
293 // but adding new connections to a signal inside a slot will still be undefined behaviour
294 for (auto i = decltype(numEntries){ 0 }; i < numEntries; ++i) {
295 const auto index = m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..indexAtEntrycxx . . $ KDBindings/Private/GenerationalIndexArray#indexAtEntry(bb268b28980a0399).(i);
296
297 if (index) {
298 const auto con = m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections..getcxx . . $ KDBindings/Private/GenerationalIndexArray#get(5274c9030ae475e0).(*cxx . . $ `operator*`(4745473a2cd80725).index);
299
300 if (!con->blocked)
301 con->slot(p...);
302 }
303 }
304 }
305
306 private:
307 struct Connectioncxx . . $ KDBindings/Signal#Impl#Connection# {
308 stdcxx . . $ std/::functioncxx . . $ std/function#<void(Args...)> slotcxx . . $ KDBindings/Signal#Impl#Connection#slot.;
309 bool blockedcxx . . $ KDBindings/Signal#Impl#Connection#blocked.{ false };
310 };
311 mutable Privatecxx . . $ KDBindings/Private/::GenerationalIndexArraycxx . . $ KDBindings/Private/GenerationalIndexArray#<Connectioncxx . . $ KDBindings/Signal#Impl#Connection#> m_connectionscxx . . $ KDBindings/Signal#Impl#m_connections.;
312 };
313
314 public:
315 /** Signals are default constructible */
316 Signalcxx . . $ KDBindings/Signal#`Signal<Args...>`(49f6e7a06ebc5aa8).() = default;
317
318 /**
319 * Signals cannot be copied.
320 **/
321 Signalcxx . . $ KDBindings/Signal#`Signal<Args...>`(5202758b0606f6cb).(const Signal &) = delete;
322 Signal &operatorcxx . . $ KDBindings/Signal#`operator=`(8138676065518908).=(Signal const &other) = delete;
323
324 /** Signals can be moved */
325 Signalcxx . . $ KDBindings/Signal#`Signal<Args...>`(53bc00d2f8bbf0c9).(Signal &&other) noexcept = default;
326 Signal &operatorcxx . . $ KDBindings/Signal#`operator=`(de40dbf294de0f89).=(Signal &&other) noexcept = default;
327
328 /**
329 * A signal disconnects all slots when it is destructed
330 *
331 * Therefore, all active ConnectionHandles that belonged to this Signal
332 * will no longer be active (i.e. ConnectionHandle::isActive will return false).
333 */
334 ~cxx . . $ KDBindings/Signal#`~Signal<Args...>`(49f6e7a06ebc5aa8).Signal()
335 {
336 disconnectAllcxx . . $ KDBindings/Signal#disconnectAll(49f6e7a06ebc5aa8).();
337 }
338
339 /**
340 * Connects a std::function to the signal.
341 *
342 * When emit() is called on the Signal, the functions will be called with
343 * the arguments provided to emit().
344 *
345 * @return An instance of ConnectionHandle, that can be used to disconnect
346 * or temporarily block the connection.
347 */
348 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# connectcxx . . $ KDBindings/Signal#connect(4555b6ed298bedbb).(stdcxx . . $ std/::functioncxx . . $ std/function#<void(Args...)> const &slot)
349 {
350 ensureImplcxx . . $ KDBindings/Signal#ensureImpl(49f6e7a06ebc5aa8).();
351
352 return ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#{ m_implcxx . . $ KDBindings/Signal#m_impl., m_implcxx . . $ KDBindings/Signal#m_impl.->connect(slot) };
353 }
354
355 /**
356 * A template overload of Signal::connect that makes it easier to connect arbitrary functions to this
357 * Signal.
358 * It connects a function to this Signal, binds any provided arguments to that function and discards
359 * any values emitted by this Signal that aren't needed by the resulting function.
360 *
361 * This is especially useful for connecting member functions to signals.
362 *
363 * Examples:
364 * @code
365 * Signal<int> signal;
366 * std::vector<int> numbers{ 1, 2, 3 };
367 * bool emitted = false;
368 *
369 * // disambiguation necessary, as push_back is overloaded.
370 * void (std::vector<int>::*push_back)(const int &) = &std::vector<int>::push_back;
371 * signal.connect(push_back, &numbers);
372 *
373 * // this slot doesn't require the int argument, so it will be discarded.
374 * signal.connect([&emitted]() { emitted = true; });
375 *
376 * signal.emit(4); // Will add 4 to the vector and set emitted to true
377 * @endcode
378 *
379 * For more examples see the @ref 07-advanced-connections/main.cpp example.
380 *
381 * @return An instance of a Signal::ConnectionHandle that refers to this connection.
382 * Warning: When connecting a member function you must use the returned ConnectionHandle
383 * to disconnect when the object containing the slot goes out of scope!
384 **/
385 // The enable_if_t makes sure that this connect function specialization is only
386 // available if we provide a function that cannot be otherwise converted to a
387 // std::function<void(Args...)>, as it otherwise tries to take precedence
388 // over the normal connect function.
389 template<typename Func, typename... FuncArgs, typename = stdcxx . . $ std/::enable_if_t<stdcxx . . $ std/::disjunction_v<stdcxx . . $ std/::negation<stdcxx . . $ std/::is_convertible<Func, stdcxx . . $ std/::functioncxx . . $ std/function#<void(Args...)>>>, stdcxx . . $ std/::integral_constant<bool, sizeof...(FuncArgs) /*Also enable this function if we want to bind at least one argument*/>>>>
390 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# connectcxx . . $ KDBindings/Signal#connect(f9e75894d901c72d).(Func &&slot, FuncArgs &&...args)
391 {
392 stdcxx . . $ std/::functioncxx . . $ std/function#<void(Args...)> bound = Privatecxx . . $ KDBindings/Private/::bind_firstcxx . . $ KDBindings/Private/bind_first(de835b68c80cc71b).(stdcxx . . $ std/::forwardcxx . . $ std/forward(2d11db7e2d3d358e).<Func>(slot), stdcxx . . $ std/::forwardcxx . . $ std/forward(f56228ea88711827).<FuncArgs>(args)...);
393 return connectcxx . . $ KDBindings/Signal#connect(f9e75894d901c72d).(bound);
394 }
395
396 /**
397 * Disconnect a previously connected slot.
398 *
399 * After the slot was successfully disconnected, the ConnectionHandle will no
400 * longer be active. (i.e. ConnectionHandle::isActive will return false).
401 *
402 * @throw std::out_of_range - If the ConnectionHandle does not belong to this
403 * Signal (i.e. ConnectionHandle::belongsTo returns false).
404 */
405 void disconnectcxx . . $ KDBindings/Signal#disconnect(8702ac75773e8ce1).(const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &handle)
406 {
407 if (m_implcxx . . $ KDBindings/Signal#m_impl. && handle.belongsTocxx . . $ KDBindings/ConnectionHandle#belongsTo(2a815907173526ab).(*this)) {
408 m_implcxx . . $ KDBindings/Signal#m_impl.->disconnect(handle.m_idcxx . . $ KDBindings/ConnectionHandle#m_id.);
409 // TODO check if Impl is now empty and reset
410 } else {
411 throw stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range#("Provided ConnectionHandle does not match any connection\nLikely the connection was deleted before!");
412 }
413 }
414
415 /**
416 * Disconnect all previously connected functions.
417 *
418 * All currently active ConnectionHandles that belong to this Signal will no
419 * longer be active afterwards. (i.e. ConnectionHandle::isActive will return false).
420 */
421 void disconnectAllcxx . . $ KDBindings/Signal#disconnectAll(49f6e7a06ebc5aa8).()
422 {
423 if (m_implcxx . . $ KDBindings/Signal#m_impl.) {
424 m_implcxx . . $ KDBindings/Signal#m_impl.->disconnectAll();
425 // Once all connections are disconnected, we can release ownership of the Impl.
426 // This does not destroy the Signal itself, just the Impl object.
427 // If another slot is connected, another Impl object will be constructed.
428 m_implcxx . . $ KDBindings/Signal#m_impl..resetcxx . . $ std/__shared_ptr#reset(b5ba1254542b9606).();
429 }
430 // If m_impl is nullptr, we don't have any connections to disconnect
431 }
432
433 /**
434 * Sets the block state of the connection.
435 * If a connection is blocked, emitting the Signal will no longer call this
436 * connections slot, until the connection is unblocked.
437 *
438 * ConnectionHandle::block can be used as an alternative.
439 *
440 * To temporarily block a connection, consider using an instance of ConnectionBlocker,
441 * which offers a RAII-style implementation that makes sure the connection is always
442 * returned to its original state.
443 *
444 * @param blocked Whether the connection should be blocked from now on.
445 * @param handle The ConnectionHandle to block.
446 * @return Whether the connection was previously blocked.
447 * @throw std::out_of_range - If the ConnectionHandle does not belong to this
448 * Signal (i.e. ConnectionHandle::belongsTo returns false).
449 */
450 bool blockConnectioncxx . . $ KDBindings/Signal#blockConnection(68ead3fa068f6d95).(const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &handle, bool blocked)
451 {
452 if (m_implcxx . . $ KDBindings/Signal#m_impl. && handle.belongsTocxx . . $ KDBindings/ConnectionHandle#belongsTo(2a815907173526ab).(*this)) {
453 return m_implcxx . . $ KDBindings/Signal#m_impl.->blockConnection(handle.m_idcxx . . $ KDBindings/ConnectionHandle#m_id., blocked);
454 } else {
455 throw stdcxx . . $ std/out_of_range#out_of_range(85c52e162fed56f9).::out_of_rangecxx . . $ std/out_of_range#("Provided ConnectionHandle does not match any connection\nLikely the connection was deleted before!");
456 }
457 }
458
459 /**
460 * Checks whether the connection is currently blocked.
461 *
462 * To change the blocked state of a connection, call blockConnection().
463 *
464 * @return Whether the connection is currently blocked
465 * @throw std::out_of_range - If the ConnectionHandle does not belong to this
466 * Signal (i.e. ConnectionHandle::belongsTo returns false).
467 */
468 bool isConnectionBlockedcxx . . $ KDBindings/Signal#isConnectionBlocked(d69f28adba7d9b69).(const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &handle) const
469 {
470 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(handle.belongsTo(*this));
471 if (!m_implcxx . . $ KDBindings/Signal#m_impl.) {
472 throw stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range#("Provided ConnectionHandle does not match any connection\nLikely the connection was deleted before!");
473 }
474
475 return m_implcxx . . $ KDBindings/Signal#m_impl.->isConnectionBlocked(handle.m_idcxx . . $ KDBindings/ConnectionHandle#m_id.);
476 }
477
478 /**
479 * Emits the Signal, which causes all connected slots to be called,
480 * as long as they are not blocked.
481 *
482 * The arguments provided to emit will be passed to each slot by copy,
483 * therefore consider using (const) references as the Args to the Signal
484 * wherever possible.
485 *
486 * Note: Slots may disconnect themselves during an emit, however it is
487 * undefined whether a slot that is connected during the emit function
488 * of the Signal will also be called during this emit, or only at the next
489 * emit.
490 */
491 void emitcxx . . $ KDBindings/Signal#emit(470c718aa20b1b04).(Args... p) const
492 {
493 if (m_implcxx . . $ KDBindings/Signal#m_impl.)
494 m_implcxx . . $ KDBindings/Signal#m_impl.->emit(p...);
495
496 // if m_impl is nullptr, we don't have any slots connected, don't bother emitting
497 }
498
499 private:
500 friend class ConnectionHandlecxx . . $ KDBindings/ConnectionHandle#;
501
502 void ensureImplcxx . . $ KDBindings/Signal#ensureImpl(49f6e7a06ebc5aa8).()
503 {
504 if (!m_implcxx . . $ KDBindings/Signal#m_impl.) {
505 m_implcxx . . $ KDBindings/Signal#m_impl. = stdcxx . . $ std/::make_sharedcxx . . $ std/make_shared(ffca0f3977536948).<Implcxx . . $ KDBindings/Signal#Impl#>();
506 }
507 }
508
509 // shared_ptr is used here instead of unique_ptr, so ConnectionHandle instances can
510 // use a weak_ptr to check if the Signal::Impl they reference is still alive.
511 //
512 // This makes Signals easily copyable in theory, but the semantics of this are unclear.
513 // Copying could either simply copy the shared_ptr, which means the copy would share
514 // the connections of the original, which is possibly unintuitive, or the Impl would
515 // have to be copied as well.
516 // This would however leave connections without handles to disconnect them.
517 // So copying is forbidden for now.
518 //
519 // Think of this shared_ptr more like a unique_ptr with additional weak_ptr's
520 // in ConnectionHandle that can check whether the Impl object is still alive.
521 mutable stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<Implcxx . . $ KDBindings/Signal#Impl#> m_implcxx . . $ KDBindings/Signal#m_impl.;
522 };
523
524 /**
525 * @brief A ScopedConnection is a RAII-style way to make sure a Connection is disconnected.
526 *
527 * When the ScopedConnections scope ends, the connection this ScopedConnection guards will be disconnected.
528 *
529 * Example:
530 * - @ref 08-managing-connections/main.cpp
531 */
532 class ScopedConnectioncxx . . $ KDBindings/ScopedConnection#
533 {
534 public:
535 /**
536 * @brief A ScopedConnection can be default constructed
537 *
538 * A default constructed ScopedConnection has no connection to guard.
539 * Therefore it does nothing when it is destructed, unless a ConnectionHandle is assigned to it.
540 */
541 ScopedConnectioncxx . . $ KDBindings/ScopedConnection#ScopedConnection(49f6e7a06ebc5aa8).() = default;
542
543 /** A ScopedConnection can be move constructed */
544 ScopedConnectioncxx . . $ KDBindings/ScopedConnection#ScopedConnection(465a407022684f68).(ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &&) = default;
545
546 /** A ScopedConnection cannot be copied */
547 ScopedConnectioncxx . . $ KDBindings/ScopedConnection#ScopedConnection(88b2efde5a99fafa).(const ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &) = delete;
548 /** A ScopedConnection cannot be copied */
549 ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &operatorcxx . . $ KDBindings/ScopedConnection#`operator=`(72aad220c1148b9c).=(const ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &) = delete;
550
551 /** A ScopedConnection can be move assigned */
552 ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &operatorcxx . . $ KDBindings/ScopedConnection#`operator=`(95e7a44150b001c9).=(ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &&other)
553 {
554 m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection..disconnectcxx . . $ KDBindings/ConnectionHandle#disconnect(49f6e7a06ebc5aa8).();
555 m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection. =cxx . . $ KDBindings/ConnectionHandle#`operator=`(ff71c4ae9e774754). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(other.m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.);
556 return *this;
557 }
558
559 /**
560 * A ScopedConnection can be constructed from a ConnectionHandle
561 */
562 ScopedConnectioncxx . . $ KDBindings/ScopedConnection#ScopedConnection(a081369ef48cf2dd).(ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &&h)
563 : m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(h))
564 {
565 }
566
567 /**
568 * A ScopedConnection can be assigned from a ConnectionHandle
569 */
570 ScopedConnectioncxx . . $ KDBindings/ScopedConnection# &operatorcxx . . $ KDBindings/ScopedConnection#`operator=`(76658f53855e5032).=(ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &&h)
571 {
572 return *this =cxx . . $ KDBindings/ScopedConnection#`operator=`(95e7a44150b001c9). ScopedConnectioncxx . . $ KDBindings/ScopedConnection#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(h));
573 }
574
575 /**
576 * @return the handle to the connection this instance is managing
577 */
578 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &handlecxx . . $ KDBindings/ScopedConnection#handle(ed6135050f5ae2f1).()
579 {
580 return m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.;
581 }
582
583 /**
584 * @overload
585 */
586 const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &handlecxx . . $ KDBindings/ScopedConnection#handle(bc93e57aa9222ac0).() const
587 {
588 return m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.;
589 }
590
591 /**
592 * Convenience access to the underlying ConnectionHandle using the `->` operator.
593 */
594 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# *operatorcxx . . $ KDBindings/ScopedConnection#`operator->`(c5cafeb613368608).->()
595 {
596 return &m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.;
597 }
598
599 /**
600 * @overload
601 */
602 const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# *operatorcxx . . $ KDBindings/ScopedConnection#`operator->`(bb6e2e849e4fa8a4).->() const
603 {
604 return &m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.;
605 }
606
607 /**
608 * When a ConnectionHandle is destructed it disconnects the connection it guards.
609 */
610 ~cxx . . $ KDBindings/ScopedConnection#`~ScopedConnection`(ced63f7c635d850d).ScopedConnectioncxx . . $ KDBindings/ScopedConnection#()
611 {
612 m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection..disconnectcxx . . $ KDBindings/ConnectionHandle#disconnect(49f6e7a06ebc5aa8).();
613 }
614
615 private:
616 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# m_connectioncxx . . $ KDBindings/ScopedConnection#m_connection.;
617 };
618
619 /**
620 * @brief A ConnectionBlocker is a convenient RAII-style mechanism for temporarily blocking a connection.
621 *
622 * When a ConnectionBlocker is constructed, it will block the connection.
623 *
624 * When it is destructed, it will return the connection to the blocked state it was in
625 * before the ConnectionBlocker was constructed.
626 *
627 * Example:
628 * - @ref 08-managing-connections/main.cpp
629 */
630 class ConnectionBlockercxx . . $ KDBindings/ConnectionBlocker#
631 {
632 public:
633 /**
634 * Constructs a new ConnectionBlocker and blocks the connection this ConnectionHandle
635 * refers to.
636 *
637 * @throw std::out_of_range If the connection is not active (i.e. ConnectionHandle::isActive() returns false).
638 */
639 explicit ConnectionBlockercxx . . $ KDBindings/ConnectionBlocker#ConnectionBlocker(8702ac75773e8ce1).(const ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# &handle)
640 : m_handlecxx . . $ KDBindings/ConnectionHandle#ConnectionHandle(3ac1fd0ae8579b93).{ handle }
641 {
642 m_wasBlockedcxx . . $ KDBindings/ConnectionBlocker#m_wasBlocked. = m_handlecxx . . $ KDBindings/ConnectionBlocker#m_handle..blockcxx . . $ KDBindings/ConnectionHandle#block(efaadfb7dc14930a).(true);
643 }
644
645 /**
646 * Destructs the ConnectionBlocker and returns the connection into the blocked state it was in
647 * before the ConnectionBlocker was constructed.
648 */
649 ~cxx . . $ KDBindings/ConnectionBlocker#`~ConnectionBlocker`(ced63f7c635d850d).ConnectionBlockercxx . . $ KDBindings/ConnectionBlocker#()
650 {
651 m_handlecxx . . $ KDBindings/ConnectionBlocker#m_handle..blockcxx . . $ KDBindings/ConnectionHandle#block(efaadfb7dc14930a).(m_wasBlockedcxx . . $ KDBindings/ConnectionBlocker#m_wasBlocked.);
652 }
653
654 private:
655 ConnectionHandlecxx . . $ KDBindings/ConnectionHandle# m_handlecxx . . $ KDBindings/ConnectionBlocker#m_handle.;
656 bool m_wasBlockedcxx . . $ KDBindings/ConnectionBlocker#m_wasBlocked.{ false };
657 };
658
659 /**
660 * @example 01-simple-connection/main.cpp
661 *
662 * A simple example of how to create a KDBindings::Signal and connect a lambda to it.
663 *
664 * The output of this example is:
665 * ```
666 * The answer: 42
667 * ```
668 */
669
670 /**
671 * @example 02-signal-member/main.cpp
672 *
673 * An example of how to connect a member function to a KDBindings::Signal.
674 *
675 * The output of this example is:
676 * ```
677 * Hello World!
678 * ```
679 */
680
681 /**
682 * @example 03-member-arguments/main.cpp
683 *
684 * An example of how to connect a member function with arguments to a KDBindings::Signal.
685 *
686 * The output of this example is:
687 * ```
688 * Bob received: Have a nice day!
689 * Alice received: Thank you!
690 * ```
691 */
692
693 /**
694 * @example 07-advanced-connections/main.cpp
695 *
696 * An example of how to use the KDBindings::Signal::connect() overloaded function for advanced slot connections.
697 *
698 * The output of this example is:
699 * ```
700 * Hello World!
701 * Emitted value: 5
702 * true
703 * ```
704 */
705
706 /**
707 * @example 08-managing-connections/main.cpp
708 *
709 * An example of how to use a ScopedConnection and ConnectionBlocker to manage
710 * when a Connection is disconnected or blocked.
711 *
712 * Expected output:
713 * ```
714 * Guard is connected: 1
715 * Connection is not blocked: 3
716 * Connection is not blocked: 5
717 * ```
718 */
719
720 } // namespace KDBindings