Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
event_queue.hpp
Go to the documentation of this file.
1
4#pragma once
5
7
8#include <algorithm>
9#include <cstddef>
10#include <functional>
11#include <vector>
12
13namespace aegis2d::core {
14
31public:
35 template <typename Event>
36 void push(const Event& event, int priority = 0) {
37 entries_.push_back({priority, [event](EventBus& bus) { bus.publish(event); }});
38 }
39
44 void dispatch(EventBus& bus) {
45 std::ranges::stable_sort(
46 entries_, [](const Entry& a, const Entry& b) { return a.priority > b.priority; });
47 for (const Entry& entry : entries_) {
48 entry.fn(bus);
49 }
50 entries_.clear();
51 }
52
55 [[nodiscard]] std::size_t size() const noexcept { return entries_.size(); }
56
58 void clear() noexcept { entries_.clear(); }
59
60private:
61 struct Entry {
62 int priority;
63 std::function<void(EventBus&)> fn;
64 };
65
66 std::vector<Entry> entries_;
67};
68
69} // namespace aegis2d::core
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
Deferred event queue dispatched by priority at a chosen point in the tick.
Definition event_queue.hpp:30
void clear() noexcept
Discards all queued events without dispatching them.
Definition event_queue.hpp:58
std::size_t size() const noexcept
Number of events currently in the queue.
Definition event_queue.hpp:55
void push(const Event &event, int priority=0)
Queues an event for deferred dispatch.
Definition event_queue.hpp:36
void dispatch(EventBus &bus)
Dispatches all queued events in priority order and clears the queue.
Definition event_queue.hpp:44
Typed publish/subscribe event bus with immediate dispatch.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11