Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
run_condition.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <functional>
10#include <string>
11#include <type_traits>
12
13namespace helios::ecs {
14
15/// @brief Type-erased run condition: returns bool, receives World& + local
16/// data.
17#ifdef HELIOS_MOVEONLY_FUNCTION_AVAILABLE
18using RunCondition = std::move_only_function<bool(World&, SystemLocalData&)>;
19#else
20using RunCondition = std::function<bool(World&, SystemLocalData&)>;
21#endif
22
23/// @brief Storage for a run condition — predicate + access policy + local data.
26 std::string name;
29
31
32 /**
33 * @brief Constructs storage from individual members and options.
34 * @param condition Run condition callable
35 * @param cond_name Human-readable name for the condition
36 * @param policy Access policy
37 * @param options System local data options
38 */
39 RunConditionStorage(RunCondition condition, std::string cond_name,
40 AccessPolicy policy, SystemLocalDataOptions options);
43 ~RunConditionStorage() noexcept = default;
44
45 RunConditionStorage& operator=(const RunConditionStorage&) = delete;
46 RunConditionStorage& operator=(RunConditionStorage&&) noexcept = default;
47
48 /**
49 * @brief Factory from a param-style run condition functor.
50 * @details Accepts only functor structs; lambdas must use
51 * `FromParamNamed`.
52 * @tparam T Run condition type satisfying `FunctorSystemTrait` and
53 * returning bool
54 * @param condition Run condition functor
55 * @param options System local data options
56 * @return RunConditionStorage
57 */
58 template <FunctorSystemTrait T>
59 [[nodiscard]] static RunConditionStorage FromParam(
60 T&& instance, SystemLocalDataOptions options = {});
61
62 /**
63 * @brief Factory from a param-style run condition with an explicit name.
64 * @details Required for lambdas. Derives identity from the provided name.
65 * @tparam T Run condition type satisfying `SystemTrait` and returning bool
66 * @param name Human-readable name
67 * @param condition Run condition functor
68 * @param options System local data options
69 * @return RunConditionStorage
70 */
71 template <SystemTrait T>
72 [[nodiscard]] static RunConditionStorage FromParamNamed(
73 std::string name, T&& instance, SystemLocalDataOptions options = {});
74
75 /**
76 * @brief Factory from a raw predicate + explicit policy.
77 * @param fn Predicate function
78 * @param policy Access policy
79 * @param options Local data options
80 * @return Run condition storage
81 */
82 [[nodiscard]] static RunConditionStorage From(
83 RunCondition fn, AccessPolicy policy = {},
84 SystemLocalDataOptions options = {}, std::string name = {}) {
85 return {std::move(fn), std::move(name), std::move(policy), options};
86 }
87};
88
90 std::string cond_name,
91 AccessPolicy policy,
93 : run_condition(std::move(condition)),
94 name(std::move(cond_name)),
95 access_policy(std::move(policy)),
96 local_data(options) {}
97
98template <FunctorSystemTrait T>
99inline auto RunConditionStorage::FromParam(T&& instance,
102 using Decayed = std::remove_cvref_t<T>;
103 using ArgsTuple =
104 typename details::MemberFnArgs<decltype(&Decayed::operator())>::ArgsTuple;
105
106 constexpr auto cond_name = SystemNameOf<Decayed>();
108
109 auto wrapped = [cond = std::forward<T>(instance), policy_ = policy](
110 World& world, SystemLocalData& data) mutable -> bool {
111 return []<typename... Params>(
112 std::tuple<Params...>*, auto& inner_cond, World& inner_world,
113 SystemLocalData& inner_data,
114 [[maybe_unused]] const AccessPolicy& inner_policy) -> bool {
115 return inner_cond(SystemParamTraits<std::remove_cvref_t<Params>>::Make(
116 inner_world, inner_data, inner_policy)...);
117 }(static_cast<ArgsTuple*>(nullptr), cond, world, data, policy_);
118 };
119
120 return {std::move(wrapped), std::string(cond_name), std::move(policy),
121 options};
122}
123
124template <SystemTrait T>
125inline auto RunConditionStorage::FromParamNamed(std::string name, T&& instance,
128 using Decayed = std::remove_cvref_t<T>;
129 using ArgsTuple =
130 typename details::MemberFnArgs<decltype(&Decayed::operator())>::ArgsTuple;
131
133
134 auto wrapped = [cond = std::forward<T>(instance), policy_ = policy](
135 World& world, SystemLocalData& data) mutable -> bool {
136 return []<typename... Params>(
137 std::tuple<Params...>*, auto& inner_cond, World& inner_world,
138 SystemLocalData& inner_data,
139 [[maybe_unused]] const AccessPolicy& inner_policy) -> bool {
140 return inner_cond(SystemParamTraits<std::remove_cvref_t<Params>>::Make(
141 inner_world, inner_data, inner_policy)...);
142 }(static_cast<ArgsTuple*>(nullptr), cond, world, data, policy_);
143 };
144
145 return {std::move(wrapped), std::move(name), std::move(policy), options};
146}
147
148} // 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
std::function< bool(World &, SystemLocalData &)> RunCondition
Type-erased run condition: returns bool, receives World& + local data.
constexpr AccessPolicy BuildPolicyFromSystem()
Deduces parameter types from the system's operator() signature and builds the access policy.
STL namespace.
Storage for a run condition — predicate + access policy + local data.
static RunConditionStorage From(RunCondition fn, AccessPolicy policy={}, SystemLocalDataOptions options={}, std::string name={})
Factory from a raw predicate + explicit policy.
RunConditionStorage(const RunConditionStorage &)=delete
RunConditionStorage(RunConditionStorage &&) noexcept=default
static RunConditionStorage FromParam(T &&instance, SystemLocalDataOptions options={})
Factory from a param-style run condition functor.
static RunConditionStorage FromParamNamed(std::string name, T &&instance, SystemLocalDataOptions options={})
Factory from a param-style run condition with an explicit name.
Options for system local data.
Local data for a system.
Primary template — deliberately incomplete.
Extracts the argument types of a member function pointer.
Definition system.hpp:25