Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
EventBus.hpp
Go to the documentation of this file.
1
7#pragma once
8
14
15#include <utility>
16
17namespace corium {
18
27template <
28 typename EventVariantType = DefaultEvents,
29 typename QueuePolicy = BoundedMpscQueuePolicy<EventVariantType, 1024>,
30 typename SignalPolicy = NoSignalPolicy,
31 typename StoragePolicy = DefaultStoragePolicy,
32 typename OverflowPolicy = DropNewestOverflowPolicy,
33 typename ProfilerPolicy = profiler::NullProfiler
34>
36public:
37 using EventVariant = EventVariantType;
39 using ProfilerPolicyType = ProfilerPolicy;
40
41 BasicEventBus() = default;
42
49 {
50 _profilerPolicy.onEventPosted(event, static_cast<uint8_t>(priority));
51 _profilerPolicy.recordPostTime(_profilerPolicy.nowNs());
52 _eventQueue.pushEvent(std::move(event), priority);
53 }
54
60 {
61 _profilerPolicy.onEventPosted(event, static_cast<uint8_t>(priority));
62 _profilerPolicy.recordPostTime(_profilerPolicy.nowNs());
63 _eventQueue.pushEvent(event, priority);
64 }
65
70 template <typename Event>
71 void postHighPriority(Event&& event)
72 {
73 post(EventVariant(std::forward<Event>(event)), EventPriority::High);
74 }
75
79 {
80 EventVariant event;
81 if (!_eventQueue.tryPopEvent(event)) {
82 return false;
83 }
84 const uint64_t postTime = _profilerPolicy.takePostTime();
85 const uint64_t dispatchTime = _profilerPolicy.nowNs();
86 _reactor.dispatch(event);
87 const uint64_t finishTime = _profilerPolicy.nowNs();
88 _profilerPolicy.onEventDispatched(event, 0, postTime, dispatchTime, finishTime);
89 return true;
90 }
91
95 std::size_t processBatch(std::size_t maxBatch)
96 {
97 std::size_t count = 0;
98 EventVariant event;
99 while (count < maxBatch) {
100 if (!_eventQueue.tryPopEvent(event)) {
101 break;
102 }
103 const uint64_t postTime = _profilerPolicy.takePostTime();
104 const uint64_t dispatchTime = _profilerPolicy.nowNs();
105 _reactor.dispatch(event);
106 const uint64_t finishTime = _profilerPolicy.nowNs();
107 _profilerPolicy.onEventDispatched(event, 0, postTime, dispatchTime, finishTime);
108 count++;
109 }
110 return count;
111 }
112
115 std::size_t drain()
116 {
117 std::size_t total = 0;
118 while (processOne()) {
119 total++;
120 }
121 return total;
122 }
123
125 [[nodiscard]] ProfilerPolicy& profiler() noexcept
126 {
127 return _profilerPolicy;
128 }
129
131 [[nodiscard]] const ProfilerPolicy& profiler() const noexcept
132 {
133 return _profilerPolicy;
134 }
135
137 [[nodiscard]] bool empty() const
138 {
139 return _eventQueue.empty();
140 }
141
143 void seal()
144 {
145 _reactor.seal();
146 }
147
150 {
151 _eventQueue.setOnQueueNonEmpty(callback);
152 }
153
158 template <typename EventType, typename Handler>
159 bool registerHandler(Handler&& handler)
160 {
161 return _reactor.template registerHandler<EventType>(std::forward<Handler>(handler));
162 }
163
167 template <typename Handler>
168 bool registerHandler(Handler&& handler)
169 {
170 using EventType = callable_event_type_t<Handler>;
171 return _reactor.template registerHandler<EventType>(std::forward<Handler>(handler));
172 }
173
175 [[nodiscard]] SignalPolicy& signalPolicy() noexcept
176 {
177 return _eventQueue.signalPolicy();
178 }
179
181 [[nodiscard]] const SignalPolicy& signalPolicy() const noexcept
182 {
183 return _eventQueue.signalPolicy();
184 }
185
187 [[nodiscard]] OverflowPolicy& overflowPolicy() noexcept
188 {
189 return _eventQueue.overflowPolicy();
190 }
191
193 [[nodiscard]] const OverflowPolicy& overflowPolicy() const noexcept
194 {
195 return _eventQueue.overflowPolicy();
196 }
197
200 {
201 return _reactor;
202 }
203
206 {
207 return EventSinkT<EventVariant>(*this);
208 }
209
210private:
212 ReactorType _reactor;
213 ProfilerPolicy _profilerPolicy{};
214};
215
218
220template <
221 typename EventVariantType = DefaultEvents,
223 typename SignalPolicy = NoSignalPolicy,
224 typename StoragePolicy = DefaultStoragePolicy,
225 typename OverflowPolicy = DropNewestOverflowPolicy,
226 typename ProfilerPolicy = profiler::NullProfiler
227>
229
230} // namespace corium
Compile-time introspection traits for callable objects and event handlers.
Internal priority and bounded lock-free event queue adapter.
Non-allocating type-erased fat pointer handle for lock-free event posting.
Latency tracking and flight recording policies with runtime toggle.
Internal static event handler registry and compile-time dispatcher.
Policy-configurable non-virtual event bus implementation.
Definition EventBus.hpp:35
bool empty() const
Check if event queue is empty.
Definition EventBus.hpp:137
const OverflowPolicy & overflowPolicy() const noexcept
Access const reference to overflow policy.
Definition EventBus.hpp:193
OverflowPolicy & overflowPolicy() noexcept
Access reference to overflow policy.
Definition EventBus.hpp:187
std::size_t drain()
Drain and dispatch all currently enqueued events.
Definition EventBus.hpp:115
void post(EventVariant &&event, EventPriority priority=EventPriority::Normal)
Post an event into the queue with optional priority (rvalue overload).
Definition EventBus.hpp:48
void setOnQueueNonEmpty(StaticCallback callback)
Set static callback for event availability when queue transitions to non-empty.
Definition EventBus.hpp:149
void post(const EventVariant &event, EventPriority priority=EventPriority::Normal)
Post an event into the queue with optional priority (const lvalue overload).
Definition EventBus.hpp:59
EventSinkT< EventVariant > sink() noexcept
Get an EventSink handle pointing to this event bus.
Definition EventBus.hpp:205
ReactorType & reactor() noexcept
Access reference to reactor.
Definition EventBus.hpp:199
BasicReactor< EventVariant, StoragePolicy > ReactorType
Definition EventBus.hpp:38
bool processOne()
Process a single event from the queue.
Definition EventBus.hpp:78
bool registerHandler(Handler &&handler)
Register an event handler with explicit event type parameter.
Definition EventBus.hpp:159
EventVariantType EventVariant
Definition EventBus.hpp:37
const SignalPolicy & signalPolicy() const noexcept
Access const reference to signal policy.
Definition EventBus.hpp:181
void seal()
Seal reactor handlers.
Definition EventBus.hpp:143
ProfilerPolicy & profiler() noexcept
Access reference to profiler policy.
Definition EventBus.hpp:125
ProfilerPolicy ProfilerPolicyType
Definition EventBus.hpp:39
const ProfilerPolicy & profiler() const noexcept
Access const reference to profiler policy.
Definition EventBus.hpp:131
bool registerHandler(Handler &&handler)
Register an event handler with automatic event type deduction.
Definition EventBus.hpp:168
SignalPolicy & signalPolicy() noexcept
Access reference to signal policy.
Definition EventBus.hpp:175
std::size_t processBatch(std::size_t maxBatch)
Process up to maxBatch events consecutively from the queue.
Definition EventBus.hpp:95
void postHighPriority(Event &&event)
Convenience helper for posting high-priority events.
Definition EventBus.hpp:71
Primary template declaration for BasicReactor.
Definition Reactor.hpp:65
Queue Policy for fixed-capacity, zero-allocation lock-free MPSC RingBuffer.
Definition QueuePolicies.hpp:43
Signal Policy for busy-spin / polling event loops (sub-microsecond latency, zero signaling cost).
Definition SignalPolicies.hpp:72
Event queue composing QueuePolicy, SignalPolicy, and OverflowPolicy strategy types.
Definition EventQueue.hpp:29
Definition Application.hpp:16
std::variant< QuitEvent, TickEvent, UpdateEvent, ErrorEvent, SignalEvent > DefaultEvents
Default variant list of core Corium events.
Definition Events.hpp:62
EventPriority
Event priority levels for multi-priority queue policies.
Definition QueuePolicies.hpp:32
DefaultEvents Event
Alias for DefaultEvents.
Definition Events.hpp:65
FixedStoragePolicy< 8, 32 > DefaultStoragePolicy
Default storage policy (8 handlers per event type, 32 bytes inline delegate storage).
Definition StoragePolicies.hpp:24
Default Overflow Policy: Silently drop incoming new event when queue is full. Zero overhead.
Definition OverflowPolicies.hpp:20
Lightweight non-allocating static callback wrapper (function pointer + optional context argument).
Definition SignalPolicies.hpp:35
Default Profiler Policy: Zero-overhead, completely compiled out by inline empty functions.
Definition ProfilerPolicies.hpp:21