31 #include "Utilities/ECS/ComponentFactory.h" 35 class ComponentManager;
39 using Deleter = void (*)(
void*);
41 std::aligned_storage_t<sizeof(CResource<char>)> resource;
48 static_assert(
sizeof(
CResource<T>) ==
sizeof(Component::resource),
"storage must fit resource size");
51 this->deleter = [](
void* ptr) { ComponentFactory::Destroy(*
static_cast<CResource<T>*
>(ptr)); };
53 *replace = std::move(component);
60 using ComponentList = MxVector<T>;
62 ComponentList<std::aligned_storage_t<sizeof(Component)>> components;
70 template<
typename T,
typename... Args>
73 this->RemoveComponent<T>();
75 auto component = ComponentFactory::CreateComponent<T>(std::forward<Args>(args)...);
76 auto& data = components.emplace_back();
78 return *
reinterpret_cast<CResource<T>*
>(&result->resource);
84 for (
const auto& component : components)
86 const auto& componentRef = *
reinterpret_cast<const Component*
>(&component);
87 if (componentRef.type == T::ComponentId)
89 return *
reinterpret_cast<const CResource<T>*
>(&componentRef.resource);
96 void RemoveComponent()
98 for (
auto it = components.begin(); it != components.end(); it++)
100 auto& componentRef = *
reinterpret_cast<Component*
>(&*it);
101 if (componentRef.type == T::ComponentId)
103 auto& resource = *
reinterpret_cast<CResource<T>*
>(&componentRef.resource);
104 if (resource.IsValid())
106 ComponentFactory::Destroy(resource);
108 components.erase(it);
115 bool HasComponent()
const 117 return this->GetComponent<T>().IsValid();
120 void RemoveAllComponents()
122 for (
auto& component : components)
124 auto& componentRef = *
reinterpret_cast<Component*
>(&component);
125 componentRef.deleter(static_cast<void*>(&componentRef.resource));
132 this->RemoveAllComponents();
136 #define MAKE_COMPONENT(class_name)\ 138 using Handle = CResource<class_name>;\ 139 private: static constexpr MxEngine::StringId ComponentId = STRING_ID(#class_name);\ 140 void* UserData = (void*)std::numeric_limits<uintptr_t>::max();\ 141 friend class MxObject;\ 142 friend class MxEngine::ComponentManager; \ 143 friend class MxEngine::ComponentFactory Definition: AbstractFactory.h:61
Definition: Component.h:57
Definition: Component.h:37
Definition: Application.cpp:49