MxEngine
Component.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/ECS/ComponentFactory.h"
32 
33 namespace MxEngine
34 {
35  class ComponentManager;
36 
37  struct Component
38  {
39  using Deleter = void (*)(void*);
40 
41  std::aligned_storage_t<sizeof(CResource<char>)> resource;
42  size_t type;
43  Deleter deleter;
44 
45  template<typename T>
46  Component(size_t type, CResource<T>&& component)
47  {
48  static_assert(sizeof(CResource<T>) == sizeof(Component::resource), "storage must fit resource size");
49 
50  this->type = type;
51  this->deleter = [](void* ptr) { ComponentFactory::Destroy(*static_cast<CResource<T>*>(ptr)); };
52  auto* replace = new (&resource) CResource<T>();
53  *replace = std::move(component);
54  }
55  };
56 
58  {
59  template<typename T>
60  using ComponentList = MxVector<T>;
61 
62  ComponentList<std::aligned_storage_t<sizeof(Component)>> components;
63  public:
64  ComponentManager() = default;
65  ComponentManager(const ComponentManager&) = delete;
66  ComponentManager(ComponentManager&&) noexcept = default;
67  ComponentManager& operator=(const ComponentManager&) = delete;
68  ComponentManager& operator=(ComponentManager&&) noexcept = default;
69 
70  template<typename T, typename... Args>
71  CResource<T> AddComponent(Args&&... args)
72  {
73  this->RemoveComponent<T>();
74 
75  auto component = ComponentFactory::CreateComponent<T>(std::forward<Args>(args)...);
76  auto& data = components.emplace_back();
77  Component* result = new (&data) Component(T::ComponentId, std::move(component));
78  return *reinterpret_cast<CResource<T>*>(&result->resource);
79  }
80 
81  template<typename T>
82  CResource<T> GetComponent() const
83  {
84  for (const auto& component : components)
85  {
86  const auto& componentRef = *reinterpret_cast<const Component*>(&component);
87  if (componentRef.type == T::ComponentId)
88  {
89  return *reinterpret_cast<const CResource<T>*>(&componentRef.resource);
90  }
91  }
92  return CResource<T>{ };
93  }
94 
95  template<typename T>
96  void RemoveComponent()
97  {
98  for (auto it = components.begin(); it != components.end(); it++)
99  {
100  auto& componentRef = *reinterpret_cast<Component*>(&*it);
101  if (componentRef.type == T::ComponentId)
102  {
103  auto& resource = *reinterpret_cast<CResource<T>*>(&componentRef.resource);
104  if (resource.IsValid())
105  {
106  ComponentFactory::Destroy(resource);
107  }
108  components.erase(it);
109  return;
110  }
111  }
112  }
113 
114  template<typename T>
115  bool HasComponent() const
116  {
117  return this->GetComponent<T>().IsValid();
118  }
119 
120  void RemoveAllComponents()
121  {
122  for (auto& component : components)
123  {
124  auto& componentRef = *reinterpret_cast<Component*>(&component);
125  componentRef.deleter(static_cast<void*>(&componentRef.resource));
126  }
127  components.clear();
128  }
129 
131  {
132  this->RemoveAllComponents();
133  }
134  };
135 
136 #define MAKE_COMPONENT(class_name)\
137  public:\
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
144 }
Definition: AbstractFactory.h:61
Definition: Component.h:57
Definition: Component.h:37
Definition: Application.cpp:49