Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
Reactor.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <cassert>
11#include <cstddef>
12#include <tuple>
13#include <type_traits>
14#include <utility>
15#include <variant>
16
17#include "corium/Events.hpp"
23
24namespace corium {
25namespace internal {
26
31template <typename EventType, size_t MaxHandlers, size_t InlineSize>
33public:
34 FixedHandlerList() = default;
35
36 template <typename Handler>
37 bool registerHandler(Handler&& handler) {
38 if (_count >= MaxHandlers) {
39 return false;
40 }
41 _handlers[_count] = EventHandlerDelegate<EventType, InlineSize>(std::forward<Handler>(handler));
42 _count++;
43 return true;
44 }
45
46 void dispatch(const EventType& event) const {
47 for (size_t i = 0; i < _count; ++i) {
48 _handlers[i].invoke(event);
49 }
50 }
51
52 [[nodiscard]] size_t size() const noexcept {
53 return _count;
54 }
55
56private:
57 std::array<EventHandlerDelegate<EventType, InlineSize>, MaxHandlers> _handlers{};
58 size_t _count = 0;
59};
60
61} // namespace internal
62
64template <typename EventVariant = DefaultEvents, typename StoragePolicy = DefaultStoragePolicy>
66
70template <typename... Events, typename StoragePolicy>
71class BasicReactor<std::variant<Events...>, StoragePolicy> {
72 static constexpr size_t MaxHandlersPerEvent = StoragePolicy::max_handlers_per_event;
73 static constexpr size_t InlineSize = StoragePolicy::inline_storage_size;
74
75public:
76 using EventVariant = std::variant<Events...>;
77 using StoragePolicyType = StoragePolicy;
78
79 BasicReactor() = default;
80 ~BasicReactor() = default;
81
82 BasicReactor(const BasicReactor&) = delete;
84
85 BasicReactor(BasicReactor&&) noexcept = default;
86 BasicReactor& operator=(BasicReactor&&) noexcept = default;
87
93 template <typename EventType, typename Handler>
94 bool registerHandler(Handler&& handler) {
95 CORIUM_ASSERT(!_sealed, "registerHandler() called after reactor was sealed by runtime.initialize(). Move handler registration into onRegisterHandlers().");
96 static_assert(has_variant_type_v<EventType, EventVariant>, "EventType is not part of EventVariant!");
97 auto& list = std::get<internal::FixedHandlerList<EventType, MaxHandlersPerEvent, InlineSize>>(_handlers);
98 return list.registerHandler(std::forward<Handler>(handler));
99 }
100
105 template <typename Handler>
106 bool registerHandler(Handler&& handler) {
107 CORIUM_ASSERT(!_sealed, "registerHandler() called after reactor was sealed by runtime.initialize(). Move handler registration into onRegisterHandlers().");
108 using EventType = callable_event_type_t<Handler>;
109 return registerHandler<EventType>(std::forward<Handler>(handler));
110 }
111
114 void dispatch(const EventVariant& event) const {
115 std::visit([this](const auto& concreteEvent) {
116 using EventType = std::decay_t<decltype(concreteEvent)>;
117 const auto& list = std::get<internal::FixedHandlerList<EventType, MaxHandlersPerEvent, InlineSize>>(_handlers);
118 list.dispatch(concreteEvent);
119 }, event);
120 }
121
123 void seal() noexcept {
124 _sealed = true;
125 }
126
128 [[nodiscard]] bool sealed() const noexcept {
129 return _sealed;
130 }
131
132private:
133 std::tuple<internal::FixedHandlerList<Events, MaxHandlersPerEvent, InlineSize>...> _handlers{};
134 bool _sealed = false;
135};
136
139
141template <typename EventVariant = DefaultEvents, typename StoragePolicy = DefaultStoragePolicy>
143
144} // namespace corium
Compile-time introspection traits for callable objects and event handlers.
Standard lifecycle events (QuitEvent, ErrorEvent, TimerEvent).
Zero-allocating Small Buffer Optimized (SBO) static delegate dispatcher.
Zero-heap assertion and panic handling utilities.
#define CORIUM_ASSERT(cond, msg)
Definition Panic.hpp:52
Static storage capacity policies for FastDelegate SBO inline buffers.
Compile-time type index resolution for std::variant alternative types.
std::variant< Events... > EventVariant
Definition Reactor.hpp:76
bool registerHandler(Handler &&handler)
Register event handler with automatic event type deduction from handler parameter signature.
Definition Reactor.hpp:106
void dispatch(const EventVariant &event) const
Dispatch event via std::visit to concrete static handler array.
Definition Reactor.hpp:114
bool sealed() const noexcept
Check if reactor has been sealed.
Definition Reactor.hpp:128
StoragePolicy StoragePolicyType
Definition Reactor.hpp:77
void seal() noexcept
Seal reactor handlers.
Definition Reactor.hpp:123
BasicReactor & operator=(const BasicReactor &)=delete
Primary template declaration for BasicReactor.
Definition Reactor.hpp:65
Lightweight non-allocating delegate wrapper with 32-byte Small Buffer Optimization (SBO)....
Definition FastDelegate.hpp:22
Fixed-capacity stack-allocated list of event handlers for a single event type.
Definition Reactor.hpp:32
size_t size() const noexcept
Definition Reactor.hpp:52
void dispatch(const EventType &event) const
Definition Reactor.hpp:46
bool registerHandler(Handler &&handler)
Definition Reactor.hpp:37
Definition Application.hpp:16