Skip to main content

TimerManager.ixx File

Manager for game timers and timer command handling. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine
namespaceruntime
namespacetiming

Classes Index

classTimerManager

Manager that owns game timers and processes timer control commands. More...

Description

Manager for game timers and timer command handling.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <cassert>
8#include <memory>
9#include <span>
10#include <vector>
11#include <stdexcept>
12#include <ranges>
13
14export module helios.engine.runtime.timing.TimerManager;
15
16import helios.engine.runtime.timing.types;
17import helios.engine.runtime.timing.commands;
18
19import helios.engine.runtime.timing.Timer;
20
21import helios.engine.runtime.timing.types.TimerId;
22
23import helios.engine.runtime.world.UpdateContext;
24import helios.engine.runtime.messaging.command.CommandHandlerRegistry;
25
26import helios.engine.core.types;
27import helios.engine.util.Guid;
28import helios.engine.runtime.world.tags.ManagerRole;
29
34export namespace helios::engine::runtime::timing {
35
48
52 std::vector<Timer> timers_;
53
57 std::vector<TimerControlContext> pendingControlContexts_;
58
66 [[nodiscard]] bool has(const TimerId timerId) noexcept {
67 return getTimer(timerId) != nullptr;
68 }
69
70
78 Timer& add(const TimerId timerId) noexcept {
79 timers_.emplace_back(Timer(timerId));
80
81 return timers_.back();
82 }
83
84 public:
86
96 Timer& addTimer(TimerId timerId) noexcept {
97 assert(!has(timerId) && "Timer with TimerId already registered");
98
99 return add(timerId);
100 }
101
109 Timer* getTimer(const TimerId timerId) {
110 const auto timerIt = std::ranges::find_if(
111 timers_,
112 [&](const auto& timer) {
113 return timer.timerId() == timerId;
114 });
115
116 if (timerIt == timers_.end()) {
117 return nullptr;
118 }
119 return &*timerIt;
120 }
121
127 [[nodiscard]] std::span<Timer> timers() noexcept {
128 return timers_;
129 }
130
140 void flush(
142 ) noexcept {
143
144 for (const auto& controlContext : pendingControlContexts_) {
145 auto* timer = getTimer(controlContext.timerId);
146 if (timer) {
147 if (controlContext.resetElapsed) {
148 timer->reset(controlContext.timerState);
149 } else {
150 timer->setState(controlContext.timerState);
151 }
152 }
153 }
154 pendingControlContexts_.clear();
155 }
156
165 pendingControlContexts_.push_back(timerControlCommand.timerControlContext());
166 return true;
167 };
168
175 commandHandlerRegistry.registerHandler<TimerControlCommand>(*this);
176 }
177
181 void reset() {
182 for (auto& timer : timers_) {
183 timer.reset();
184 }
185 }
186 };
187
188}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.