1#ifndef ENTT_SIGNAL_SIGH_HPP
2#define ENTT_SIGNAL_SIGH_HPP
22template<
typename Type>
34template<
typename Type,
typename Allocator>
53template<
typename Ret,
typename... Args,
typename Allocator>
54class sigh<Ret(Args...), Allocator> {
55 friend class sink<sigh<Ret(Args...), Allocator>>;
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>>;
63 using allocator_type = Allocator;
65 using size_type = std::size_t;
67 using sink_type =
sink<sigh<Ret(Args...), Allocator>>;
70 sigh() noexcept(noexcept(allocator_type{}))
71 : sigh{allocator_type{}} {}
77 explicit sigh(
const allocator_type &allocator) noexcept
84 sigh(
const sigh &other)
85 : calls{other.calls} {}
92 sigh(
const sigh &other,
const allocator_type &allocator)
93 : calls{other.calls, allocator} {}
99 sigh(sigh &&other) noexcept
100 : calls{std::move(other.calls)} {}
107 sigh(sigh &&other,
const allocator_type &allocator)
108 : calls{std::move(other.calls), allocator} {}
118 sigh &operator=(
const sigh &other) {
128 sigh &operator=(sigh &&other)
noexcept {
137 void swap(sigh &other)
noexcept {
139 swap(calls, other.calls);
146 [[nodiscard]]
constexpr allocator_type get_allocator() const noexcept {
147 return calls.get_allocator();
154 [[nodiscard]] size_type size() const noexcept {
162 [[nodiscard]]
bool empty() const noexcept {
163 return calls.empty();
173 void publish(Args... args)
const {
174 for(
auto pos = calls.size(); pos; --pos) {
175 calls[pos - 1u](args...);
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...);
199 if constexpr(std::is_invocable_r_v<bool, Func>) {
207 if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
208 if(func(calls[pos - 1u](args...))) {
212 func(calls[pos - 1u](args...));
219 container_type calls;
234 : disconnect{fn}, signal{
ref} {}
245 [[nodiscard]]
explicit operator bool() const noexcept {
246 return static_cast<bool>(disconnect);
290 : conn{std::exchange(other.conn, {})} {}
309 conn = std::exchange(other.conn, {});
327 [[nodiscard]]
explicit operator bool() const noexcept {
328 return static_cast<bool>(conn);
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;
365 template<auto Cand
idate,
typename Type>
366 static void release(Type value_or_instance,
void *signal) {
367 sink{*
static_cast<signal_type *
>(signal)}.disconnect<Candidate>(value_or_instance);
370 template<auto Cand
idate>
371 static void release(
void *signal) {
372 sink{*
static_cast<signal_type *
>(signal)}.disconnect<Candidate>();
375 template<
typename Func>
376 void disconnect_if(Func callback) {
377 auto &
ref = signal_or_assert();
379 for(
auto pos =
ref.calls.size(); pos; --pos) {
380 if(
auto &elem =
ref.calls[pos - 1u]; callback(elem)) {
381 elem = std::move(
ref.calls.back());
382 ref.calls.pop_back();
387 [[nodiscard]]
auto &signal_or_assert() const noexcept {
388 ENTT_ASSERT(signal !=
nullptr,
"Invalid pointer to signal");
401 sink(sigh<Ret(Args...), Allocator> &
ref) noexcept
408 [[nodiscard]]
bool empty() const noexcept {
409 return signal_or_assert().calls.empty();
417 template<auto Cand
idate>
418 connection connect() {
419 disconnect<Candidate>();
421 delegate_type call{};
422 call.template connect<Candidate>();
423 signal_or_assert().calls.push_back(std::move(call));
426 conn.template connect<&release<Candidate>>();
427 return {conn, signal};
446 template<auto Cand
idate,
typename Type>
447 connection connect(Type &value_or_instance) {
448 disconnect<Candidate>(value_or_instance);
450 delegate_type call{};
451 call.template connect<Candidate>(value_or_instance);
452 signal_or_assert().calls.push_back(std::move(call));
455 conn.template connect<&release<Candidate, Type &>>(value_or_instance);
456 return {conn, signal};
470 template<auto Cand
idate,
typename Type>
471 connection connect(Type *value_or_instance) {
472 disconnect<Candidate>(value_or_instance);
474 delegate_type call{};
475 call.template connect<Candidate>(value_or_instance);
476 signal_or_assert().calls.push_back(std::move(call));
479 conn.template connect<&release<Candidate, Type *>>(value_or_instance);
480 return {conn, signal};
487 template<auto Cand
idate>
489 delegate_type call{};
490 call.template connect<Candidate>();
491 disconnect_if([&call](
const auto &elem) {
return elem == call; });
509 template<auto Cand
idate,
typename Type>
510 void disconnect(Type &value_or_instance) {
511 delegate_type call{};
512 call.template connect<Candidate>(value_or_instance);
513 disconnect_if([&call](
const auto &elem) {
return elem == call; });
526 template<auto Cand
idate,
typename Type>
527 void disconnect(Type *value_or_instance) {
528 delegate_type call{};
529 call.template connect<Candidate>(value_or_instance);
530 disconnect_if([&call](
const auto &elem) {
return elem == call; });
538 void disconnect(
const void *value_or_instance) {
539 ENTT_ASSERT(value_or_instance !=
nullptr,
"Invalid value or instance");
540 disconnect_if([value_or_instance](
const auto &elem) {
return elem.data() == value_or_instance; });
545 signal_or_assert().calls.clear();
552 [[nodiscard]]
explicit operator bool() const noexcept {
553 return signal !=
nullptr;
570template<
typename Ret,
typename... Args,
typename Allocator>
connection()
Default constructor.
void release()
Breaks the connection.
Basic delegate implementation.
Unmanaged signal handler.
constexpr void swap(compressed_pair< First, Second > &lhs, compressed_pair< First, Second > &rhs) noexcept
Swaps two compressed pair objects.
delegate(connect_arg_t< Candidate >) -> delegate< std::remove_pointer_t< internal::function_pointer_t< decltype(Candidate)> > >
Deduction guide.
@ ref
Aliasing mode, the object points to a non-const element.
@ empty
Default mode, the object does not own any elements.
sink(sigh< Ret(Args...), Allocator > &) -> sink< sigh< Ret(Args...), Allocator > >
Deduction guide.
scoped_connection()=default
Default constructor.
void release()
Breaks the connection.
scoped_connection(scoped_connection &&other) noexcept
Move constructor.
~scoped_connection()
Automatically breaks the link on destruction.
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.
scoped_connection(const scoped_connection &)=delete
Default copy constructor, deleted on purpose.
scoped_connection & operator=(connection other)
Acquires a connection.
scoped_connection & operator=(scoped_connection &&other) noexcept
Move assignment operator.