Skip to main content

SystemRegistry.ixx File

Registry for managing System instances within a game loop phase. More...

Included Headers

#include <memory> #include <vector> #include <cassert> #include <helios.engine.ecs.System>

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespaceruntime

Runtime infrastructure for game execution and lifecycle orchestration. More...

namespaceworld

World state management and per-frame update context. More...

Classes Index

classSystemRegistry

Container for System instances within a game loop pass. More...

Description

Registry for managing System instances within a game loop phase.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file SystemRegistry.ixx
3 * @brief Registry for managing System instances within a game loop phase.
4 */
5module;
6
7#include <memory>
8#include <vector>
9#include <cassert>
10
11
12export module helios.engine.runtime.world.SystemRegistry;
13
14import helios.engine.ecs.System;
15
16
17export namespace helios::engine::runtime::world {
18
19 /**
20 * @brief Container for System instances within a game loop pass.
21 *
22 * @details SystemRegistry manages a collection of Systems that execute
23 * together within a single pass of the game loop. It provides type-safe
24 * registration, lookup, and iteration over Systems.
25 *
26 * ## Ownership
27 *
28 * The registry owns all registered Systems via `std::unique_ptr`. Systems
29 * are destroyed when the registry is destroyed.
30 *
31 * ## Type-Safe Access
32 *
33 * Systems can be retrieved by type using `getSystem<T>()`, enabling
34 * inter-system communication when necessary.
35 *
36 * Example:
37 * ```cpp
38 * SystemRegistry registry;
39 *
40 * // Add systems
41 * registry.add<Move2DSystem>(gameWorld);
42 * registry.add<CollisionSystem>(gameWorld);
43 *
44 * // Check for system
45 * if (registry.hasSystem<CollisionSystem>()) {
46 * auto* collision = registry.getSystem<CollisionSystem>();
47 * }
48 *
49 * // Iterate all systems
50 * for (auto& system : registry.systems()) {
51 * system->update(ctx);
52 * }
53 * ```
54 *
55 * @see System
56 * @see GameLoop
57 * @see GameLoopPhase
58 */
60
61 /**
62 * @brief Collection of owned System instances.
63 */
64 std::vector<std::unique_ptr<helios::engine::ecs::System>> systems_;
65
66 public:
67
68 /**
69 * @brief Returns a reference to the system collection.
70 *
71 * @return Reference to the vector of System unique_ptrs.
72 */
73 [[nodiscard]] std::vector<std::unique_ptr<helios::engine::ecs::System>>& systems() noexcept {
74 return systems_;
75 }
76
77 /**
78 * @brief Adds a new System of type T to the registry.
79 *
80 * @tparam T The System type to add. Must derive from System.
81 * @tparam Args Constructor argument types.
82 *
83 * @param args Arguments forwarded to the System constructor.
84 *
85 * @return Reference to the newly added System.
86 *
87 * @pre No System of type T is already registered.
88 */
89 template<typename T, typename... Args>
90 requires std::is_base_of_v<helios::engine::ecs::System, T>
91 T& add(Args&&... args) {
92 assert(!hasSystem<T>() && "System already registered with GameLoopPhase");
93 auto system_ptr = std::make_unique<T>(std::forward<Args>(args)...);
94 T* raw_ptr = system_ptr.get();
95 systems_.push_back(std::move(system_ptr));
96 return *raw_ptr;
97 }
98
99 /**
100 * @brief Checks if a System of type T is registered.
101 *
102 * @tparam T The System type to check for.
103 *
104 * @return True if the System exists, false otherwise.
105 */
106 template<typename T>
107 requires std::is_base_of_v<helios::engine::ecs::System, T>
108 [[nodiscard]] bool hasSystem() const {
109 return getSystem<T>() != nullptr;
110 }
111
112 /**
113 * @brief Retrieves a System by type.
114 *
115 * @tparam T The System type to retrieve.
116 *
117 * @return Pointer to the System if found, nullptr otherwise.
118 *
119 * @note Uses dynamic_cast for type checking. O(n) complexity.
120 */
121 template<typename T>
122 requires std::is_base_of_v<helios::engine::ecs::System, T>
123 [[nodiscard]] T* getSystem() const {
124 for (const auto& system : systems_) {
125 if (auto* sys = dynamic_cast<T*>(system.get())) {
126 return sys;
127 }
128 }
129
130 return nullptr;
131 }
132
133 };
134
135
136}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.