11#include "signal_types.hpp"
34template<
typename... Args>
36 static_assert(
sizeof...(Args) <= 4,
"signals::Signal supports at most four arguments");
38 using callback_t = std::function<void(Args...)>;
42 std::string identifier;
43 ConnectionSource source = ConnectionSource::unknown;
44 bool forwards_args =
true;
46 toast::Box<toast::Node> node;
50 using ConnectionList = std::vector<Connection>;
57 mutable std::mutex m_mutex;
58 std::shared_ptr<const ConnectionList> m_connections = std::make_shared<const ConnectionList>();
61 auto snapshot() const -> std::shared_ptr<const ConnectionList> {
62 std::scoped_lock lock(m_mutex);
66 template<
typename Edit>
67 void modify(Edit&& edit) {
68 std::scoped_lock lock(m_mutex);
69 auto copy = std::make_shared<ConnectionList>(*m_connections);
71 m_connections = std::move(copy);
77 Signal(
const Signal& other) : m_connections(other.snapshot()) { }
79 Signal(Signal&& other) noexcept : m_connections(other.snapshot()) { }
81 auto operator=(
const Signal& other) -> Signal& {
83 auto other_connections = other.snapshot();
84 std::scoped_lock lock(m_mutex);
85 m_connections = std::move(other_connections);
90 auto operator=(Signal&& other)
noexcept -> Signal& {
return *
this = other; }
95 requires(std::is_invocable_r_v<void, F, Args...> || std::is_invocable_r_v<void, F>)
96 void connect(toast::Node& node, F&& cb);
99 toast::Node& node, std::string_view identifier, ConnectionSource source = ConnectionSource::cpp,
bool forwards_args =
true
102 void disconnect(toast::Node& node, std::string_view identifier, ConnectionSource source = ConnectionSource::cpp);
104 void clear(ConnectionSource source);
107 auto connections() const -> std::vector<ConnectionInfo>;
109 void fire(const Args&... args);
111 template<typename NodeType, auto MemberPtr>
112 static auto get(
void* signal) -> std::vector<ConnectionInfo>;
114 template<typename NodeType, auto MemberPtr>
116 connect(
void* signal, toast::Node& target, std::string_view identifier, ConnectionSource source,
bool forwards_args);
118 template<typename NodeType, auto MemberPtr>
119 static
void disconnect(
void* signal, toast::Node& target, std::string_view identifier, ConnectionSource source);
121 template<typename NodeType, auto MemberPtr>
122 static
void clear(
void* signal, ConnectionSource source);
124 template<typename NodeType, auto MemberPtr>
125 static auto fire(
void* signal, std::span<const std::any> args) ->
bool;
130#include <toast/events/signals.inl>
pseudo shared pointer for Nodes
Unique identifier for everything that exists in our engine.