diff --git a/Bigfoot/Sources/Engine/Asset/Reference.cpp b/Bigfoot/Sources/Engine/Asset/Reference.cpp deleted file mode 100644 index a9f9ce6..0000000 --- a/Bigfoot/Sources/Engine/Asset/Reference.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/********************************************************************* - * \file Reference.cpp - * - * \author Romain BOULLARD - * \date July 2026 - *********************************************************************/ -#include - -#include - -namespace flatbuffers -{ -Flat::Bigfoot::AnyHardReference Pack(const ::Bigfoot::AnyHardReference& p_ref) -{ - return {Pack(p_ref.GetUUID())}; -} - -::Bigfoot::AnyHardReference UnPack(const Flat::Bigfoot::AnyHardReference& p_ref) -{ - return {UnPack(p_ref.uuid())}; -} - -/****************************************************************************************/ - -Flat::Bigfoot::AnySoftReference Pack(const ::Bigfoot::AnySoftReference& p_ref) -{ - return {Pack(p_ref.GetUUID())}; -} - -::Bigfoot::AnySoftReference UnPack(const Flat::Bigfoot::AnySoftReference& p_ref) -{ - return {UnPack(p_ref.uuid())}; -} -} // namespace flatbuffers diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/Asset.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/Asset.hpp index 264ba6b..c4b945d 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/Asset.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/Asset.hpp @@ -65,7 +65,7 @@ class Asset: public AssetBase flatbuffers::GetRoot(asset->inner_asset()->data())->UnPackTo(&m_asset, nullptr); } - Asset(const Asset& p_asset) = delete; + Asset(const Asset& p_asset) = default; Asset(Asset&& p_asset) = default; ~Asset() = default; @@ -130,7 +130,7 @@ class Asset: public AssetBase return FLAT_ASSET::GetFullyQualifiedName(); } - Asset& operator=(const Asset& p_asset) = delete; + Asset& operator=(const Asset& p_asset) = default; Asset& operator=(Asset&& p_asset) = default; private: diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp index 3ae2212..b17d756 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp @@ -48,7 +48,13 @@ class ContainerHandle } [[nodiscard]] - AssetBase* GetAsset() const + AssetBase* GetAsset() + { + return m_asset; + } + + [[nodiscard]] + const AssetBase* GetAsset() const { return m_asset; } @@ -67,6 +73,8 @@ class ContainerHandle AssetBase* m_asset = nullptr; }; +/****************************************************************************************/ + class AssetContainerBase { public: @@ -98,7 +106,7 @@ class AssetContainerBase } const SlotMapKey key = m_handles.Insert(); - m_uuidToContainerHandleSlotKey.insert({p_uuid, key}); + m_uuidToContainerHandleSlotKey.emplace(p_uuid, key); return *m_handles.Get(key); } @@ -119,6 +127,8 @@ class AssetContainerBase StableSlotMap m_handles; }; +/****************************************************************************************/ + template class AssetContainer: public AssetContainerBase { @@ -134,11 +144,19 @@ class AssetContainer: public AssetContainerBase [[nodiscard]] SlotMapKey Insert(ARGS&&... p_args) { - const SlotMapKey slotKey = m_assets.Insert(std::forward(p_args)...); + SlotMapKey slotKey = m_assets.Insert(std::forward(p_args)...); ASSET* asset = m_assets.Get(slotKey); - m_uuidToAssetSlotKey.insert({asset->GetHeader().uuid, slotKey}); - AcquireHandle(asset->GetHeader().uuid).SetAsset(asset); + if (const auto it = m_uuidToAssetSlotKey.find(asset->GetHeader().uuid); it != m_uuidToAssetSlotKey.end()) + { + m_assets.Remove(slotKey); + slotKey = it->second; + } + else + { + m_uuidToAssetSlotKey.emplace(asset->GetHeader().uuid, slotKey); + AcquireHandle(asset->GetHeader().uuid).SetAsset(asset); + } return slotKey; } diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp index 11d2410..f8023c8 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp @@ -23,6 +23,13 @@ class AssetLibrary ~AssetLibrary() = default; + /** + * Insert an AssetContainer pointer to the library + * + * \tparam ASSET The Asset type + * \param p_assetContainer The asset container + * \return The AssetContainer for the Asset type. Nullptr if not found + */ template [[nodiscard]] bool Insert(AssetContainer* p_assetContainer) @@ -38,24 +45,42 @@ class AssetLibrary return inserted.second; } + /** + * Get the AssetContainer for a given Asset type + * + * \tparam ASSET The Asset type + * \return The AssetContainer for the Asset type. Nullptr if not found + */ template [[nodiscard]] - AssetContainer* Get() + AssetContainer* GetAssetContainer() { static const AssetTypeID typeID = ASSET::GetTypeID(); - return static_cast*>(GetBase(typeID)); + return static_cast*>(GetAssetContainer(typeID)); } + /** + * Get the AssetContainer for a given Asset type + * + * \tparam ASSET The Asset type + * \return The AssetContainer for the Asset type. Nullptr if not found + */ template [[nodiscard]] - const AssetContainer* Get() const + const AssetContainer* GetAssetContainer() const { static const AssetTypeID typeID = ASSET::GetTypeID(); - return static_cast*>(GetBase(typeID)); + return static_cast*>(GetAssetContainer(typeID)); } + /** + * Get the AssetContainer for a given AssetType + * + * \param p_typeID The asset type ID + * \return The AssetContainer for the AssetType. Nullptr if not found + */ [[nodiscard]] - AssetContainerBase* GetBase(const AssetTypeID p_typeID) + AssetContainerBase* GetAssetContainer(const AssetTypeID p_typeID) { if (const auto container = m_assetContainers.find(p_typeID); container != m_assetContainers.end()) { @@ -65,8 +90,14 @@ class AssetLibrary return nullptr; } + /** + * Get the AssetContainer for a given AssetType + * + * \param p_typeID The asset type ID + * \return The AssetContainer for the AssetType. Nullptr if not found + */ [[nodiscard]] - const AssetContainerBase* GetBase(const AssetTypeID p_typeID) const + const AssetContainerBase* GetAssetContainer(const AssetTypeID p_typeID) const { if (const auto container = m_assetContainers.find(p_typeID); container != m_assetContainers.end()) { @@ -76,8 +107,14 @@ class AssetLibrary return nullptr; } + /** + * Find The associated container for a given UUID identifying an Asset + * + * \param p_uuid The UUID + * \return The AssetContainer containing the asset. Nullptr if not found + */ [[nodiscard]] - AssetContainerBase* FindContainer(const UUID& p_uuid) + AssetContainerBase* FindAssetContainerContainerForUUID(const UUID& p_uuid) { for (const auto& [typeID, container]: m_assetContainers) { @@ -90,8 +127,14 @@ class AssetLibrary return nullptr; } + /** + * Find The associated container for a given UUID identifying an Asset + * + * \param p_uuid The UUID + * \return The AssetContainer containing the asset. Nullptr if not found + */ [[nodiscard]] - const AssetContainerBase* FindContainer(const UUID& p_uuid) const + const AssetContainerBase* FindAssetContainerContainerForUUID(const UUID& p_uuid) const { for (const auto& [typeID, container]: m_assetContainers) { @@ -108,10 +151,44 @@ class AssetLibrary AssetLibrary& operator=(AssetLibrary&& p_library) = default; private: + // All the asset containers ankerl::unordered_dense::segmented_map m_assetContainers; }; +/****************************************************************************************/ + +// The global asset library for Bigfoot inline AssetLibrary g_assetLibrary; +inline AssetLibrary* g_activeAssetLibrary = &g_assetLibrary; + +/****************************************************************************************/ + +class ScopedActiveAssetLibrary +{ + public: + ScopedActiveAssetLibrary(AssetLibrary* p_assetLibrary): + m_previous(g_activeAssetLibrary) + { + g_activeAssetLibrary = p_assetLibrary; + } + + ScopedActiveAssetLibrary(const ScopedActiveAssetLibrary& p_scopedActiveAssetLibrary) = default; + ScopedActiveAssetLibrary(ScopedActiveAssetLibrary&& p_scopedActiveAssetLibrary) = default; + + ~ScopedActiveAssetLibrary() + { + g_activeAssetLibrary = m_previous; + } + + ScopedActiveAssetLibrary& operator=(const ScopedActiveAssetLibrary& p_scopedActiveAssetLibrary) = default; + ScopedActiveAssetLibrary& operator=(ScopedActiveAssetLibrary&& p_scopedActiveAssetLibrary) = default; + + private: + /* + * The previously active asset library. + */ + AssetLibrary* m_previous = nullptr; +}; } // namespace Bigfoot #endif diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.fbs b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.fbs index 24e160a..1282b64 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.fbs +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.fbs @@ -10,16 +10,6 @@ struct HardReference (bigfoot_ref_wrapper: "::Bigfoot::HardReference") } struct SoftReference (bigfoot_ref_wrapper: "::Bigfoot::SoftReference") -{ - uuid: UUID; -} - -struct AnyHardReference (native_type: "::Bigfoot::AnyHardReference") -{ - uuid: UUID; -} - -struct AnySoftReference (native_type: "::Bigfoot::AnySoftReference") { uuid: UUID; } \ No newline at end of file diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp index 77cb77d..cf34ca1 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference.hpp @@ -49,7 +49,13 @@ class HardReference } [[nodiscard]] - ASSET* Get() const + 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; } @@ -91,14 +97,28 @@ class HardReference private: void Acquire() { - if (AssetContainerBase* assetContainer = g_assetLibrary.GetBase(GetAssetTypeID()); - assetContainer && m_uuid) + if (!m_uuid) + { + return; + } + + AssetContainerBase* assetContainer = nullptr; + if constexpr (std::is_same_v) + { + assetContainer = g_activeAssetLibrary->FindAssetContainerContainerForUUID(m_uuid); + } + else + { + assetContainer = g_activeAssetLibrary->GetAssetContainer(GetAssetTypeID()); + } + + if (assetContainer) { if (!m_handle) { m_handle = &assetContainer->AcquireHandle(m_uuid); } - m_handle->IncrementHardRefCount(); + m_handle->IncrementSoftRefCount(); } } @@ -147,7 +167,13 @@ class SoftReference } [[nodiscard]] - ASSET* Get() const + 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; } @@ -189,188 +215,22 @@ class SoftReference private: void Acquire() { - if (AssetContainerBase* assetContainer = g_assetLibrary.GetBase(GetAssetTypeID()); - assetContainer && m_uuid) + if (!m_uuid) { - if (!m_handle) - { - m_handle = &assetContainer->AcquireHandle(m_uuid); - } - m_handle->IncrementSoftRefCount(); + return; } - } - void Release() - { - if (m_handle) + AssetContainerBase* assetContainer = nullptr; + if constexpr (std::is_same_v) { - m_handle->DecrementSoftRefCount(); - m_handle = nullptr; + assetContainer = g_activeAssetLibrary->FindAssetContainerContainerForUUID(m_uuid); } - } - - UUID m_uuid = UUID::NULL_UUID; - ContainerHandle* m_handle = nullptr; -}; - -class AnyHardReference -{ - public: - AnyHardReference() = default; - - AnyHardReference(const UUID& p_uuid): - m_uuid(p_uuid) - { - Acquire(); - } - - AnyHardReference(const AnyHardReference& p_ref): - m_uuid(p_ref.m_uuid), - m_handle(p_ref.m_handle) - { - Acquire(); - } - - AnyHardReference(AnyHardReference&& 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; - } - - ~AnyHardReference() - { - Release(); - } - - AnyHardReference& operator=(const AnyHardReference& p_ref) - { - if (this != &p_ref) + else { - Release(); - m_uuid = p_ref.m_uuid; - m_handle = p_ref.m_handle; - Acquire(); + assetContainer = g_activeAssetLibrary->GetAssetContainer(GetAssetTypeID()); } - return *this; - } - AnyHardReference& operator=(AnyHardReference&& 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 AnyHardReference& p_ref) const - { - return m_uuid == p_ref.m_uuid; - } - - private: - void Acquire() - { - if (AssetContainerBase* assetContainer = g_assetLibrary.FindContainer(m_uuid); assetContainer && m_uuid) - { - if (!m_handle) - { - m_handle = &assetContainer->AcquireHandle(m_uuid); - } - m_handle->IncrementHardRefCount(); - } - } - - void Release() - { - if (m_handle) - { - m_handle->DecrementHardRefCount(); - m_handle = nullptr; - } - } - - UUID m_uuid = UUID::NULL_UUID; - ContainerHandle* m_handle = nullptr; -}; - -class AnySoftReference -{ - public: - AnySoftReference() = default; - - AnySoftReference(const UUID& p_uuid): - m_uuid(p_uuid) - { - Acquire(); - } - - AnySoftReference(const AnySoftReference& p_ref): - m_uuid(p_ref.m_uuid), - m_handle(p_ref.m_handle) - { - Acquire(); - } - - AnySoftReference(AnySoftReference&& 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; - } - - ~AnySoftReference() - { - Release(); - } - - AnySoftReference& operator=(const AnySoftReference& p_ref) - { - if (this != &p_ref) - { - Release(); - m_uuid = p_ref.m_uuid; - m_handle = p_ref.m_handle; - Acquire(); - } - return *this; - } - - AnySoftReference& operator=(AnySoftReference&& 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 AnySoftReference& p_ref) const - { - return m_uuid == p_ref.m_uuid; - } - - private: - void Acquire() - { - if (AssetContainerBase* assetContainer = g_assetLibrary.FindContainer(m_uuid); assetContainer && m_uuid) + if (assetContainer) { if (!m_handle) { @@ -393,27 +253,4 @@ class AnySoftReference ContainerHandle* m_handle = nullptr; }; } // namespace Bigfoot - -/****************************************************************************************/ - -namespace Flat -{ -namespace Bigfoot -{ -struct AnyHardReference; -struct AnySoftReference; -} // namespace Bigfoot -} // namespace Flat - -/****************************************************************************************/ - -namespace flatbuffers -{ -Flat::Bigfoot::AnyHardReference Pack(const ::Bigfoot::AnyHardReference& p_ref); -::Bigfoot::AnyHardReference UnPack(const Flat::Bigfoot::AnyHardReference& p_ref); - -Flat::Bigfoot::AnySoftReference Pack(const ::Bigfoot::AnySoftReference& p_ref); -::Bigfoot::AnySoftReference UnPack(const Flat::Bigfoot::AnySoftReference& p_ref); -} // namespace flatbuffers - #endif diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference_generated.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference_generated.hpp index f7ef4e1..1bc2051 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference_generated.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/Reference_generated.hpp @@ -35,18 +35,10 @@ struct HardReference; struct SoftReference; -struct AnyHardReference; - -struct AnySoftReference; - inline const ::flatbuffers::TypeTable *HardReferenceTypeTable(); inline const ::flatbuffers::TypeTable *SoftReferenceTypeTable(); -inline const ::flatbuffers::TypeTable *AnyHardReferenceTypeTable(); - -inline const ::flatbuffers::TypeTable *AnySoftReferenceTypeTable(); - FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(1) HardReference FLATBUFFERS_FINAL_CLASS { private: Flat::Bigfoot::UUID uuid_; @@ -103,62 +95,6 @@ struct SoftReference::Traits { using type = SoftReference; }; -FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(1) AnyHardReference FLATBUFFERS_FINAL_CLASS { - private: - Flat::Bigfoot::UUID uuid_; - - public: - struct Traits; - static const ::flatbuffers::TypeTable *MiniReflectTypeTable() { - return AnyHardReferenceTypeTable(); - } - static FLATBUFFERS_CONSTEXPR_CPP11 const char *GetFullyQualifiedName() { - return "Flat.Bigfoot.AnyHardReference"; - } - AnyHardReference() - : uuid_() { - } - AnyHardReference(const Flat::Bigfoot::UUID &_uuid) - : uuid_(_uuid) { - } - const Flat::Bigfoot::UUID &uuid() const { - return uuid_; - } -}; -FLATBUFFERS_STRUCT_END(AnyHardReference, 16); - -struct AnyHardReference::Traits { - using type = AnyHardReference; -}; - -FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(1) AnySoftReference FLATBUFFERS_FINAL_CLASS { - private: - Flat::Bigfoot::UUID uuid_; - - public: - struct Traits; - static const ::flatbuffers::TypeTable *MiniReflectTypeTable() { - return AnySoftReferenceTypeTable(); - } - static FLATBUFFERS_CONSTEXPR_CPP11 const char *GetFullyQualifiedName() { - return "Flat.Bigfoot.AnySoftReference"; - } - AnySoftReference() - : uuid_() { - } - AnySoftReference(const Flat::Bigfoot::UUID &_uuid) - : uuid_(_uuid) { - } - const Flat::Bigfoot::UUID &uuid() const { - return uuid_; - } -}; -FLATBUFFERS_STRUCT_END(AnySoftReference, 16); - -struct AnySoftReference::Traits { - using type = AnySoftReference; -}; - inline const ::flatbuffers::TypeTable *HardReferenceTypeTable() { static const ::flatbuffers::TypeCode type_codes[] = { { ::flatbuffers::ET_SEQUENCE, 0, 0 } @@ -187,34 +123,6 @@ inline const ::flatbuffers::TypeTable *SoftReferenceTypeTable() { return &tt; } -inline const ::flatbuffers::TypeTable *AnyHardReferenceTypeTable() { - static const ::flatbuffers::TypeCode type_codes[] = { - { ::flatbuffers::ET_SEQUENCE, 0, 0 } - }; - static const ::flatbuffers::TypeFunction type_refs[] = { - Flat::Bigfoot::UUIDTypeTable - }; - static const int64_t values[] = { 0, 16 }; - static const ::flatbuffers::TypeTable tt = { - ::flatbuffers::ST_STRUCT, 1, type_codes, type_refs, nullptr, values, nullptr - }; - return &tt; -} - -inline const ::flatbuffers::TypeTable *AnySoftReferenceTypeTable() { - static const ::flatbuffers::TypeCode type_codes[] = { - { ::flatbuffers::ET_SEQUENCE, 0, 0 } - }; - static const ::flatbuffers::TypeFunction type_refs[] = { - Flat::Bigfoot::UUIDTypeTable - }; - static const int64_t values[] = { 0, 16 }; - static const ::flatbuffers::TypeTable tt = { - ::flatbuffers::ST_STRUCT, 1, type_codes, type_refs, nullptr, values, nullptr - }; - return &tt; -} - } // namespace Bigfoot } // namespace Flat diff --git a/Bigfoot/Tests/Engine/Asset/Asset.cpp b/Bigfoot/Tests/Engine/Asset/Asset.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp b/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp new file mode 100644 index 0000000..a1a51fb --- /dev/null +++ b/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp @@ -0,0 +1,22 @@ +/********************************************************************* + * \file AssetContainer.cpp + * + * \author Romain BOULLARD + * \date July 2026 + *********************************************************************/ +#include + +#include +#include + +namespace Bigfoot +{ +class AssetContainerFixture: public ::testing::Test +{ + protected: + AssetContainer m_containerA; +}; + +/****************************************************************************************/ + +} // namespace Bigfoot diff --git a/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp b/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp new file mode 100644 index 0000000..068e76b --- /dev/null +++ b/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp @@ -0,0 +1,135 @@ +/********************************************************************* + * \file AssetLibrary.cpp + * + * \author Romain BOULLARD + * \date July 2026 + *********************************************************************/ +#include + +#include +#include +#include +#include + +namespace Bigfoot +{ +class AssetLibraryFixture: public ::testing::Test +{ + protected: + AssetContainer m_containerA; + AssetContainer m_containerB; + AssetContainer m_containerC; + + AssetLibrary m_library; +}; + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, Insert_ShouldReturnTrueOnSuccess) +{ + EXPECT_TRUE(m_library.Insert(&m_containerA)); + EXPECT_TRUE(m_library.Insert(&m_containerB)); + EXPECT_TRUE(m_library.Insert(&m_containerC)); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, Insert_ShouldReturnFalseOnDuplicate) +{ + std::ignore = m_library.Insert(&m_containerA); + EXPECT_FALSE(m_library.Insert(&m_containerA)); + + AssetContainer m_containerAA; + EXPECT_FALSE(m_library.Insert(&m_containerAA)); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, GetAssetContainer_ShouldReturnTheAssetContainerIfAvailable) +{ + std::ignore = m_library.Insert(&m_containerA); + std::ignore = m_library.Insert(&m_containerB); + std::ignore = m_library.Insert(&m_containerC); + + EXPECT_EQ(m_library.GetAssetContainer(), &m_containerA); + EXPECT_EQ(m_library.GetAssetContainer(GetAssetTypeID()), &m_containerA); + EXPECT_EQ(m_library.GetAssetContainer(), &m_containerB); + EXPECT_EQ(m_library.GetAssetContainer(GetAssetTypeID()), &m_containerB); + EXPECT_EQ(m_library.GetAssetContainer(), &m_containerC); + EXPECT_EQ(m_library.GetAssetContainer(GetAssetTypeID()), &m_containerC); + + EXPECT_EQ(const_cast(m_library).GetAssetContainer(), &m_containerA); + EXPECT_EQ(const_cast(m_library).GetAssetContainer(GetAssetTypeID()), &m_containerA); + EXPECT_EQ(const_cast(m_library).GetAssetContainer(), &m_containerB); + EXPECT_EQ(const_cast(m_library).GetAssetContainer(GetAssetTypeID()), &m_containerB); + EXPECT_EQ(const_cast(m_library).GetAssetContainer(), &m_containerC); + EXPECT_EQ(const_cast(m_library).GetAssetContainer(GetAssetTypeID()), &m_containerC); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, GetAssetContainer_ShouldReturnNullptrIfTheContainerDoesNotExist) +{ + std::ignore = m_library.Insert(&m_containerB); + + EXPECT_EQ(m_library.GetAssetContainer(), nullptr); + EXPECT_EQ(m_library.GetAssetContainer(GetAssetTypeID()), nullptr); + + EXPECT_EQ(const_cast(m_library).GetAssetContainer(), nullptr); + EXPECT_EQ(const_cast(m_library).GetAssetContainer(GetAssetTypeID()), nullptr); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, FindAssetContainerContainerForUUID_ShouldReturnTheAssetContainerIfAvailable) +{ + std::ignore = m_library.Insert(&m_containerA); + std::ignore = m_library.Insert(&m_containerB); + std::ignore = m_library.Insert(&m_containerC); + + const SlotMapKey keyA = m_containerA.Insert(); + const SlotMapKey keyB = m_containerB.Insert(); + const SlotMapKey keyC = m_containerC.Insert(); + + const UUID uuidA = m_containerA.Get(keyA)->GetHeader().uuid; + const UUID uuidB = m_containerB.Get(keyB)->GetHeader().uuid; + const UUID uuidC = m_containerC.Get(keyC)->GetHeader().uuid; + + EXPECT_EQ(m_library.FindAssetContainerContainerForUUID(uuidA), &m_containerA); + EXPECT_EQ(m_library.FindAssetContainerContainerForUUID(uuidB), &m_containerB); + EXPECT_EQ(m_library.FindAssetContainerContainerForUUID(uuidC), &m_containerC); + + EXPECT_EQ(const_cast(m_library).FindAssetContainerContainerForUUID(uuidA), &m_containerA); + EXPECT_EQ(const_cast(m_library).FindAssetContainerContainerForUUID(uuidB), &m_containerB); + EXPECT_EQ(const_cast(m_library).FindAssetContainerContainerForUUID(uuidC), &m_containerC); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, FindAssetContainerContainerForUUID_ShouldReturnNullptrIfTheContainerDoesNotExist) +{ + std::ignore = m_library.Insert(&m_containerA); + std::ignore = m_library.Insert(&m_containerB); + std::ignore = m_library.Insert(&m_containerC); + + std::ignore = m_containerA.Insert(); + std::ignore = m_containerB.Insert(); + std::ignore = m_containerC.Insert(); + + EXPECT_EQ(m_library.FindAssetContainerContainerForUUID(UUID {}), nullptr); + + EXPECT_EQ(const_cast(m_library).FindAssetContainerContainerForUUID(UUID {}), nullptr); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, ScopedActiveAssetLibrary_ShouldChangeTheActiveAssetLibraryInAScopedWay) +{ + EXPECT_EQ(g_activeAssetLibrary, &g_assetLibrary); + { + ScopedActiveAssetLibrary activeAssetLibraryScoped {&m_library}; + EXPECT_EQ(g_activeAssetLibrary, &m_library); + } + EXPECT_EQ(g_activeAssetLibrary, &g_assetLibrary); +} +} // namespace Bigfoot diff --git a/Bigfoot/Tests/Engine/Asset/Reference.cpp b/Bigfoot/Tests/Engine/Asset/Reference.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Bigfoot/Tests/Engine/BigFile/BigFile.cpp b/Bigfoot/Tests/Engine/BigFile/BigFile.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetA.hpp b/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetA.hpp index 9bee1f1..245c486 100644 --- a/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetA.hpp +++ b/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetA.hpp @@ -27,12 +27,12 @@ class AssetA: public Asset { } - AssetA(const AssetA& p_asset) = delete; + AssetA(const AssetA& p_asset) = default; AssetA(AssetA&& p_asset) = default; ~AssetA() = default; - AssetA& operator=(const AssetA& p_asset) = delete; + AssetA& operator=(const AssetA& p_asset) = default; AssetA& operator=(AssetA&& p_asset) = default; }; } // namespace Bigfoot diff --git a/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC.fbs b/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC.fbs index e21178e..6c537bc 100644 --- a/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC.fbs +++ b/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC.fbs @@ -7,8 +7,8 @@ table InnerTable asset_a_ref: HardReference (native_inline, bigfoot_ref: "::Bigfoot::AssetA"); asset_a_refs: [HardReference] (native_inline, bigfoot_ref: "::Bigfoot::AssetA"); - any_asset: AnyHardReference (native_inline); - any_assets: [AnySoftReference] (native_inline); + any_asset: HardReference (native_inline, bigfoot_ref: "::Bigfoot::AssetBase"); + any_assets: [SoftReference] (native_inline, bigfoot_ref: "::Bigfoot::AssetBase"); } table AssetC diff --git a/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC_generated.hpp b/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC_generated.hpp index eaab7e8..80ab68b 100644 --- a/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC_generated.hpp +++ b/Bigfoot/Tests/Misc/AssetsForTesting/Include/AssetsForTesting/Asset/AssetC_generated.hpp @@ -46,6 +46,10 @@ namespace Bigfoot { class AssetA; } // namespace Bigfoot +namespace Bigfoot { +class AssetBase; +} // namespace Bigfoot + namespace Bigfoot { class AssetB; } // namespace Bigfoot @@ -53,6 +57,10 @@ class AssetB; namespace flatbuffers { Flat::Bigfoot::HardReference PackHardReferenceAssetA(const ::Bigfoot::HardReference<::Bigfoot::AssetA>& p_asset); ::Bigfoot::HardReference<::Bigfoot::AssetA> UnPackHardReferenceAssetA(const Flat::Bigfoot::HardReference& p_asset); +Flat::Bigfoot::HardReference PackHardReferenceAssetBase(const ::Bigfoot::HardReference<::Bigfoot::AssetBase>& p_asset); +::Bigfoot::HardReference<::Bigfoot::AssetBase> UnPackHardReferenceAssetBase(const Flat::Bigfoot::HardReference& p_asset); +Flat::Bigfoot::SoftReference PackSoftReferenceAssetBase(const ::Bigfoot::SoftReference<::Bigfoot::AssetBase>& p_asset); +::Bigfoot::SoftReference<::Bigfoot::AssetBase> UnPackSoftReferenceAssetBase(const Flat::Bigfoot::SoftReference& p_asset); Flat::Bigfoot::HardReference PackHardReferenceAssetB(const ::Bigfoot::HardReference<::Bigfoot::AssetB>& p_asset); ::Bigfoot::HardReference<::Bigfoot::AssetB> UnPackHardReferenceAssetB(const Flat::Bigfoot::HardReference& p_asset); Flat::Bigfoot::SoftReference PackSoftReferenceAssetB(const ::Bigfoot::SoftReference<::Bigfoot::AssetB>& p_asset); @@ -73,8 +81,8 @@ struct InnerTableT : public ::flatbuffers::NativeTable { } ::Bigfoot::HardReference<::Bigfoot::AssetA> asset_a_ref{}; eastl::vector<::Bigfoot::HardReference<::Bigfoot::AssetA>> asset_a_refs{}; - ::Bigfoot::AnyHardReference any_asset{}; - eastl::vector<::Bigfoot::AnySoftReference> any_assets{}; + ::Bigfoot::HardReference<::Bigfoot::AssetBase> any_asset{}; + eastl::vector<::Bigfoot::SoftReference<::Bigfoot::AssetBase>> any_assets{}; }; struct InnerTable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { @@ -99,11 +107,11 @@ struct InnerTable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { const ::flatbuffers::Vector *asset_a_refs() const { return GetPointer *>(VT_ASSET_A_REFS); } - const Flat::Bigfoot::AnyHardReference *any_asset() const { - return GetStruct(VT_ANY_ASSET); + const Flat::Bigfoot::HardReference *any_asset() const { + return GetStruct(VT_ANY_ASSET); } - const ::flatbuffers::Vector *any_assets() const { - return GetPointer *>(VT_ANY_ASSETS); + const ::flatbuffers::Vector *any_assets() const { + return GetPointer *>(VT_ANY_ASSETS); } template bool Verify(::flatbuffers::VerifierTemplate &verifier) const { @@ -111,7 +119,7 @@ struct InnerTable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { VerifyField(verifier, VT_ASSET_A_REF, 1) && VerifyOffset(verifier, VT_ASSET_A_REFS) && verifier.VerifyVector(asset_a_refs()) && - VerifyField(verifier, VT_ANY_ASSET, 1) && + VerifyField(verifier, VT_ANY_ASSET, 1) && VerifyOffset(verifier, VT_ANY_ASSETS) && verifier.VerifyVector(any_assets()) && verifier.EndTable(); @@ -131,10 +139,10 @@ struct InnerTableBuilder { void add_asset_a_refs(::flatbuffers::Offset<::flatbuffers::Vector> asset_a_refs) { fbb_.AddOffset(InnerTable::VT_ASSET_A_REFS, asset_a_refs); } - void add_any_asset(const Flat::Bigfoot::AnyHardReference *any_asset) { + void add_any_asset(const Flat::Bigfoot::HardReference *any_asset) { fbb_.AddStruct(InnerTable::VT_ANY_ASSET, any_asset); } - void add_any_assets(::flatbuffers::Offset<::flatbuffers::Vector> any_assets) { + void add_any_assets(::flatbuffers::Offset<::flatbuffers::Vector> any_assets) { fbb_.AddOffset(InnerTable::VT_ANY_ASSETS, any_assets); } explicit InnerTableBuilder(::flatbuffers::FlatBufferBuilder &_fbb) @@ -152,8 +160,8 @@ inline ::flatbuffers::Offset CreateInnerTable( ::flatbuffers::FlatBufferBuilder &_fbb, const Flat::Bigfoot::HardReference *asset_a_ref = nullptr, ::flatbuffers::Offset<::flatbuffers::Vector> asset_a_refs = 0, - const Flat::Bigfoot::AnyHardReference *any_asset = nullptr, - ::flatbuffers::Offset<::flatbuffers::Vector> any_assets = 0) { + const Flat::Bigfoot::HardReference *any_asset = nullptr, + ::flatbuffers::Offset<::flatbuffers::Vector> any_assets = 0) { InnerTableBuilder builder_(_fbb); builder_.add_any_assets(any_assets); builder_.add_any_asset(any_asset); @@ -277,8 +285,8 @@ inline void InnerTable::UnPackTo(InnerTableT *_o, const ::flatbuffers::resolver_ (void)_resolver; { auto _e = asset_a_ref(); if (_e) _o->asset_a_ref = ::flatbuffers::UnPackHardReferenceAssetA(*_e); } { auto _e = asset_a_refs(); if (_e) { _o->asset_a_refs.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->asset_a_refs[_i] = ::flatbuffers::UnPackHardReferenceAssetA(*_e->Get(_i)); } } else { _o->asset_a_refs.resize(0); } } - { auto _e = any_asset(); if (_e) _o->any_asset = ::flatbuffers::UnPack(*_e); } - { auto _e = any_assets(); if (_e) { _o->any_assets.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->any_assets[_i] = ::flatbuffers::UnPack(*_e->Get(_i)); } } else { _o->any_assets.resize(0); } } + { auto _e = any_asset(); if (_e) _o->any_asset = ::flatbuffers::UnPackHardReferenceAssetBase(*_e); } + { auto _e = any_assets(); if (_e) { _o->any_assets.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->any_assets[_i] = ::flatbuffers::UnPackSoftReferenceAssetBase(*_e->Get(_i)); } } else { _o->any_assets.resize(0); } } } inline ::flatbuffers::Offset CreateInnerTable(::flatbuffers::FlatBufferBuilder &_fbb, const InnerTableT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) { @@ -291,8 +299,8 @@ inline ::flatbuffers::Offset InnerTable::Pack(::flatbuffers::FlatBuf struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const InnerTableT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; auto _asset_a_ref = ::flatbuffers::PackHardReferenceAssetA(_o->asset_a_ref); auto _asset_a_refs = _fbb.CreateVectorOfNativeStructs>(_o->asset_a_refs.data(), _o->asset_a_refs.size(), ::flatbuffers::PackHardReferenceAssetA); - auto _any_asset = ::flatbuffers::Pack(_o->any_asset); - auto _any_assets = _fbb.CreateVectorOfNativeStructs(_o->any_assets.data(), _o->any_assets.size()); + auto _any_asset = ::flatbuffers::PackHardReferenceAssetBase(_o->any_asset); + auto _any_assets = _fbb.CreateVectorOfNativeStructs>(_o->any_assets.data(), _o->any_assets.size(), ::flatbuffers::PackSoftReferenceAssetBase); return Flat::Bigfoot::CreateInnerTable( _fbb, &_asset_a_ref, @@ -350,13 +358,12 @@ inline const ::flatbuffers::TypeTable *InnerTableTypeTable() { static const ::flatbuffers::TypeCode type_codes[] = { { ::flatbuffers::ET_SEQUENCE, 0, 0 }, { ::flatbuffers::ET_SEQUENCE, 1, 0 }, - { ::flatbuffers::ET_SEQUENCE, 0, 1 }, - { ::flatbuffers::ET_SEQUENCE, 1, 2 } + { ::flatbuffers::ET_SEQUENCE, 0, 0 }, + { ::flatbuffers::ET_SEQUENCE, 1, 1 } }; static const ::flatbuffers::TypeFunction type_refs[] = { Flat::Bigfoot::HardReferenceTypeTable, - Flat::Bigfoot::AnyHardReferenceTypeTable, - Flat::Bigfoot::AnySoftReferenceTypeTable + Flat::Bigfoot::SoftReferenceTypeTable }; static const ::flatbuffers::TypeTable tt = { ::flatbuffers::ST_TABLE, 4, type_codes, type_refs, nullptr, nullptr, nullptr @@ -438,6 +445,16 @@ namespace flatbuffers { inline Flat::Bigfoot::HardReference PackHardReferenceAssetA(const ::Bigfoot::HardReference<::Bigfoot::AssetA>& p_asset) { return {Pack(p_asset.GetUUID())}; } inline ::Bigfoot::HardReference<::Bigfoot::AssetA> UnPackHardReferenceAssetA(const Flat::Bigfoot::HardReference& p_asset) { return {UnPack(p_asset.uuid())}; } #endif // FLATBUFFERS_BIGFOOT_REF_HARDREFERENCEASSETA +#ifndef FLATBUFFERS_BIGFOOT_REF_HARDREFERENCEASSETBASE +#define FLATBUFFERS_BIGFOOT_REF_HARDREFERENCEASSETBASE +inline Flat::Bigfoot::HardReference PackHardReferenceAssetBase(const ::Bigfoot::HardReference<::Bigfoot::AssetBase>& p_asset) { return {Pack(p_asset.GetUUID())}; } +inline ::Bigfoot::HardReference<::Bigfoot::AssetBase> UnPackHardReferenceAssetBase(const Flat::Bigfoot::HardReference& p_asset) { return {UnPack(p_asset.uuid())}; } +#endif // FLATBUFFERS_BIGFOOT_REF_HARDREFERENCEASSETBASE +#ifndef FLATBUFFERS_BIGFOOT_REF_SOFTREFERENCEASSETBASE +#define FLATBUFFERS_BIGFOOT_REF_SOFTREFERENCEASSETBASE +inline Flat::Bigfoot::SoftReference PackSoftReferenceAssetBase(const ::Bigfoot::SoftReference<::Bigfoot::AssetBase>& p_asset) { return {Pack(p_asset.GetUUID())}; } +inline ::Bigfoot::SoftReference<::Bigfoot::AssetBase> UnPackSoftReferenceAssetBase(const Flat::Bigfoot::SoftReference& p_asset) { return {UnPack(p_asset.uuid())}; } +#endif // FLATBUFFERS_BIGFOOT_REF_SOFTREFERENCEASSETBASE #ifndef FLATBUFFERS_BIGFOOT_REF_HARDREFERENCEASSETB #define FLATBUFFERS_BIGFOOT_REF_HARDREFERENCEASSETB inline Flat::Bigfoot::HardReference PackHardReferenceAssetB(const ::Bigfoot::HardReference<::Bigfoot::AssetB>& p_asset) { return {Pack(p_asset.GetUUID())}; } diff --git a/Bigfoot/Tests/Utils/Containers/SlotMap.cpp b/Bigfoot/Tests/Utils/Containers/SlotMap.cpp new file mode 100644 index 0000000..e69de29