31 #include "Utilities/UUID/UUID.h" 32 #include "Utilities/VectorPool/VectorPool.h" 43 template<
typename... Args>
45 : value(std::forward<Args>(value)...), uuid(uuid)
49 ManagedResource(
const ManagedResource&) =
delete;
50 ManagedResource(ManagedResource&&) noexcept(std::is_nothrow_move_constructible_v<T>) =
default;
51 ManagedResource& operator=(
const ManagedResource&) =
delete;
52 ManagedResource& operator=(ManagedResource&&) noexcept(std::is_nothrow_move_assignable_v<T>) =
default;
56 uuid = UUIDGenerator::GetNull();
60 template<
typename T,
typename Factory>
66 #if defined(MXENGINE_DEBUG) 70 static constexpr
size_t InvalidHandle = std::numeric_limits<size_t>::max();
75 ++this->Dereference().refCount;
82 auto& resource = this->Dereference();
83 if ((--resource.refCount) == 0)
90 auto& resource = AccessThis(this->handle);
92 #if defined(MXENGINE_DEBUG) 93 this->_resourcePtr = &resource;
101 Factory::Destroy(resource);
106 return Factory::template Get<T>()[handle];
110 : uuid(UUIDGenerator::GetNull()), handle(InvalidHandle)
116 : uuid(uuid), handle(handle)
122 : uuid(wrapper.uuid), handle(wrapper.handle)
125 #if defined(MXENGINE_DEBUG) 126 this->_resourcePtr = wrapper._resourcePtr;
134 #if defined(MXENGINE_DEBUG) 135 this->_resourcePtr = wrapper._resourcePtr;
138 this->uuid = wrapper.uuid;
139 this->handle = wrapper.handle;
146 : uuid(wrapper.uuid), handle(wrapper.handle)
148 #if defined(MXENGINE_DEBUG) 149 this->_resourcePtr = wrapper._resourcePtr;
151 wrapper.handle = InvalidHandle;
157 this->uuid = wrapper.uuid;
158 this->handle = wrapper.handle;
159 wrapper.handle = InvalidHandle;
161 #if defined(MXENGINE_DEBUG) 162 this->_resourcePtr = wrapper._resourcePtr;
167 [[nodiscard]]
bool IsValid()
const 169 return handle != InvalidHandle && Dereference().uuid == uuid;
179 MX_ASSERT(this->IsValid());
180 if (!this->IsValid())
return nullptr;
181 return this->GetUnchecked();
184 [[nodiscard]]
const T* operator->()
const 186 MX_ASSERT(this->IsValid());
187 if (!this->IsValid())
return nullptr;
188 return this->GetUnchecked();
191 [[nodiscard]] T& operator*()
193 MX_ASSERT(this->IsValid());
194 return *this->GetUnchecked();
197 [[nodiscard]]
const T& operator*()
const 199 MX_ASSERT(this->IsValid());
200 return *this->GetUnchecked();
203 [[nodiscard]] T* GetUnchecked()
205 return &this->Dereference().value;
208 [[nodiscard]]
const T* GetUnchecked()
const 210 return &this->Dereference().value;
213 [[nodiscard]]
auto GetHandle()
const 218 [[nodiscard]]
const auto& GetUUID()
const 223 [[nodiscard]]
bool operator==(
const Resource& wrapper)
const 225 return this->handle == wrapper.handle && this->uuid == wrapper.uuid;
228 [[nodiscard]]
bool operator!=(
const Resource& wrapper)
const 230 return !(*
this == wrapper);
239 template<
typename T,
typename Factory>
244 mutable Factory* factory;
246 #if defined(MXENGINE_DEBUG) 250 static constexpr
size_t InvalidHandle = std::numeric_limits<size_t>::max();
255 ++this->Dereference().refCount;
262 auto& resource = this->Dereference();
263 if ((--resource.refCount) == 0)
270 auto& resource = AccessThis(this->handle);
272 #if defined(MXENGINE_DEBUG) 273 this->_resourcePtr = &resource;
281 this->factory->Destroy(resource);
286 return this->factory->template Get<T>()[handle];
290 : uuid(UUIDGenerator::GetNull()), handle(InvalidHandle), factory(
nullptr)
296 : uuid(uuid), handle(handle), factory(factory)
302 : uuid(wrapper.uuid), handle(wrapper.handle), factory(wrapper.factory)
311 this->uuid = wrapper.uuid;
312 this->handle = wrapper.handle;
313 this->factory = wrapper.factory;
320 : uuid(wrapper.uuid), handle(wrapper.handle), factory(wrapper.factory)
322 wrapper.handle = InvalidHandle;
328 this->uuid = wrapper.uuid;
329 this->handle = wrapper.handle;
330 this->factory = wrapper.factory;
331 wrapper.handle = InvalidHandle;
336 [[nodiscard]]
bool IsValid()
const 338 return handle != InvalidHandle && Dereference().uuid == uuid;
348 MX_ASSERT(this->IsValid());
349 if (!this->IsValid())
return nullptr;
350 return this->GetUnchecked();
353 [[nodiscard]]
const T* operator->()
const 355 MX_ASSERT(this->IsValid());
356 if (!this->IsValid())
return nullptr;
357 return this->GetUnchecked();
360 [[nodiscard]] T& operator*()
362 MX_ASSERT(this->IsValid());
363 return *this->GetUnchecked();
366 [[nodiscard]]
const T& operator*()
const 368 MX_ASSERT(this->IsValid());
369 return *this->GetUnchecked();
372 [[nodiscard]] T* GetUnchecked()
374 return &this->Dereference().value;
377 [[nodiscard]]
const T* GetUnchecked()
const 379 return &this->Dereference().value;
382 [[nodiscard]]
auto GetHandle()
const 387 [[nodiscard]]
const auto& GetUUID()
const 398 template<
typename T,
typename... Args>
408 if constexpr (std::is_same<T, U>::value)
411 return static_cast<Base*
>(
this)->
template GetPool<U>();
415 void ForEach(F&& func)
418 static_cast<Base*
>(
this)->ForEach(std::forward<F>(func));
431 static_assert(std::is_same<T, U>::value,
"cannot find appropriate Factory<T>");
436 void ForEach(F&& func)
442 template<
typename... Args>
449 inline static Factory* factory =
nullptr;
459 if (factory ==
nullptr)
463 static void Clone(
Factory* other)
471 return factory->template GetPool<U>();
474 template<
typename T,
typename... ConstructArgs>
477 MX_ASSERT(factory !=
nullptr);
478 UUID uuid = UUIDGenerator::Get();
479 auto& pool = factory->template GetPool<T>();
480 size_t index = pool.Allocate(uuid, std::forward<ConstructArgs>(args)...);
487 factory->template GetPool<T>().Deallocate(resource.GetHandle());
Definition: AbstractFactory.h:61
Definition: VectorPool.h:42
Definition: AbstractFactory.h:443
Definition: AbstractFactory.h:399
Definition: AbstractFactory.h:240
Definition: AbstractFactory.h:37
Definition: Application.cpp:49