Skip to main content

ComponentReflector Class Template

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

Declaration

template <typename TEntityManager> class helios::ecs::ComponentReflector<TEntityManager> { ... }

Public Member Typedefs Index

template <typename TEntityManager>
usingHandle_type = TEntityManager::Handle_type

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.

## Template Parameters

The reflector is templated on `THandle` and `TEntityManager` so that the generated clone function can cast the type-erased `void*` pointers back to the concrete types of the target domain. This also ensures that `ComponentOpsRegistry<THandle>` stores the ops in the correct per-domain registry.

## Usage

```cpp // Register a component type for the game domain ComponentReflector<GameHandle, GameEM>::registerType<HealthComponent>();

// Or use the module registry pattern inline void registerComponents() { using R = ComponentReflector<GameHandle, GameEM>; 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
Template Parameters
THandle

The concrete `EntityHandle<TStrongId>` specialisation. Used to scope `ComponentTypeId` and `ComponentOpsRegistry`.

TEntityManager

The concrete `EntityManager` specialisation. Used by the clone function to emplace components on the target entity.

See Also

ComponentOps

See Also

ComponentOpsRegistry

See Also

Traits

Definition at line 75 of file ComponentReflector.ixx.

Public Member Typedefs

Handle_type

template <typename TEntityManager>
using helios::ecs::ComponentReflector< TEntityManager >::Handle_type = TEntityManager::Handle_type

Definition at line 80 of file ComponentReflector.ixx.

80 using Handle_type = TEntityManager::Handle_type;

Public Static Functions

registerType()

template <typename T>
void helios::ecs::ComponentReflector< TEntityManager >::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 93 of file ComponentReflector.ixx.

93 static void registerType() {
94
95 ComponentOps ops{
96
97 .onAcquire = [](void* ptr) {
98 if constexpr (HasOnAcquire<T>) {
99 static_cast<T*>(ptr)->onAcquire();
100 }
101 },
102
103 .onRelease = [](void* ptr) {
104 if constexpr (HasOnRelease<T>) {
105 static_cast<T*>(ptr)->onRelease();
106 }
107 },
108
109 .onRemove = [](void* ptr) -> bool {
110 if constexpr (HasOnRemove<T>) {
111 return static_cast<T*>(ptr)->onRemove();
112 }
113 return true;
114 },
115
116 .enable = [](void* ptr) {
117 if constexpr (HasToggleable<T>) {
118 static_cast<T*>(ptr)->enable();
119 }
120 },
121
122 .disable = [](void* ptr) {
123 if constexpr (HasToggleable<T>) {
124 static_cast<T*>(ptr)->disable();
125 }
126 },
127
128 .clone = [](void* managerRaw, const void* sourceRaw, const void* targetRaw) -> void* {
129
130 auto* manager = static_cast<TEntityManager*>(managerRaw);
131 const auto* source = static_cast<const T*>(sourceRaw);
132 const auto* target = static_cast<const Handle_type*>(targetRaw);
133
134 T* cmp = nullptr;
135 if constexpr (std::is_copy_constructible_v<T>) {
136 cmp = manager->template emplace<T>(*target, *source);
137 } else {
138 return nullptr;
139 }
140
141 if (!cmp) {
142 return nullptr;
143 }
144
145 if constexpr (HasClone<T>) {
146 cmp->onClone(*source);
147 }
148
149 return cmp;
150 },
151
152 .onActivate = [](void* ptr) {
153 if constexpr (HasActivatable<T>) {
154 static_cast<T*>(ptr)->onActivate();
155 }
156 },
157
158 .onDeactivate = [](void* ptr) {
159 if constexpr (HasActivatable<T>) {
160 static_cast<T*>(ptr)->onDeactivate();
161 }
162 },
163
164 };
165
166 const auto typeId = ComponentTypeId<Handle_type>::template id<T>();
167
169
170 }

Reference helios::ecs::ComponentOpsRegistry< THandle >::setOps.


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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.