Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
system_set_config.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
8
9#include <functional>
10#include <vector>
11
12namespace helios::app {
13
14// Forward declaration
15class SubApp;
16
17/**
18 * @brief Fluent builder for configuring system sets with ordering.
19 * @details Provides a chainable API for specifying system set dependencies.
20 * The configuration is applied when the builder is destroyed or when explicitly finalized.
21 * @tparam Schedule Schedule type where the set is configured
22 * @tparam Set System set type being configured
23 *
24 * @example
25 * @code
26 * app.ConfigureSet<PhysicsSet>(kUpdate).After<InputSet>().Before<RenderSet>();
27 * @endcode
28 */
29template <ScheduleTrait Schedule, SystemSetTrait Set>
31public:
32 /**
33 * @brief Constructs a system set configuration builder.
34 * @param sub_app Reference to the sub-app where the set is configured
35 * @param schedule Schedule instance where the set is configured
36 */
37 explicit SystemSetConfig(SubApp& sub_app, Schedule schedule = {}) noexcept : sub_app_(sub_app), schedule_(schedule) {}
39 SystemSetConfig(SystemSetConfig&&) noexcept = default;
41
42 SystemSetConfig& operator=(const SystemSetConfig&) = delete;
43 SystemSetConfig& operator=(SystemSetConfig&&) noexcept = default;
44
45 /**
46 * @brief Adds ordering constraint: this set runs after specified sets.
47 * @details Creates a dependency edge from each specified set to this set.
48 * All systems in the specified sets will run before systems in this set.
49 * @tparam AfterSets System set types that must run before this set
50 * @return Reference to this config for method chaining
51 */
52 template <SystemSetTrait... AfterSets>
53 requires(sizeof...(AfterSets) > 0 && utils::UniqueTypes<AfterSets...>)
55
56 /**
57 * @brief Adds ordering constraint: this set runs before specified sets.
58 * @details Creates a dependency edge from this set to each specified set.
59 * All systems in this set will run before systems in the specified sets.
60 * @tparam BeforeSets System set types that must run after this set
61 * @return Reference to this config for method chaining
62 */
63 template <SystemSetTrait... BeforeSets>
64 requires(sizeof...(BeforeSets) > 0 && utils::UniqueTypes<BeforeSets...>)
66
67 /**
68 * @brief Explicitly applies the configuration.
69 * @details Called automatically by destructor if not already applied.
70 * Can be called explicitly to control when configuration is finalized.
71 */
72 void Apply();
73
74private:
75 std::reference_wrapper<SubApp> sub_app_; ///< Sub-app where set is configured
76 Schedule schedule_; ///< Schedule where set is configured
77 bool applied_ = false; ///< Whether configuration has been applied
78
79 std::vector<SystemSetId> before_sets_; ///< System sets that run after this set
80 std::vector<SystemSetId> after_sets_; ///< System sets that run before this set
81};
82
83template <ScheduleTrait Schedule, SystemSetTrait Set>
84inline SystemSetConfig<Schedule, Set>::~SystemSetConfig() {
85 if (!applied_) {
86 Apply();
87 }
88}
89
90template <ScheduleTrait Schedule, SystemSetTrait Set>
91template <SystemSetTrait... AfterSets>
92 requires(sizeof...(AfterSets) > 0 && utils::UniqueTypes<AfterSets...>)
93inline auto SystemSetConfig<Schedule, Set>::After() -> SystemSetConfig& {
94 (after_sets_.push_back(SystemSetIdOf<AfterSets>()), ...);
95 return *this;
96}
97
98template <ScheduleTrait Schedule, SystemSetTrait Set>
99template <SystemSetTrait... BeforeSets>
100 requires(sizeof...(BeforeSets) > 0 && utils::UniqueTypes<BeforeSets...>)
101inline auto SystemSetConfig<Schedule, Set>::Before() -> SystemSetConfig& {
102 (before_sets_.push_back(SystemSetIdOf<BeforeSets>()), ...);
103 return *this;
104}
105
106template <ScheduleTrait Schedule, SystemSetTrait Set>
108 if (applied_) {
109 return;
110 }
111
112 applied_ = true;
113
114 auto& sub_app = sub_app_.get();
115 auto& scheduler = sub_app.GetScheduler();
116
117 // Ensure this set exists in the registry
118 const auto& this_info = scheduler.template GetOrRegisterSystemSet<Set>();
119 const SystemSetId this_id = this_info.id;
120
121 // Register "After" relationships:
122 // ConfigureSet<Set>().After<A, B>()
123 // means all systems in A and B must run before systems in Set.
124 for (const SystemSetId after_id : after_sets_) {
125 scheduler.AddSetRunsBefore(after_id, this_id);
126 }
127
128 // Register "Before" relationships:
129 // ConfigureSet<Set>().Before<A, B>()
130 // means all systems in Set must run before systems in A and B.
131 for (const SystemSetId before_id : before_sets_) {
132 scheduler.AddSetRunsBefore(this_id, before_id);
133 }
134}
135
136} // namespace helios::app
A sub-application with its own ECS world, systems, and resources.
Definition sub_app.hpp:167
void Apply()
Explicitly applies the configuration.
SystemSetConfig(SubApp &sub_app, Schedule schedule={}) noexcept
Constructs a system set configuration builder.
SystemSetConfig(const SystemSetConfig &)=delete
SystemSetConfig & Before()
Adds ordering constraint: this set runs before specified sets.
SystemSetConfig & After()
Adds ordering constraint: this set runs after specified sets.
SystemSetConfig(SystemSetConfig &&) noexcept=default
Trait to identify valid system set types.
size_t SystemSetId
Type alias for system set type IDs.
STL namespace.