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
21using namespace helios::ecs::types;
22export namespace helios::ecs {
72 template<typename TEntityManager>
74 public:
75 using Handle_type = TEntityManager::Handle_type;
76
87 template<typename T>
88 static void registerType() {
89 ComponentOps ops{
90
91 .onAcquire = [](void *ptr) {
92 if constexpr (HasOnAcquire<T>) {
93 static_cast<T *>(ptr)->onAcquire();
94 }
95 },
96
97 .onRelease = [](void *ptr) {
98 if constexpr (HasOnRelease<T>) {
99 static_cast<T *>(ptr)->onRelease();
100 }
101 },
102
103 .onRemove = [](void *ptr) -> bool {
104 if constexpr (HasOnRemove<T>) {
105 return static_cast<T *>(ptr)->onRemove();
106 }
107 return true;
108 },
109
110 .clone = [](void *managerRaw, const void *sourceRaw, const void *targetRaw) -> void * {
111 auto *manager = static_cast<TEntityManager *>(managerRaw);
112 const auto *source = static_cast<const T *>(sourceRaw);
113 const auto *target = static_cast<const Handle_type *>(targetRaw);
114
115 T *cmp = nullptr;
116 if constexpr (std::is_copy_constructible_v<T>) {
117 cmp = manager->template emplace<T>(*target, *source);
118 } else {
119 return nullptr;
120 }
121
122 if (!cmp) {
123 return nullptr;
124 }
125
126 if constexpr (HasClone<T>) {
127 cmp->onClone(*source);
128 }
129
130 return cmp;
131 },
132
133 .onActivate = [](void *ptr) {
134 if constexpr (HasActivatable<T>) {
135 static_cast<T *>(ptr)->onActivate();
136 }
137 },
138
139 .onDeactivate = [](void *ptr) {
140 if constexpr (HasActivatable<T>) {
141 static_cast<T *>(ptr)->onDeactivate();
142 }
143 },
144
145
146 };
147
148 const auto typeId = ComponentTypeId<Handle_type>::template id<T>();
149
151 }
152 };
153}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.