Skip to main content

ComponentReflector.ixx File

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

Included Headers

Namespaces Index

namespacehelios
namespaceecs

Classes Index

classComponentReflector<TEntityManager>

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
5module;
6
7#include <type_traits>
8
9export module helios.ecs.ComponentReflector;
10
11import helios.ecs.concepts.Traits;
12
13import helios.ecs.types.ComponentTypeId;
14import helios.ecs.types.EntityHandle;
15import helios.ecs.types.ComponentOps;
16
17import helios.ecs.ComponentOpsRegistry;
18import helios.ecs.EntityManager;
19
20using namespace helios::ecs::concepts::traits;
21using namespace helios::ecs::types;
22export namespace helios::ecs {
23
74 template<typename TEntityManager>
76
77
78 public:
79
80 using Handle_type = TEntityManager::Handle_type;
81
92 template<typename T>
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 }
171
172 };
173
174
175}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.