EnTT 3.14.0
Loading...
Searching...
No Matches
sigh.hpp
1#ifndef ENTT_SIGNAL_SIGH_HPP
2#define ENTT_SIGNAL_SIGH_HPP
3
4#include <cstddef>
5#include <memory>
6#include <type_traits>
7#include <utility>
8#include <vector>
9#include "delegate.hpp"
10#include "fwd.hpp"
11
12namespace entt {
13
22template<typename Type>
23class sink;
24
34template<typename Type, typename Allocator>
35class sigh;
36
53template<typename Ret, typename... Args, typename Allocator>
54class sigh<Ret(Args...), Allocator> {
55 friend class sink<sigh<Ret(Args...), Allocator>>;
56
57 using alloc_traits = std::allocator_traits<Allocator>;
58 using delegate_type = delegate<Ret(Args...)>;
59 using container_type = std::vector<delegate_type, typename alloc_traits::template rebind_alloc<delegate_type>>;
60
61public:
63 using allocator_type = Allocator;
65 using size_type = std::size_t;
67 using sink_type = sink<sigh<Ret(Args...), Allocator>>;
68
71 : sigh{allocator_type{}} {}
72
78 : calls{allocator} {}
79
84 sigh(const sigh &other)
85 : calls{other.calls} {}
86
93 : calls{other.calls, allocator} {}
94
100 : calls{std::move(other.calls)} {}
101
108 : calls{std::move(other.calls), allocator} {}
109
111 ~sigh() = default;
112
119 calls = other.calls;
120 return *this;
121 }
122
128 sigh &operator=(sigh &&other) noexcept {
129 swap(other);
130 return *this;
131 }
132
137 void swap(sigh &other) noexcept {
138 using std::swap;
139 swap(calls, other.calls);
140 }
141
147 return calls.get_allocator();
148 }
149
155 return calls.size();
156 }
157
163 return calls.empty();
164 }
165
173 void publish(Args... args) const {
174 for(auto pos = calls.size(); pos; --pos) {
175 calls[pos - 1u](args...);
176 }
177 }
178
193 template<typename Func>
194 void collect(Func func, Args... args) const {
195 for(auto pos = calls.size(); pos; --pos) {
196 if constexpr(std::is_void_v<Ret> || !std::is_invocable_v<Func, Ret>) {
197 calls[pos - 1u](args...);
198
199 if constexpr(std::is_invocable_r_v<bool, Func>) {
200 if(func()) {
201 break;
202 }
203 } else {
204 func();
205 }
206 } else {
207 if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
208 if(func(calls[pos - 1u](args...))) {
209 break;
210 }
211 } else {
212 func(calls[pos - 1u](args...));
213 }
214 }
215 }
216 }
217
218private:
219 container_type calls;
220};
221
230 template<typename>
231 friend class sink;
232
233 connection(delegate<void(void *)> fn, void *ref)
234 : disconnect{fn}, signal{ref} {}
235
236public:
239 : signal{} {}
240
245 [[nodiscard]] explicit operator bool() const noexcept {
246 return static_cast<bool>(disconnect);
247 }
248
250 void release() {
251 if(disconnect) {
252 disconnect(signal);
253 disconnect.reset();
254 }
255 }
256
257private:
258 delegate<void(void *)> disconnect;
259 void *signal;
260};
261
273 scoped_connection() = default;
274
280 : conn{other} {}
281
284
290 : conn{std::exchange(other.conn, {})} {}
291
294 conn.release();
295 }
296
302
309 conn = std::exchange(other.conn, {});
310 return *this;
311 }
312
319 conn = other;
320 return *this;
321 }
322
327 [[nodiscard]] explicit operator bool() const noexcept {
328 return static_cast<bool>(conn);
329 }
330
332 void release() {
333 conn.release();
334 }
335
336private:
337 connection conn;
338};
339
359template<typename Ret, typename... Args, typename Allocator>
360class sink<sigh<Ret(Args...), Allocator>> {
361 using signal_type = sigh<Ret(Args...), Allocator>;
362 using delegate_type = typename signal_type::delegate_type;
363 using difference_type = typename signal_type::container_type::difference_type;
364
365 template<auto Candidate, typename Type>
366 static void release(Type value_or_instance, void *signal) {
367 sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
368 }
369
370 template<auto Candidate>
371 static void release(void *signal) {
372 sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>();
373 }
374
375 template<typename Func>
376 void disconnect_if(Func callback) {
377 for(auto pos = signal->calls.size(); pos; --pos) {
378 if(auto &elem = signal->calls[pos - 1u]; callback(elem)) {
379 elem = std::move(signal->calls.back());
380 signal->calls.pop_back();
381 }
382 }
383 }
384
385public:
390 sink(sigh<Ret(Args...), Allocator> &ref) noexcept
391 : signal{&ref} {}
392
398 return signal->calls.empty();
399 }
400
409 template<auto Candidate, typename... Type>
412
413 delegate_type call{};
415 signal->calls.push_back(std::move(call));
416
417 delegate<void(void *)> conn{};
418 conn.template connect<&release<Candidate, Type...>>(value_or_instance...);
419 return {conn, signal};
420 }
421
429 template<auto Candidate, typename... Type>
431 delegate_type call{};
433 disconnect_if([&call](const auto &elem) { return elem == call; });
434 }
435
441 void disconnect(const void *value_or_instance) {
442 ENTT_ASSERT(value_or_instance != nullptr, "Invalid value or instance");
443 disconnect_if([value_or_instance](const auto &elem) { return elem.data() == value_or_instance; });
444 }
445
447 void disconnect() {
448 signal->calls.clear();
449 }
450
451private:
452 signal_type *signal;
453};
454
465template<typename Ret, typename... Args, typename Allocator>
466sink(sigh<Ret(Args...), Allocator> &) -> sink<sigh<Ret(Args...), Allocator>>;
467
468} // namespace entt
469
470#endif
Connection class.
Definition sigh.hpp:229
connection()
Default constructor.
Definition sigh.hpp:238
void release()
Breaks the connection.
Definition sigh.hpp:250
Utility class to use to send around functions and members.
Definition delegate.hpp:66
Basic delegate implementation.
Definition delegate.hpp:51
Unmanaged signal handler.
Definition sigh.hpp:54
void publish(Args... args) const
Triggers a signal.
Definition sigh.hpp:173
Allocator allocator_type
Allocator type.
Definition sigh.hpp:63
sigh() noexcept(noexcept(allocator_type{}))
Default constructor.
Definition sigh.hpp:70
sigh(const sigh &other)
Copy constructor.
Definition sigh.hpp:84
~sigh()=default
Default destructor.
sigh & operator=(sigh &&other) noexcept
Move assignment operator.
Definition sigh.hpp:128
void swap(sigh &other) noexcept
Exchanges the contents with those of a given signal handler.
Definition sigh.hpp:137
size_type size() const noexcept
Number of listeners connected to the signal.
Definition sigh.hpp:154
sigh(sigh &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition sigh.hpp:107
sigh(sigh &&other) noexcept
Move constructor.
Definition sigh.hpp:99
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition sigh.hpp:146
sigh(const allocator_type &allocator) noexcept
Constructs a signal handler with a given allocator.
Definition sigh.hpp:77
bool empty() const noexcept
Returns false if at least a listener is connected to the signal.
Definition sigh.hpp:162
std::size_t size_type
Unsigned integer type.
Definition sigh.hpp:65
sigh & operator=(const sigh &other)
Copy assignment operator.
Definition sigh.hpp:118
sigh(const sigh &other, const allocator_type &allocator)
Allocator-extended copy constructor.
Definition sigh.hpp:92
void collect(Func func, Args... args) const
Collects return values from the listeners.
Definition sigh.hpp:194
Unmanaged signal handler.
Definition fwd.hpp:25
void disconnect(const void *value_or_instance)
Disconnects free functions with payload or bound members from a signal.
Definition sigh.hpp:441
sink(sigh< Ret(Args...), Allocator > &ref) noexcept
Constructs a sink that is allowed to modify a given signal.
Definition sigh.hpp:390
bool empty() const noexcept
Returns false if at least a listener is connected to the sink.
Definition sigh.hpp:397
void disconnect()
Disconnects all the listeners from a signal.
Definition sigh.hpp:447
connection connect(Type &&...value_or_instance)
Connects a free function (with or without payload), a bound or an unbound member to a signal.
Definition sigh.hpp:410
void disconnect(Type &&...value_or_instance)
Disconnects a free function (with or without payload), a bound or an unbound member from a signal.
Definition sigh.hpp:430
Sink class.
Definition fwd.hpp:22
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
constexpr void swap(compressed_pair< First, Second > &lhs, compressed_pair< First, Second > &rhs)
Swaps two compressed pair objects.
@ ref
Aliasing mode, the object points to a non-const element.
sink(sigh< Ret(Args...), Allocator > &) -> sink< sigh< Ret(Args...), Allocator > >
Deduction guide.
Scoped connection class.
Definition sigh.hpp:271
scoped_connection()=default
Default constructor.
void release()
Breaks the connection.
Definition sigh.hpp:332
scoped_connection(scoped_connection &&other) noexcept
Move constructor.
Definition sigh.hpp:289
~scoped_connection()
Automatically breaks the link on destruction.
Definition sigh.hpp:293
scoped_connection & operator=(const scoped_connection &)=delete
Default copy assignment operator, deleted on purpose.
scoped_connection(const connection &other)
Constructs a scoped connection from a basic connection.
Definition sigh.hpp:279
scoped_connection(const scoped_connection &)=delete
Default copy constructor, deleted on purpose.
scoped_connection & operator=(connection other)
Acquires a connection.
Definition sigh.hpp:318
scoped_connection & operator=(scoped_connection &&other) noexcept
Move assignment operator.
Definition sigh.hpp:308