Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
system.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
6
7#include <ctti/name.hpp>
8#include <ctti/type_id.hpp>
9
10#include <concepts>
11#include <cstddef>
12#include <string_view>
13
14namespace helios::app {
15
16class SystemContext;
17
18} // namespace helios::app
19
20namespace helios::ecs {
21
22/**
23 * @brief Base class for all systems.
24 * @details Systems are responsible for processing entities and their components.
25 * Derived classes must implement:
26 * - `void Update(helios::ecs::SystemContext&)` for per-frame or per-tick updates
27 * - `static constexpr helios::app::AccessPolicy GetAccessPolicy()` to declare data access requirements
28 * - `static constexpr std::string_view GetName() noexcept` (optional)
29 *
30 * @example
31 * @code
32 * struct MovementSystem : public helios::ecs::System {
33 * public:
34 * ~System() override = default;
35 *
36 * static constexpr std::string_view GetName() noexcept { return "MovementSystem"; }
37 *
38 * static constexpr auto GetAccessPolicy() {
39 * return helios::app::AccessPolicy()
40 * .Query<Transform&, const Velocity&>()
41 * .ReadResources<CommandLineArgs>();
42 * }
43 *
44 * void Update(helios::app::SystemContext& ctx) override {
45 * const auto& args = ctx.ReadResource<CommandLineArgs>();
46 * const int fps = args.TryGet<int>("fps");
47 * const float dt = 1.0F / fps;
48 *
49 * auto query = ctx.Query().With<Transform, Velocity>().Get<Transform&, const Velocity&>();
50 * for (auto&& [transform, velocity] : query) {
51 * transform.position += velocity.value * dt;
52 * }
53 * }
54 * };
55 * @endcode
56 */
57class System {
58public:
59 virtual ~System() = default;
60
61 /**
62 * @brief Updates the system. This method is called every frame or tick.
63 * @param ctx Context providing validated access to world and resources
64 */
65 virtual void Update(app::SystemContext& ctx) = 0;
66};
67
68/**
69 * @brief Concept for valid system types.
70 * @details A valid system must:
71 * - Derive from System
72 * - Be default constructible
73 * - Provide `static constexpr app::AccessPolicy GetAccessPolicy() noexcept`
74 */
75template <typename T>
76concept SystemTrait = std::derived_from<T, System> && std::constructible_from<T> && requires {
77 { T::GetAccessPolicy() } -> std::same_as<app::AccessPolicy>;
78};
79
80/**
81 * @brief Concept for systems with custom names.
82 * @details A system with name trait must satisfy SystemTrait and provide:
83 * - `static constexpr std::string_view GetName() noexcept`
84 */
85template <typename T>
86concept SystemWithNameTrait = SystemTrait<T> && requires {
87 { T::GetName() } -> std::same_as<std::string_view>;
88};
89
90/**
91 * @brief Type ID for systems.
92 */
94
95/**
96 * @brief Gets type ID for a system type.
97 * @tparam T System type
98 * @return Unique type ID for the system
99 */
100template <typename T>
102 return ctti::type_index_of<T>().hash();
103}
104
105/**
106 * @brief Gets the name of a system.
107 * @details Returns provided name or type name as fallback.
108 * @tparam T System type
109 * @return System name
110 */
111template <SystemTrait T>
112[[nodiscard]] constexpr std::string_view SystemNameOf() noexcept {
113 if constexpr (SystemWithNameTrait<T>) {
114 return T::GetName();
115 } else {
116 return ctti::name_of<T>();
117 }
118}
119
120} // namespace helios::ecs
Per-system execution context with validated access.
virtual ~System()=default
virtual void Update(app::SystemContext &ctx)=0
Updates the system. This method is called every frame or tick.
Concept for valid system types.
Definition system.hpp:76
Concept for systems with custom names.
Definition system.hpp:86
constexpr std::string_view SystemNameOf() noexcept
Gets the name of a system.
Definition system.hpp:112
constexpr SystemTypeId SystemTypeIdOf() noexcept
Gets type ID for a system type.
Definition system.hpp:101
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481