Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
system_storage.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <functional>
10#include <string>
11#include <type_traits>
12#include <utility>
13
14namespace helios::ecs {
15
16class World;
17
18#ifdef HELIOS_MOVEONLY_FUNCTION_AVAILABLE
19using SystemCallable = std::move_only_function<void(World&, SystemLocalData&)>;
20#else
21using SystemCallable = std::function<void(World&, SystemLocalData&)>;
22#endif
23
24/// @brief Storage for a system.
27 std::string name;
29
30 /// Type-erased system callable.
31 /// Receives World& and SystemLocalData& directly. The wrapper
32 /// lambda constructs params and calls Update after the system runs.
34
36
37 SystemStorage() = default;
38
39 /**
40 * @brief Constructs storage from individual members and options.
41 * @param id System identifier
42 * @param name System name
43 * @param policy Access policy
44 * @param system Callable that receives (World&, SystemLocalData&)
45 * @param options System local data options
46 */
47 SystemStorage(SystemId sid, std::string sname, AccessPolicy spolicy,
48 SystemCallable ssystem, SystemLocalDataOptions options);
49 SystemStorage(const SystemStorage&) = delete;
50 SystemStorage(SystemStorage&&) noexcept = default;
51 ~SystemStorage() noexcept = default;
52
53 SystemStorage& operator=(const SystemStorage&) = delete;
54 SystemStorage& operator=(SystemStorage&&) noexcept = default;
55
56 /**
57 * @brief Factory from a param-style system functor.
58 * @details Auto-deduces the access policy from the functor's parameter
59 * types. Wraps invocation so that Update is called after the system runs.
60 * Only accepts functor structs; lambdas must use `FromParamNamed`.
61 * @tparam T System type satisfying `FunctorSystemTrait`
62 * @param system System instance
63 * @param options System local data options
64 * @return System storage
65 */
66 template <FunctorSystemTrait T>
67 [[nodiscard]] static SystemStorage FromParam(
68 T&& system, SystemLocalDataOptions options = {});
69
70 /**
71 * @brief Factory from a param-style system functor with an explicit name.
72 * @details Identical to `FromParam` but uses the provided name instead of
73 * the auto-deduced type name for both the storage name and the `SystemId`
74 * derivation. Required for lambdas and optional for functor duplicates.
75 * @tparam T System type satisfying `SystemTrait`
76 * @param name Human-readable system name
77 * @param system System instance
78 * @param options System local data options
79 * @return System storage
80 */
81 template <SystemTrait T>
82 [[nodiscard]] static SystemStorage FromParamNamed(
83 std::string name, T&& system, SystemLocalDataOptions options = {});
84
85 /**
86 * @brief Factory for a raw callable with explicit name and access policy.
87 * @param name System name
88 * @param system Callable receiving (`World&`, `SystemLocalData&`)
89 * @param access_policy Access policy for scheduling
90 * @param options System local data options
91 * @return System storage
92 */
93 [[nodiscard]] static SystemStorage From(std::string name,
96 SystemLocalDataOptions options = {}) {
97 return {SystemId::From(name), std::move(name), std::move(access_policy),
98 std::move(system), options};
99 }
100
101 /**
102 * @brief Factory from a system type index with explicit name and policy.
103 * @param name System name
104 * @param index System type index
105 * @param system Callable receiving (`World&`, `SystemLocalData&`)
106 * @param access_policy Access policy for scheduling
107 * @param options System local data options
108 * @return System storage
109 */
110 [[nodiscard]] static SystemStorage From(std::string name,
111 SystemTypeIndex index,
114 SystemLocalDataOptions options = {}) {
115 return {SystemId::From(index), std::move(name), std::move(access_policy),
116 std::move(system), options};
117 }
118
119 /**
120 * @brief Factory from a system type id with explicit name and policy.
121 * @param name System name
122 * @param id System type id
123 * @param system Callable receiving (`World&`, `SystemLocalData&`)
124 * @param access_policy Access policy for scheduling
125 * @param options System local data options
126 * @return System storage
127 */
128 [[nodiscard]] static SystemStorage From(std::string name, SystemTypeId id,
131 SystemLocalDataOptions options = {}) {
132 return From(std::move(name), id.Index(), std::move(system),
133 std::move(access_policy), options);
134 }
135};
136
137inline SystemStorage::SystemStorage(SystemId sid, std::string sname,
138 AccessPolicy spolicy,
139 SystemCallable ssystem,
141 : id(sid),
142 name(std::move(sname)),
143 access_policy(std::move(spolicy)),
144 system(std::move(ssystem)),
145 local_data(options) {}
146
147template <FunctorSystemTrait T>
149 SystemLocalDataOptions options) {
150 using Decayed = std::remove_cvref_t<T>;
151
152 constexpr auto name = SystemNameOf<Decayed>();
153 constexpr SystemId id = SystemId::From<Decayed>();
155
156 auto wrapped = [system = std::forward<T>(system), policy_ = policy](
157 World& world, SystemLocalData& local) mutable {
158 []<typename... Params>(std::tuple<Params...>*, auto& inner_system,
159 World& inner_world, SystemLocalData& inner_data,
160 const AccessPolicy& inner_policy) {
161 inner_system(SystemParamTraits<std::remove_cvref_t<Params>>::Make(
162 inner_world, inner_data, inner_policy)...);
163 }(static_cast<typename details::MemberFnArgs<
164 decltype(&Decayed::operator())>::ArgsTuple*>(nullptr),
165 system, world, local, policy_);
166 };
167
168 return {id, std::string(name), std::move(policy), std::move(wrapped),
169 options};
170}
171
172template <SystemTrait T>
174 std::string name, T&& system, SystemLocalDataOptions options) {
175 using Decayed = std::remove_cvref_t<T>;
176
179
180 auto wrapped = [system = std::forward<T>(system), policy_ = policy](
181 World& world, SystemLocalData& local) mutable {
182 []<typename... Params>(std::tuple<Params...>*, auto& inner_system,
183 World& inner_world, SystemLocalData& inner_data,
184 const AccessPolicy& inner_policy) {
185 inner_system(SystemParamTraits<std::remove_cvref_t<Params>>::Make(
186 inner_world, inner_data, inner_policy)...);
187 }(static_cast<typename details::MemberFnArgs<
188 decltype(&Decayed::operator())>::ArgsTuple*>(nullptr),
189 system, world, local, policy_);
190 };
191
192 return {id, std::move(name), std::move(policy), std::move(wrapped), options};
193}
194
195} // namespace helios::ecs
Stores data access requirements for a system at compile time.
The World class manages entities with their components and systems.
Definition world.hpp:39
Concept for system types that are not lambda closures.
Definition system.hpp:105
constexpr std::string_view SystemNameOf() noexcept
Gets the name of a system.
Definition system.hpp:125
constexpr AccessPolicy BuildPolicyFromSystem()
Deduces parameter types from the system's operator() signature and builds the access policy.
utils::TypeId SystemTypeId
Type id for systems.
Definition system.hpp:16
std::function< void(World &, SystemLocalData &)> SystemCallable
utils::TypeIndex SystemTypeIndex
Type index for systems.
Definition system.hpp:19
STL namespace.
Id for systems.
Definition system.hpp:146
static constexpr SystemId From() noexcept
Creates system id from a system type.
Definition system.hpp:182
Options for system local data.
Local data for a system.
Primary template — deliberately incomplete.
Storage for a system.
static SystemStorage FromParamNamed(std::string name, T &&system, SystemLocalDataOptions options={})
Factory from a param-style system functor with an explicit name.
SystemStorage(const SystemStorage &)=delete
static SystemStorage FromParam(T &&system, SystemLocalDataOptions options={})
Factory from a param-style system functor.
static SystemStorage From(std::string name, SystemTypeId id, SystemCallable system, AccessPolicy access_policy={}, SystemLocalDataOptions options={})
Factory from a system type id with explicit name and policy.
SystemStorage(SystemStorage &&) noexcept=default
static SystemStorage From(std::string name, SystemCallable system, AccessPolicy access_policy={}, SystemLocalDataOptions options={})
Factory for a raw callable with explicit name and access policy.
static SystemStorage From(std::string name, SystemTypeIndex index, SystemCallable system, AccessPolicy access_policy={}, SystemLocalDataOptions options={})
Factory from a system type index with explicit name and policy.
Extracts the argument types of a member function pointer.
Definition system.hpp:25