Skip to main content

ComponentReflector Class

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

Declaration

class helios::engine::ecs::ComponentReflector { ... }

Public Static Functions Index

template <typename T>
static voidregisterType ()

Registers a component type with the reflection system. More...

Description

Generates and registers ComponentOps for a component type.

`ComponentReflector` uses compile-time trait detection to generate appropriate function pointers for each lifecycle hook that a component type implements. This enables zero-overhead conditional invocation at runtime.

## Usage

```cpp // Register a component type ComponentReflector::registerType<HealthComponent>();

// Or use the module registry pattern inline void registerComponents() { using R = ComponentReflector; R::registerType<CollisionComponent>(); R::registerType<AabbColliderComponent>(); } ```

## Trait Detection

The reflector checks for the following traits at compile time:

  • `HasOnAcquire` - `onAcquire()` method
  • `HasOnRelease` - `onRelease()` method
  • `HasOnRemove` - `onRemove()` method returning bool
  • `HasToggleable` - `enable()` and `disable()` methods
  • `HasClone` - `onClone(const T&)` method
  • `HasActivatable` - `onActivate()` and `onDeactivate()` methods
See Also

ComponentOps

See Also

ComponentOpsRegistry

See Also

Traits

Definition at line 56 of file ComponentReflector.ixx.

Public Static Functions

registerType()

template <typename T>
void helios::engine::ecs::ComponentReflector::registerType ()
inline static

Registers a component type with the reflection system.

Generates `ComponentOps` function pointers based on which lifecycle hooks the type implements. Uses `if constexpr` for zero-overhead when hooks are not present.

Template Parameters
T

The component type to register. Must be copy-constructible for cloning support.

Definition at line 71 of file ComponentReflector.ixx.

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 }

References helios::engine::ecs::types::ComponentTypeId::id and helios::engine::ecs::ComponentOpsRegistry::setOps.


The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.