53 other.disconnect_ = {};
64 other.disconnect_ = {};
99template <
typename... Args>
class Event
121 std::lock_guard<std::recursive_mutex> lock(*
mutex_);
125 listeners_.push_back({id, std::move(callback)});
135 if (!alive.expired()) this->unsubscribe(
id);
149 std::lock_guard<std::recursive_mutex> lock(*
mutex_);
154 for (
auto &listener : listeners)
156 listener.callback(args...);
169 std::lock_guard<std::recursive_mutex> lock(*
mutex_);
174 { return listener.id == id; }),
183 std::unique_ptr<std::recursive_mutex>
mutex_ =
184 std::make_unique<std::recursive_mutex>();
Manages the lifetime of a subscription to an Event.
Definition event.hpp:30
EventConnection & operator=(EventConnection &&other) noexcept
Move assignment operator.
Definition event.hpp:57
std::function< void()> disconnect_
Definition event.hpp:85
~EventConnection()
Destructor automatically triggers the disconnect action.
Definition event.hpp:44
EventConnection & operator=(const EventConnection &)=delete
EventConnection(EventConnection &&other) noexcept
Move constructor.
Definition event.hpp:50
EventConnection(std::function< void()> disconnect)
Construct a connection with a custom disconnect callback.
Definition event.hpp:38
void disconnect()
Manually disconnect the subscription.
Definition event.hpp:75
EventConnection(const EventConnection &)=delete
EventConnection()=default
Thread-safe publisher-subscriber event stream.
Definition event.hpp:100
void notify(Args... args)
Notify all subscribers with the given arguments.
Definition event.hpp:145
EventConnection subscribe(callback_t callback)
Subscribe to the event.
Definition event.hpp:119
std::function< void(Args...)> callback_t
Definition event.hpp:102
Event & operator=(const Event &)=delete
void unsubscribe(listener_id_t id)
Definition event.hpp:167
Event(const Event &)=delete
Event(Event &&) noexcept=default
std::size_t listener_id_t
Definition event.hpp:103
std::vector< Listener > listeners_
Definition event.hpp:178
std::unique_ptr< std::recursive_mutex > mutex_
Definition event.hpp:183
std::shared_ptr< void > alive_token_
Definition event.hpp:189
listener_id_t next_id_
Definition event.hpp:180
callback_t callback
Definition event.hpp:164
listener_id_t id
Definition event.hpp:163