libnick 2024.9.2
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
1
23#ifndef EVENT_H
24#define EVENT_H
25
26#include <algorithm>
27#include <functional>
28#include <mutex>
29#include <type_traits>
30#include <vector>
31#include "eventargs.h"
32
34{
35 template<typename T>
36 concept DerivedEventArgs = std::is_base_of_v<EventArgs, T>;
37
41 enum class HandlerId : size_t {};
42
47 template <DerivedEventArgs T>
48 class Event
49 {
50 public:
54 Event() = default;
59 Event(const Event& e)
60 {
61 std::lock_guard<std::mutex> lock{ e.m_mutex };
62 m_handlers = e.m_handlers;
63 }
68 Event(Event&& e) noexcept
69 {
70 std::lock_guard<std::mutex> lock{ e.m_mutex };
71 m_handlers = std::move(e.m_handlers);
72 }
77 size_t count() const
78 {
79 std::lock_guard<std::mutex> lock{ m_mutex };
80 return m_handlers.size();
81 }
87 HandlerId subscribe(const std::function<void(const T&)>& handler)
88 {
89 std::lock_guard<std::mutex> lock{ m_mutex };
90 m_handlers.push_back(handler);
91 return HandlerId{ m_handlers.size() - 1 };
92 }
98 {
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())
101 {
102 return;
103 }
104 m_handlers.erase(m_handlers.begin() + static_cast<size_t>(id));
105 }
110 void invoke(const T& param) const
111 {
112 std::lock_guard<std::mutex> lock{ m_mutex };
113 for (const std::function<void(const T&)>& handler : m_handlers)
114 {
115 handler(param);
116 }
117 }
123 HandlerId operator+=(const std::function<void(const T&)>& handler)
124 {
125 return subscribe(handler);
126 }
132 {
133 unsubscribe(id);
134 }
139 void operator()(const T& param)
140 {
141 invoke(param);
142 }
149 {
150 if (this != &e)
151 {
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;
155 }
156 return *this;
157 }
163 Event& operator=(Event&& e) noexcept
164 {
165 if (this != &e)
166 {
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);
170 }
171 return *this;
172 }
177 operator bool() const
178 {
179 return count() > 0;
180 }
181
182 private:
183 mutable std::mutex m_mutex;
184 std::vector<std::function<void(const T&)>> m_handlers;
185 };
186}
187
188#endif //EVENT_H
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 &param)
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 &param) 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
Definition event.h:34
HandlerId
The ID of a handler for an event.
Definition event.h:41