Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
system_handle.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <compare>
8#include <cstddef>
9#include <functional>
10#include <string>
11
12namespace helios::ecs {
13
14class Schedule;
15class SystemSetHandle;
17
18/**
19 * @brief Unique identifier for a system within a schedule.
20 * @details Combines the type-based SystemId with a per-schedule index for
21 * uniqueness, enabling the same system type to be added multiple times.
22 */
25 size_t slot = 0;
26 size_t generation = 0;
27
28 [[nodiscard]] constexpr bool operator==(
29 const ScheduleSystemId&) const noexcept = default;
30 [[nodiscard]] constexpr std::strong_ordering operator<=>(
31 const ScheduleSystemId&) const noexcept = default;
32};
33
34/// @brief Handle returned by Schedule::Add for configuring a system.
36public:
37 /**
38 * @brief Constructs a `SystemHandle` with the given system id and schedule.
39 * @param id The system id
40 * @param schedule The schedule that the system belongs to
41 */
42 constexpr SystemHandle(ScheduleSystemId id, Schedule& schedule) noexcept
43 : id_(id), schedule_(schedule) {}
44
45 SystemHandle(const SystemHandle&) = delete;
46 constexpr SystemHandle(SystemHandle&&) noexcept = default;
47 constexpr ~SystemHandle() noexcept = default;
48
49 SystemHandle& operator=(const SystemHandle&) = delete;
50 constexpr SystemHandle& operator=(SystemHandle&&) noexcept = default;
51
52 /**
53 * @brief Adds an ordering constraint: this system must run before the given
54 * system id.
55 * @param target System id that this system must precede
56 * @return Reference to this handle for chaining
57 */
58 constexpr auto Before(this auto&& self, SystemId target)
59 -> decltype(std::forward<decltype(self)>(self));
60
61 /**
62 * @brief Adds an ordering constraint: this system must run before the given
63 * system type.
64 * @tparam T System type satisfying `SystemTrait`
65 * @return Reference to this handle for chaining
66 */
67 template <SystemTrait T>
68 constexpr auto Before(this auto&& self)
69 -> decltype(std::forward<decltype(self)>(self)) {
70 return std::forward<decltype(self)>(self).Before(SystemId::From<T>());
71 }
72
73 /**
74 * @brief Adds an ordering constraint: this system must run before the given
75 * system handle.
76 * @param target System handle that this system must precede
77 * @return Reference to this handle for chaining
78 */
79 constexpr auto Before(this auto&& self, const SystemHandle& target)
80 -> decltype(std::forward<decltype(self)>(self)) {
81 return std::forward<decltype(self)>(self).Before(target.Id().id);
82 }
83
84 /**
85 * @brief Adds an ordering constraint: this system must run before the given
86 * system set handle.
87 * @param target System set handle that this system must precede
88 * @return Reference to this handle for chaining
89 */
90 constexpr auto Before(this auto&& self, const SystemSetHandle& target)
91 -> decltype(std::forward<decltype(self)>(self));
92
93 /**
94 * @brief Adds an ordering constraint: this system must run before all members
95 * of the given system group.
96 * @param target System group handle that this system must precede
97 * @return Reference to this handle for chaining
98 */
99 constexpr auto Before(this auto&& self, const SystemGroupHandle& target)
100 -> decltype(std::forward<decltype(self)>(self));
101
102 /**
103 * @brief Adds an ordering constraint: this system must run before all members
104 * of the given set.
105 * @param target Set that this system must precede
106 * @return Reference to this handle for chaining
107 */
108 constexpr auto Before(this auto&& self, SystemSetId target)
109 -> decltype(std::forward<decltype(self)>(self));
110
111 /**
112 * @brief Adds an ordering constraint: this system must run before all members
113 * of the given set type.
114 * @tparam T Set type satisfying `SystemSetTrait`
115 * @return Reference to this handle for chaining
116 */
117 template <SystemSetTrait T>
118 constexpr auto BeforeSet(this auto&& self)
119 -> decltype(std::forward<decltype(self)>(self)) {
120 return std::forward<decltype(self)>(self).Before(SystemSetId::From<T>());
121 }
122
123 /**
124 * @brief Adds an ordering constraint: this system must run after the given
125 * system id.
126 * @param target System id that this system must follow
127 * @return Reference to this handle for chaining
128 */
129 constexpr auto After(this auto&& self, SystemId target)
130 -> decltype(std::forward<decltype(self)>(self));
131
132 /**
133 * @brief Adds an ordering constraint: this system must run after the given
134 * system type.
135 * @tparam T System type satisfying `SystemTrait`
136 * @return Reference to this handle for chaining
137 */
138 template <SystemTrait T>
139 constexpr auto After(this auto&& self)
140 -> decltype(std::forward<decltype(self)>(self)) {
141 return std::forward<decltype(self)>(self).After(SystemId::From<T>());
142 }
143
144 /**
145 * @brief Adds an ordering constraint: this system must run after the given
146 * system handle.
147 * @param target System handle that this system must follow
148 * @return Reference to this handle for chaining
149 */
150 constexpr auto After(this auto&& self, const SystemHandle& target)
151 -> decltype(std::forward<decltype(self)>(self)) {
152 return std::forward<decltype(self)>(self).After(target.Id().id);
153 }
154
155 /**
156 * @brief Adds an ordering constraint: this system must run after all members
157 * of the given set.
158 * @param target Set that this system must follow
159 * @return Reference to this handle for chaining
160 */
161 constexpr auto After(this auto&& self, SystemSetId target)
162 -> decltype(std::forward<decltype(self)>(self));
163
164 /**
165 * @brief Adds an ordering constraint: this system must run after all members
166 * of the given set type.
167 * @tparam T Set type satisfying `SystemSetTrait`
168 * @return Reference to this handle for chaining
169 */
170 template <SystemSetTrait T>
171 constexpr auto AfterSet(this auto&& self)
172 -> decltype(std::forward<decltype(self)>(self)) {
173 return std::forward<decltype(self)>(self).After(SystemSetId::From<T>());
174 }
175
176 /**
177 * @brief Adds an ordering constraint: this system must run after all members
178 * of the given system set handle.
179 * @param target System set handle that this system must follow
180 * @return Reference to this handle for chaining
181 */
182 constexpr auto After(this auto&& self, const SystemSetHandle& target)
183 -> decltype(std::forward<decltype(self)>(self));
184
185 /**
186 * @brief Adds an ordering constraint: this system must run after all members
187 * of the given system group.
188 * @param target System group handle that this system must follow
189 * @return Reference to this handle for chaining
190 */
191 constexpr auto After(this auto&& self, const SystemGroupHandle& target)
192 -> decltype(std::forward<decltype(self)>(self));
193
194 /**
195 * @brief Adds a run condition predicate that must return true for this system
196 * to execute.
197 * @param condition Predicate evaluated before the system runs
198 * @param policy Access policy for the run condition
199 * @param options Options for run condition local data construction
200 * @return Reference to this handle for chaining
201 */
202 constexpr auto RunIf(this auto&& self, RunCondition condition,
203 AccessPolicy policy = {},
204 SystemLocalDataOptions options = {})
205 -> decltype(std::forward<decltype(self)>(self));
206
207 /**
208 * @brief Adds a run condition functor that must return true for this system
209 * to execute.
210 * @details Lambdas are rejected; use `RunIf(std::string, T&&)` to register
211 * them with an explicit name.
212 * @tparam T Run condition type satisfying `FunctorSystemTrait`
213 * @param condition Functor with declared access policy
214 * @return Reference to this handle for chaining
215 */
216 template <FunctorSystemTrait T>
217 constexpr auto RunIf(this auto&& self, T&& condition)
218 -> decltype(std::forward<decltype(self)>(self));
219
220 /**
221 * @brief Adds a run condition to this system with an explicit name.
222 * @details Required for lambdas. Derives identity from the provided name.
223 * @tparam T Run condition type satisfying `SystemTrait`
224 * @param name Human-readable name
225 * @param condition Functor with declared access policy
226 * @return Reference to this handle for chaining
227 */
228 template <SystemTrait T>
229 constexpr auto RunIf(this auto&& self, std::string name, T&& condition)
230 -> decltype(std::forward<decltype(self)>(self));
231
232 /**
233 * @brief Assigns this system to a labeled set by id.
234 * @details Creates the target set if needed. Repeated calls with the same set
235 * id are ignored.
236 * @param set_id Set to assign this system to
237 * @return Reference to this handle for chaining
238 */
239 constexpr auto InSet(this auto&& self, SystemSetId set_id)
240 -> decltype(std::forward<decltype(self)>(self));
241
242 /**
243 * @brief Assigns this system to a labeled set by type.
244 * @tparam T Set type satisfying `SystemSetTrait`
245 * @return Reference to this handle for chaining
246 */
247 template <SystemSetTrait T>
248 constexpr auto InSet(this auto&& self, const T& /*set*/ = {})
249 -> decltype(std::forward<decltype(self)>(self)) {
250 return std::forward<decltype(self)>(self).InSet(SystemSetId::From<T>());
251 }
252
253 /**
254 * @brief Finishes configuration and returns a reference to the parent
255 * schedule.
256 * @return Reference to the parent schedule for continued configuration
257 */
258 constexpr Schedule& Done() noexcept { return schedule_.get(); }
259
260 /**
261 * @brief Gets the unique schedule-local identifier for this system.
262 * @return Schedule system id
263 */
264 [[nodiscard]] constexpr ScheduleSystemId Id() const noexcept { return id_; }
265
266private:
268 std::reference_wrapper<Schedule> schedule_;
269};
270
271} // namespace helios::ecs
Stores data access requirements for a system at compile time.
A collection of systems, sets, and run conditions with ordering constraints.
Definition schedule.hpp:182
Handle returned by variadic Schedule::Add for configuring a batch of systems added together.
constexpr auto After(this auto &&self, const SystemHandle &target) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run after the given system handle.
constexpr auto After(this auto &&self) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run after the given system type.
constexpr auto RunIf(this auto &&self, RunCondition condition, AccessPolicy policy={}, SystemLocalDataOptions options={}) -> decltype(std::forward< decltype(self)>(self))
Adds a run condition predicate that must return true for this system to execute.
Definition schedule.hpp:756
constexpr auto Before(this auto &&self, const SystemHandle &target) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run before the given system handle.
constexpr SystemHandle(ScheduleSystemId id, Schedule &schedule) noexcept
Constructs a SystemHandle with the given system id and schedule.
constexpr auto After(this auto &&self, SystemId target) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run after the given system id.
Definition schedule.hpp:722
constexpr auto AfterSet(this auto &&self) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run after all members of the given set type.
constexpr ScheduleSystemId Id() const noexcept
Gets the unique schedule-local identifier for this system.
constexpr auto InSet(this auto &&self, SystemSetId set_id) -> decltype(std::forward< decltype(self)>(self))
Assigns this system to a labeled set by id.
Definition schedule.hpp:797
constexpr auto Before(this auto &&self, SystemId target) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run before the given system id.
Definition schedule.hpp:688
constexpr Schedule & Done() noexcept
Finishes configuration and returns a reference to the parent schedule.
SystemHandle(const SystemHandle &)=delete
constexpr auto InSet(this auto &&self, const T &={}) -> decltype(std::forward< decltype(self)>(self))
Assigns this system to a labeled set by type.
constexpr auto BeforeSet(this auto &&self) -> decltype(std::forward< decltype(self)>(self))
Adds an ordering constraint: this system must run before all members of the given set type.
constexpr SystemHandle(SystemHandle &&) noexcept=default
Handle returned by Schedule::Set for configuring a named system set.
Concept for systems that declare all data access via parameter types.
Definition system.hpp:87
std::function< bool(World &, SystemLocalData &)> RunCondition
Type-erased run condition: returns bool, receives World& + local data.
STL namespace.
Unique identifier for a system within a schedule.
constexpr bool operator==(const ScheduleSystemId &) const noexcept=default
constexpr std::strong_ordering operator<=>(const ScheduleSystemId &) const noexcept=default
Id for systems.
Definition system.hpp:146
static constexpr SystemId From(std::string_view name) noexcept
Creates system id from a name.
Definition system.hpp:154
Identifier for a system set.
static constexpr SystemSetId From(SystemSetTypeIndex index) noexcept
Creates system set id from a system set type index.