7#include "detail/SignalInternal.h"
24 explicit Connection(std::weak_ptr<detail::SharedConnectionState> state) noexcept
25 : m_state{std::move(state)}
39 bool IsConnected()
const noexcept
41 const auto state = m_state.lock();
42 return state && state->IsConnected();
45 bool Disconnect()
noexcept
47 auto state = m_state.lock();
48 return state && state->Disconnect();
52 std::weak_ptr<detail::SharedConnectionState> m_state;
58 static constexpr size_t Lowest = 0ull;
59 static constexpr size_t Highest = std::numeric_limits<size_t>::max();
63template<
class... Args>
76 [[nodiscard]]
auto Connect(std::move_only_function<
void(Args...)> func,
size_t priority = SignalPriority::Highest) ->
Connection
78 const auto pos = std::ranges::find_if(m_slots, [priority](
auto&& slot)
80 return priority >= slot.Priority();
83 if (pos != m_slots.cend())
85 auto result = m_slots.emplace(pos, std::move(func), m_link.get(), priority, ++m_currentId);
89 auto& result = m_slots.emplace_back(std::move(func), m_link.get(), priority, ++m_currentId);
95 [[nodiscard]]
auto Connect(T *inst,
void (T::*func)(Args...),
size_t priority = SignalPriority::Highest) ->
Connection
97 return Connect([=](Args... args){ (inst->*func)(args...); }, priority);
102 [[nodiscard]]
auto Connect(
const T *inst,
void (T::*func)(Args...)
const,
size_t priority = SignalPriority::Highest) ->
Connection
104 return Connect([=](Args... args){ (inst->*func)(args...); }, priority);
112 m_link->GetPendingDisconnections();
119 return m_slots.size();
126 for (
auto& slot : m_slots)
139 mutable std::unique_ptr<detail::ConnectionBacklink> m_link = std::make_unique<detail::ConnectionBacklink>();
140 mutable std::vector<Slot_t> m_slots{};
145 const auto pendingRemovals = m_link->GetPendingDisconnections();
146 for(
const auto id : pendingRemovals)
148 std::erase_if(m_slots, [
id](
const auto& state) {
return state.Id() ==
id; });
An RAII object managing a Signal connection.
Definition: Signal.h:22
An event source supporting multiple Connections.
Definition: Signal.h:65
void DisconnectAll()
Remove all connections.
Definition: Signal.h:108
auto ConnectionCount() const noexcept -> size_t
Get the number of active connections.
Definition: Signal.h:116
void Emit(Args... args)
Invoke all slots.
Definition: Signal.h:123
auto Connect(std::move_only_function< void(Args...)> func, size_t priority=SignalPriority::Highest) -> Connection
Connect a std::move_only_function.
Definition: Signal.h:76
void operator()(Args... args)
Invoke all slots.
Definition: Signal.h:133
auto Connect(T *inst, void(T::*func)(Args...), size_t priority=SignalPriority::Highest) -> Connection
Connect a member function.
Definition: Signal.h:95
auto Connect(const T *inst, void(T::*func)(Args...) const, size_t priority=SignalPriority::Highest) -> Connection
Connect a const member function.
Definition: Signal.h:102
Definition: SignalInternal.h:84
Priority values for controlling call order from a Signal.
Definition: Signal.h:57