diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp index ad85d50..c2136a3 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp @@ -17,26 +17,26 @@ namespace Bigfoot template AssetTypeID GetAssetTypeID(); -template -class HardReference +template +class ReferenceBase { public: - HardReference() = default; + ReferenceBase() = default; - HardReference(const UUID& p_uuid): + ReferenceBase(const UUID& p_uuid): m_uuid(p_uuid) { Acquire(); } - HardReference(const HardReference& p_ref): + ReferenceBase(const ReferenceBase& p_ref): m_uuid(p_ref.m_uuid), m_handle(p_ref.m_handle) { Acquire(); } - HardReference(HardReference&& p_ref) noexcept: + ReferenceBase(ReferenceBase&& p_ref) noexcept: m_uuid(std::exchange(p_ref.m_uuid, UUID::NULL_UUID)), m_handle(std::exchange(p_ref.m_handle, nullptr)) { @@ -60,12 +60,12 @@ class HardReference return m_handle ? static_cast(m_handle->GetAsset()) : nullptr; } - ~HardReference() + ~ReferenceBase() { Release(); } - HardReference& operator=(const HardReference& p_ref) + ReferenceBase& operator=(const ReferenceBase& p_ref) { if (this != &p_ref) { @@ -77,7 +77,7 @@ class HardReference return *this; } - HardReference& operator=(HardReference&& p_ref) noexcept + ReferenceBase& operator=(ReferenceBase&& p_ref) noexcept { if (this != &p_ref) { @@ -89,7 +89,7 @@ class HardReference } [[nodiscard]] - bool operator==(const HardReference& p_ref) const + bool operator==(const ReferenceBase& p_ref) const { return m_uuid == p_ref.m_uuid; } @@ -108,7 +108,7 @@ class HardReference { m_handle = assetContainer->AcquireContainerHandle(m_uuid); } - m_handle->IncrementHardRefCount(); + DERIVED::IncrementCount(*m_handle); } } @@ -116,7 +116,7 @@ class HardReference { if (m_handle) { - m_handle->DecrementHardRefCount(); + DERIVED::DecrementCount(*m_handle); m_handle = nullptr; } } @@ -126,111 +126,37 @@ class HardReference }; template -class SoftReference +class HardReference: public ReferenceBase, ASSET> { - public: - SoftReference() = default; - - SoftReference(const UUID& p_uuid): - m_uuid(p_uuid) - { - Acquire(); - } - - SoftReference(const SoftReference& p_ref): - m_uuid(p_ref.m_uuid), - m_handle(p_ref.m_handle) - { - Acquire(); - } - - SoftReference(SoftReference&& p_ref) noexcept: - m_uuid(std::exchange(p_ref.m_uuid, UUID::NULL_UUID)), - m_handle(std::exchange(p_ref.m_handle, nullptr)) - { - } - - [[nodiscard]] - const UUID& GetUUID() const - { - return m_uuid; - } - - [[nodiscard]] - const ASSET* Get() const - { - return m_handle ? static_cast(m_handle->GetAsset()) : nullptr; - } - - [[nodiscard]] - ASSET* Get() - { - return m_handle ? static_cast(m_handle->GetAsset()) : nullptr; - } - - ~SoftReference() - { - Release(); - } - - SoftReference& operator=(const SoftReference& p_ref) - { - if (this != &p_ref) - { - Release(); - m_uuid = p_ref.m_uuid; - m_handle = p_ref.m_handle; - Acquire(); - } - return *this; - } - - SoftReference& operator=(SoftReference&& p_ref) noexcept - { - if (this != &p_ref) - { - Release(); - m_uuid = std::exchange(p_ref.m_uuid, UUID::NULL_UUID); - m_handle = std::exchange(p_ref.m_handle, nullptr); - } - return *this; - } - - [[nodiscard]] - bool operator==(const SoftReference& p_ref) const - { - return m_uuid == p_ref.m_uuid; - } - private: - void Acquire() + static void IncrementCount(ContainerHandle& p_handle) { - if (!m_uuid) - { - return; - } - - if (AssetContainerBase* assetContainer = g_activeAssetLibrary->GetAssetContainer(GetAssetTypeID())) - { - if (!m_handle) - { - m_handle = assetContainer->AcquireContainerHandle(m_uuid); - } - m_handle->IncrementSoftRefCount(); - } + p_handle.IncrementHardRefCount(); } - void Release() + static void DecrementCount(ContainerHandle& p_handle) { - if (m_handle) - { - m_handle->DecrementSoftRefCount(); - m_handle = nullptr; - } + p_handle.DecrementHardRefCount(); } - UUID m_uuid = UUID::NULL_UUID; - ContainerHandle* m_handle = nullptr; + friend ReferenceBase, ASSET>; +}; + +template +class SoftReference: public ReferenceBase, ASSET> +{ + private: + static void IncrementCount(ContainerHandle& p_handle) + { + p_handle.IncrementSoftRefCount(); + } + + static void DecrementCount(ContainerHandle& p_handle) + { + p_handle.DecrementSoftRefCount(); + } + + friend ReferenceBase, ASSET>; }; } // namespace Bigfoot #endif