Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Signal.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
16
17#pragma once
18
19// =============================================================================
20// Includes
21// =============================================================================
22
23#include "Connection.hpp"
24#include "../core/Types.hpp"
25
26#include <functional>
27#include <unordered_map>
28#include <vector>
29#include <algorithm>
30
31namespace esengine {
32
33// Forward declaration
34template<typename Signature>
35class Signal;
36
37template<typename Signature>
38class Sink;
39
40// =============================================================================
41// Signal Class (void return)
42// =============================================================================
43
61template<typename... Args>
62class Signal<void(Args...)> {
63public:
64 using Callback = std::function<void(Args...)>;
65
66 Signal() = default;
67 ~Signal() = default;
68
69 Signal(Signal&&) noexcept = default;
70 Signal& operator=(Signal&&) noexcept = default;
71
72 Signal(const Signal&) = delete;
73 Signal& operator=(const Signal&) = delete;
74
79 void publish(Args... args) {
80 if (publishing_) {
81 for (const auto& [id, callback] : callbacks_) {
82 if (callback && !isPendingRemove(id)) {
83 callback(args...);
84 }
85 }
86 } else {
87 publishing_ = true;
88 for (const auto& [id, callback] : callbacks_) {
89 if (callback && !isPendingRemove(id)) {
90 callback(args...);
91 }
92 }
93 publishing_ = false;
94 processPendingOperations();
95 }
96 }
97
102 [[nodiscard]] usize size() const {
103 return callbacks_.size();
104 }
105
110 [[nodiscard]] bool empty() const {
111 return callbacks_.empty();
112 }
113
114private:
115 friend class Sink<Signal<void(Args...)>>;
116
122 CallbackId connect(Callback callback) {
123 CallbackId id = nextId_++;
124 if (publishing_) {
125 pendingAdds_.emplace_back(id, std::move(callback));
126 } else {
127 callbacks_.emplace(id, std::move(callback));
128 }
129 return id;
130 }
131
136 void disconnect(CallbackId id) {
137 if (publishing_) {
138 pendingRemoves_.push_back(id);
139 } else {
140 callbacks_.erase(id);
141 }
142 }
143
147 bool isPendingRemove(CallbackId id) const {
148 for (auto removedId : pendingRemoves_) {
149 if (removedId == id) return true;
150 }
151 return false;
152 }
153
154 void processPendingOperations() {
155 for (auto id : pendingRemoves_) {
156 callbacks_.erase(id);
157 }
158 pendingRemoves_.clear();
159
160 for (auto& [id, callback] : pendingAdds_) {
161 callbacks_.emplace(id, std::move(callback));
162 }
163 pendingAdds_.clear();
164 }
165
166 Shared<bool> alive_ = makeShared<bool>(true);
167 std::unordered_map<CallbackId, Callback> callbacks_;
168 std::vector<std::pair<CallbackId, Callback>> pendingAdds_;
169 std::vector<CallbackId> pendingRemoves_;
170 CallbackId nextId_ = 1;
171 bool publishing_ = false;
172};
173
184template<typename Ret, typename... Args>
185class Signal<Ret(Args...)> {
186public:
187 using Callback = std::function<Ret(Args...)>;
188
189 Signal() = default;
190 ~Signal() = default;
191
192 Signal(Signal&&) noexcept = default;
193 Signal& operator=(Signal&&) noexcept = default;
194
195 Signal(const Signal&) = delete;
196 Signal& operator=(const Signal&) = delete;
197
203 std::vector<Ret> publish(Args... args) {
204 std::vector<Ret> results;
205 results.reserve(callbacks_.size());
206
207 publishing_ = true;
208 for (const auto& [id, callback] : callbacks_) {
209 if (callback && !isPendingRemove(id)) {
210 results.push_back(callback(args...));
211 }
212 }
213 publishing_ = false;
214 processPendingOperations();
215
216 return results;
217 }
218
225 Ret publishFirst(Ret defaultValue, Args... args) {
226 publishing_ = true;
227 for (const auto& [id, callback] : callbacks_) {
228 if (callback && !isPendingRemove(id)) {
229 Ret result = callback(args...);
230 if (result != defaultValue) {
231 publishing_ = false;
232 processPendingOperations();
233 return result;
234 }
235 }
236 }
237 publishing_ = false;
238 processPendingOperations();
239 return defaultValue;
240 }
241
242 [[nodiscard]] usize size() const {
243 return callbacks_.size();
244 }
245
246 [[nodiscard]] bool empty() const {
247 return callbacks_.empty();
248 }
249
250private:
251 friend class Sink<Signal<Ret(Args...)>>;
252
253 CallbackId connect(Callback callback) {
254 CallbackId id = nextId_++;
255 if (publishing_) {
256 pendingAdds_.emplace_back(id, std::move(callback));
257 } else {
258 callbacks_.emplace(id, std::move(callback));
259 }
260 return id;
261 }
262
263 void disconnect(CallbackId id) {
264 if (publishing_) {
265 pendingRemoves_.push_back(id);
266 } else {
267 callbacks_.erase(id);
268 }
269 }
270
271 bool isPendingRemove(CallbackId id) const {
272 for (auto removedId : pendingRemoves_) {
273 if (removedId == id) return true;
274 }
275 return false;
276 }
277
278 void processPendingOperations() {
279 for (auto id : pendingRemoves_) {
280 callbacks_.erase(id);
281 }
282 pendingRemoves_.clear();
283
284 for (auto& [id, callback] : pendingAdds_) {
285 callbacks_.emplace(id, std::move(callback));
286 }
287 pendingAdds_.clear();
288 }
289
290 Shared<bool> alive_ = makeShared<bool>(true);
291 std::unordered_map<CallbackId, Callback> callbacks_;
292 std::vector<std::pair<CallbackId, Callback>> pendingAdds_;
293 std::vector<CallbackId> pendingRemoves_;
294 CallbackId nextId_ = 1;
295 bool publishing_ = false;
296};
297
298} // namespace esengine
RAII connection handle for event system.
Core type definitions and utilities for ESEngine.
std::function< Ret(Args...)> Callback
Definition Signal.hpp:187
Signal(Signal &&) noexcept=default
Ret publishFirst(Ret defaultValue, Args... args)
Publish and return first non-default result.
Definition Signal.hpp:225
std::vector< Ret > publish(Args... args)
Publish an event and collect results.
Definition Signal.hpp:203
Signal(Signal &&) noexcept=default
bool empty() const
Check if there are any subscribers.
Definition Signal.hpp:110
void publish(Args... args)
Publish an event to all subscribers.
Definition Signal.hpp:79
usize size() const
Get the number of active subscribers.
Definition Signal.hpp:102
std::function< void(Args...)> Callback
Definition Signal.hpp:64
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::shared_ptr< T > Shared
Shared ownership smart pointer.
Definition Types.hpp:82
u64 CallbackId
constexpr Shared< T > makeShared(Args &&... args)
Creates a Shared pointer with the given arguments.
Definition Types.hpp:111