Skip to main content

engine/ecs/ComponentReflector.ixx File

Compile-time reflection for component lifecycle hook registration. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine

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

namespaceecs

Core Entity-Component-System architecture. More...

Classes Index

classComponentReflector

Generates and registers ComponentOps for a component type. More...

Description

Compile-time reflection for component lifecycle hook registration.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file ComponentReflector.ixx
3 * @brief Compile-time reflection for component lifecycle hook registration.
4 */
5module;
6
7#include <type_traits>
8
9export module helios.engine.ecs.ComponentReflector;
10
11import helios.engine.ecs.EntityHandle;
12import helios.engine.ecs.ComponentOps;
13import helios.engine.ecs.Traits;
14import helios.engine.ecs.types.ComponentTypeId;
15import helios.engine.ecs.ComponentOpsRegistry;
16import helios.engine.ecs.EntityManager;
17
18export namespace helios::engine::ecs {
19
20 /**
21 * @brief Generates and registers ComponentOps for a component type.
22 *
23 * @details `ComponentReflector` uses compile-time trait detection to
24 * generate appropriate function pointers for each lifecycle hook that
25 * a component type implements. This enables zero-overhead conditional
26 * invocation at runtime.
27 *
28 * ## Usage
29 *
30 * ```cpp
31 * // Register a component type
32 * ComponentReflector::registerType<HealthComponent>();
33 *
34 * // Or use the module registry pattern
35 * inline void registerComponents() {
36 * using R = ComponentReflector;
37 * R::registerType<CollisionComponent>();
38 * R::registerType<AabbColliderComponent>();
39 * }
40 * ```
41 *
42 * ## Trait Detection
43 *
44 * The reflector checks for the following traits at compile time:
45 * - `HasOnAcquire` - `onAcquire()` method
46 * - `HasOnRelease` - `onRelease()` method
47 * - `HasOnRemove` - `onRemove()` method returning bool
48 * - `HasToggleable` - `enable()` and `disable()` methods
49 * - `HasClone` - `onClone(const T&)` method
50 * - `HasActivatable` - `onActivate()` and `onDeactivate()` methods
51 *
52 * @see ComponentOps
53 * @see ComponentOpsRegistry
54 * @see Traits
55 */
57
58 public:
59
60 /**
61 * @brief Registers a component type with the reflection system.
62 *
63 * @details Generates `ComponentOps` function pointers based on which
64 * lifecycle hooks the type implements. Uses `if constexpr` for
65 * zero-overhead when hooks are not present.
66 *
67 * @tparam T The component type to register. Must be copy-constructible
68 * for cloning support.
69 */
70 template<typename T>
71 static void registerType() {
72
73 ComponentOps ops{
74
75 .onAcquire = [](void* ptr) {
76 if constexpr (helios::engine::ecs::traits::HasOnAcquire<T>) {
77 static_cast<T*>(ptr)->onAcquire();
78 }
79 },
80
81 .onRelease = [](void* ptr) {
82 if constexpr (helios::engine::ecs::traits::HasOnRelease<T>) {
83 static_cast<T*>(ptr)->onRelease();
84 }
85 },
86
87 .onRemove = [](void* ptr) -> bool {
88 if constexpr (helios::engine::ecs::traits::HasOnRemove<T>) {
89 return static_cast<T*>(ptr)->onRemove();
90 }
91 return true;
92 },
93
94 .enable = [](void* ptr) {
95 if constexpr (helios::engine::ecs::traits::HasToggleable<T>) {
96 static_cast<T*>(ptr)->enable();
97 }
98 },
99
100 .disable = [](void* ptr) {
101 if constexpr (helios::engine::ecs::traits::HasToggleable<T>) {
102 static_cast<T*>(ptr)->disable();
103 }
104 },
105
106 .clone = [](void* managerRaw, const void* sourceRaw, const EntityHandle* target) -> void* {
107
108 auto* manager = static_cast<helios::engine::ecs::EntityManager*>(managerRaw);
109 const auto* source = static_cast<const T*>(sourceRaw);
110
111 T* cmp = nullptr;
112 if constexpr (std::is_copy_constructible_v<T>) {
113 cmp = manager->template emplace<T>(*target, *source);
114 } else {
115 return nullptr;
116 }
117
118 if (!cmp) {
119 return nullptr;
120 }
121
122 if constexpr (helios::engine::ecs::traits::HasClone<T>) {
123 cmp->onClone(*source);
124 }
125
126 return cmp;
127 },
128
129 .onActivate = [](void* ptr) {
130 if constexpr (helios::engine::ecs::traits::HasActivatable<T>) {
131 static_cast<T*>(ptr)->onActivate();
132 }
133 },
134
135 .onDeactivate = [](void* ptr) {
136 if constexpr (helios::engine::ecs::traits::HasActivatable<T>) {
137 static_cast<T*>(ptr)->onDeactivate();
138 }
139 },
140
141 };
142
144
146
147 }
148
149 };
150
151
152}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.