27#include <unordered_map>
34template<
typename Signature>
37template<
typename Signature>
61template<
typename... Args>
81 for (
const auto& [
id, callback] : callbacks_) {
82 if (callback && !isPendingRemove(
id)) {
88 for (
const auto& [
id, callback] : callbacks_) {
89 if (callback && !isPendingRemove(
id)) {
94 processPendingOperations();
103 return callbacks_.size();
111 return callbacks_.empty();
125 pendingAdds_.emplace_back(
id, std::move(callback));
127 callbacks_.emplace(
id, std::move(callback));
138 pendingRemoves_.push_back(
id);
140 callbacks_.erase(
id);
148 for (
auto removedId : pendingRemoves_) {
149 if (removedId ==
id)
return true;
154 void processPendingOperations() {
155 for (
auto id : pendingRemoves_) {
156 callbacks_.erase(
id);
158 pendingRemoves_.clear();
160 for (
auto& [
id, callback] : pendingAdds_) {
161 callbacks_.emplace(
id, std::move(callback));
163 pendingAdds_.clear();
167 std::unordered_map<CallbackId, Callback> callbacks_;
168 std::vector<std::pair<CallbackId, Callback>> pendingAdds_;
169 std::vector<CallbackId> pendingRemoves_;
171 bool publishing_ =
false;
184template<
typename Ret,
typename... Args>
204 std::vector<Ret> results;
205 results.reserve(callbacks_.size());
208 for (
const auto& [
id, callback] : callbacks_) {
209 if (callback && !isPendingRemove(
id)) {
210 results.push_back(callback(args...));
214 processPendingOperations();
227 for (
const auto& [
id, callback] : callbacks_) {
228 if (callback && !isPendingRemove(
id)) {
229 Ret result = callback(args...);
230 if (result != defaultValue) {
232 processPendingOperations();
238 processPendingOperations();
243 return callbacks_.size();
247 return callbacks_.empty();
256 pendingAdds_.emplace_back(
id, std::move(callback));
258 callbacks_.emplace(
id, std::move(callback));
265 pendingRemoves_.push_back(
id);
267 callbacks_.erase(
id);
272 for (
auto removedId : pendingRemoves_) {
273 if (removedId ==
id)
return true;
278 void processPendingOperations() {
279 for (
auto id : pendingRemoves_) {
280 callbacks_.erase(
id);
282 pendingRemoves_.clear();
284 for (
auto& [
id, callback] : pendingAdds_) {
285 callbacks_.emplace(
id, std::move(callback));
287 pendingAdds_.clear();
291 std::unordered_map<CallbackId, Callback> callbacks_;
292 std::vector<std::pair<CallbackId, Callback>> pendingAdds_;
293 std::vector<CallbackId> pendingRemoves_;
295 bool publishing_ =
false;
RAII connection handle for event system.
Core type definitions and utilities for ESEngine.
std::function< Ret(Args...)> Callback
Signal(Signal &&) noexcept=default
Ret publishFirst(Ret defaultValue, Args... args)
Publish and return first non-default result.
std::vector< Ret > publish(Args... args)
Publish an event and collect results.
Signal(Signal &&) noexcept=default
bool empty() const
Check if there are any subscribers.
void publish(Args... args)
Publish an event to all subscribers.
usize size() const
Get the number of active subscribers.
std::function< void(Args...)> Callback
std::size_t usize
Size type for indexing and sizes.
std::shared_ptr< T > Shared
Shared ownership smart pointer.
constexpr Shared< T > makeShared(Args &&... args)
Creates a Shared pointer with the given arguments.