Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1
7#pragma once
8
10#include "corium/EventSink.hpp"
13
14#include <utility>
15
16namespace corium {
17
18// Forward declaration of BasicRuntime for friendship
19template <
20 typename EventVariant,
21 typename QueuePolicy,
22 typename SignalPolicy,
23 typename StoragePolicy,
24 typename OverflowPolicy,
25 typename TimerStoragePolicy,
26 typename ProfilerPolicy
27>
28class BasicRuntime;
29
37template <typename Derived, typename EventVariantOrBus = DefaultEvents, std::size_t MaxServices = 8>
39public:
43
44 Application() = default;
46 {
47 shutdownServices();
48 _context.detachFromRuntime();
49 }
50
51 Application(const Application&) = delete;
53
56
57protected:
59 template <typename Handler>
60 bool on(Handler&& handler)
61 {
62 return _context.registerHandler(std::forward<Handler>(handler));
63 }
64
68 template <typename Filter, typename Handler>
69 requires (std::is_invocable_r_v<bool, Filter&, const callable_event_type_t<Handler>&>)
70 bool on(Filter&& filter, Handler&& handler)
71 {
72 return _context.registerFilteredHandler(std::forward<Filter>(filter), std::forward<Handler>(handler));
73 }
74
77 {
78 return _context.eventSink();
79 }
80
82 template <typename Rep, typename Period>
83 TimerId postDelayed(EventVariant event, const std::chrono::duration<Rep, Period>& delay, EventPriority priority = EventPriority::Normal)
84 {
85 return _context.scheduleDelayed(std::move(event), delay, priority);
86 }
87
89 template <typename Rep, typename Period>
90 TimerId postPeriodic(EventVariant event, const std::chrono::duration<Rep, Period>& interval, EventPriority priority = EventPriority::Normal)
91 {
92 return _context.schedulePeriodic(std::move(event), interval, priority);
93 }
94
96 bool cancelTimer(TimerId id) noexcept
97 {
98 return _context.cancelTimer(id);
99 }
100
103 {
104 _context.requestQuit();
105 }
106
108 [[nodiscard]] ServiceRegistryType& services() noexcept
109 {
110 return _serviceRegistry;
111 }
112
114 [[nodiscard]] const ServiceRegistryType& services() const noexcept
115 {
116 return _serviceRegistry;
117 }
118
120 template <typename ServiceType>
121 [[nodiscard]] ServiceType* getService() const noexcept
122 {
123 return _serviceRegistry.template getService<ServiceType>();
124 }
125
126private:
127 template <
128 typename EV,
129 typename QP,
130 typename SP,
131 typename STP,
132 typename OP,
133 typename TP,
134 typename PP
135 >
136 friend class BasicRuntime;
137
138 template <typename Registry>
139 void configureServices(Registry& registry)
140 {
141 if constexpr (requires(Derived& d, Registry& r) { d.onConfigureServices(r); }) {
142 static_cast<Derived*>(this)->onConfigureServices(registry);
143 }
144 }
145
146 void initializeServices(EventSinkT<EventVariant> sink)
147 {
148 configureServices(_serviceRegistry);
149 _serviceRegistry.initialize(BasicServiceContext<EventVariant>{sink});
150 }
151
152 void shutdownServices() noexcept
153 {
154 _serviceRegistry.shutdown();
155 }
156
157 void registerHandlers()
158 {
159 if constexpr (requires(Derived& d) { d.onRegisterHandlers(); }) {
160 static_cast<Derived*>(this)->onRegisterHandlers();
161 }
162 }
163
164 void initialize()
165 {
166 if constexpr (requires(Derived& d) { d.onInitialize(); }) {
167 static_cast<Derived*>(this)->onInitialize();
168 }
169 }
170
171 void shutdown()
172 {
173 if constexpr (requires(Derived& d) { d.onShutdown(); }) {
174 static_cast<Derived*>(this)->onShutdown();
175 }
176 }
177
178 void setContext(ApplicationContext<EventVariant> context)
179 {
180 _context = context;
181 if constexpr (requires(Derived& d, ApplicationContext<EventVariant> c) { d.onSetContext(c); }) {
182 static_cast<Derived*>(this)->onSetContext(context);
183 }
184 }
185
186 void resetContext() noexcept
187 {
188 _context.reset();
189 }
190
191 ApplicationContext<EventVariant> _context;
192 ServiceRegistryType _serviceRegistry;
193};
194
195} // namespace corium
Type-erased context for application runtime introspection and lifecycle control.
Non-allocating type-erased fat pointer handle for lock-free event posting.
Fixed-capacity static container for background worker services.
Compile-time type index resolution for std::variant alternative types.
bool registerFilteredHandler(Filter &&filter, Handler &&handler)
Register a filtered event handler executed only when predicate evaluates to true.
Definition ApplicationContext.hpp:113
TimerId schedulePeriodic(EventVariant event, const std::chrono::duration< Rep, Period > &interval, EventPriority priority=EventPriority::Normal) const
Schedule a recurring periodic event with std::chrono duration.
Definition ApplicationContext.hpp:141
void reset() noexcept
Reset context state to empty.
Definition ApplicationContext.hpp:184
bool registerHandler(Handler &&handler)
Register an event handler into the application event bus.
Definition ApplicationContext.hpp:77
EventSinkT< EventVariant > eventSink() const noexcept
Access event sink handle.
Definition ApplicationContext.hpp:124
bool cancelTimer(TimerId id) const noexcept
Cancel an active timer.
Definition ApplicationContext.hpp:150
TimerId scheduleDelayed(EventVariant event, const std::chrono::duration< Rep, Period > &delay, EventPriority priority=EventPriority::Normal) const
Schedule a single-shot delayed event with std::chrono duration.
Definition ApplicationContext.hpp:131
void requestQuit() const
Request graceful application exit.
Definition ApplicationContext.hpp:159
void detachFromRuntime() noexcept
Detach application from runtime to prevent dangling callbacks on shutdown.
Definition ApplicationContext.hpp:174
Static CRTP base class for applications managed by Corium Runtime. Subclass Application<Derived> or A...
Definition Application.hpp:38
ServiceRegistryType & services() noexcept
Access background service registry.
Definition Application.hpp:108
void requestQuit()
Request graceful runtime shutdown.
Definition Application.hpp:102
EventSinkT< EventVariant > eventSink()
Access event sink handle.
Definition Application.hpp:76
TimerId postDelayed(EventVariant event, const std::chrono::duration< Rep, Period > &delay, EventPriority priority=EventPriority::Normal)
Schedule a single-shot delayed event.
Definition Application.hpp:83
const ServiceRegistryType & services() const noexcept
Access const background service registry.
Definition Application.hpp:114
Application(Application &&)=delete
ServiceType * getService() const noexcept
Retrieve registered background service by concrete type.
Definition Application.hpp:121
TimerId postPeriodic(EventVariant event, const std::chrono::duration< Rep, Period > &interval, EventPriority priority=EventPriority::Normal)
Schedule a recurring periodic event.
Definition Application.hpp:90
~Application()
Definition Application.hpp:45
bool on(Handler &&handler)
Register event handler with automatic event type deduction from callable signature.
Definition Application.hpp:60
Application & operator=(const Application &)=delete
bool cancelTimer(TimerId id) noexcept
Cancel an active timer.
Definition Application.hpp:96
internal::extract_event_variant_t< EventVariantOrBus > EventVariant
Definition Application.hpp:40
Application & operator=(Application &&)=delete
bool on(Filter &&filter, Handler &&handler)
Register a filtered event handler executed only when predicate evaluates to true.
Definition Application.hpp:70
Application(const Application &)=delete
BasicServiceRegistry< MaxServices, EventVariant > ServiceRegistryType
Definition Application.hpp:41
Corium Application Runtime managing MPSC event loops and static policy execution. Zero dynamic heap a...
Definition Runtime.hpp:48
Non-allocating ServiceRegistry storing service handles in a fixed stack/static array....
Definition ServiceRegistry.hpp:57
void shutdown() noexcept
Stop and join all registered background service threads.
Definition ServiceRegistry.hpp:165
void initialize(ServiceContextType ctx)
Initialize and launch all registered background service jthreads.
Definition ServiceRegistry.hpp:148
typename extract_event_variant< T >::type extract_event_variant_t
Definition VariantIndex.hpp:79
Definition Application.hpp:16
EventPriority
Event priority levels for multi-priority queue policies.
Definition QueuePolicies.hpp:32
uint32_t TimerId
Definition TimerScheduler.hpp:22