47 template <DerivedEventArgs T>
61 std::lock_guard<std::mutex> lock{ e.m_mutex };
62 m_handlers = e.m_handlers;
70 std::lock_guard<std::mutex> lock{ e.m_mutex };
71 m_handlers = std::move(e.m_handlers);
79 std::lock_guard<std::mutex> lock{ m_mutex };
80 return m_handlers.size();
89 std::lock_guard<std::mutex> lock{ m_mutex };
90 m_handlers.push_back(handler);
91 return HandlerId{ m_handlers.size() - 1 };
99 std::lock_guard<std::mutex> lock{ m_mutex };
100 if (
static_cast<size_t>(
id) < 0 ||
static_cast<size_t>(
id) >= m_handlers.size())
104 m_handlers.erase(m_handlers.begin() +
static_cast<size_t>(
id));
112 std::lock_guard<std::mutex> lock{ m_mutex };
113 for (
const std::function<
void(
const T&)>& handler : m_handlers)
152 std::lock_guard<std::mutex> lock{ m_mutex };
153 std::lock_guard<std::mutex> lock2{ e.m_mutex };
154 m_handlers = e.m_handlers;
167 std::lock_guard<std::mutex> lock{ m_mutex };
168 std::lock_guard<std::mutex> lock2{ e.m_mutex };
169 m_handlers = std::move(e.m_handlers);
177 operator bool()
const
183 mutable std::mutex m_mutex;
184 std::vector<std::function<void(
const T&)>> m_handlers;
An event that can have handlers subscribe to it, which in turn will be called when the event is invok...
Definition event.h:49
Event()=default
Constructs an Event.
Event(Event &&e) noexcept
Constructs an Event via move.
Definition event.h:68
HandlerId operator+=(const std::function< void(const T &)> &handler)
Subscribes a handler to the event.
Definition event.h:123
Event & operator=(const Event &e)
Copies an Event.
Definition event.h:148
void operator()(const T ¶m)
Invokes the event, calling all handlers.
Definition event.h:139
size_t count() const
Gets the number of handlers subscribed to the event.
Definition event.h:77
HandlerId subscribe(const std::function< void(const T &)> &handler)
Subscribes a handler to the event.
Definition event.h:87
Event & operator=(Event &&e) noexcept
Moves an Event.
Definition event.h:163
Event(const Event &e)
Constructs an Event via copy.
Definition event.h:59
void invoke(const T ¶m) const
Invokes the event, calling all handlers.
Definition event.h:110
void unsubscribe(HandlerId id)
Unsubscribes a handler from the event.
Definition event.h:97
void operator-=(HandlerId id)
Unsubscribes a handler from the event.
Definition event.h:131
HandlerId
The ID of a handler for an event.
Definition event.h:41