diff --git a/Bigfoot/Sources/Engine/Asset/AssetLibrary.cpp b/Bigfoot/Sources/Engine/Asset/AssetLibrary.cpp new file mode 100644 index 0000000..e78d2f4 --- /dev/null +++ b/Bigfoot/Sources/Engine/Asset/AssetLibrary.cpp @@ -0,0 +1,15 @@ +/********************************************************************* + * \file AssetLibrary.cpp + * + * \author Romain BOULLARD + * \date August 2026 + *********************************************************************/ +#include + +namespace Bigfoot +{ +void SetActiveAssetLibrary(AssetLibrary* p_assetLibrary) +{ + g_activeAssetLibrary = p_assetLibrary; +} +} // namespace Bigfoot diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp index a0dd49d..855a850 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetContainer.hpp @@ -27,6 +27,17 @@ class ContainerHandle ~ContainerHandle() = default; + /* + * Get the HardRef count + * + * \return The HardRef count. + */ + [[nodiscard]] + std::uint32_t GetHardRefCount() const + { + return m_hardRefCount; + } + /* * Increment the HardRef count * @@ -45,6 +56,17 @@ class ContainerHandle --m_hardRefCount; } + /* + * Get the SoftRef count + * + * \return The SoftRef count. + */ + [[nodiscard]] + std::uint32_t GetSoftRefCount() const + { + return m_softRefCount; + } + /* * Increment the SoftRef count * diff --git a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp index 49c540c..627b931 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/Asset/AssetLibrary.hpp @@ -163,13 +163,22 @@ inline AssetLibrary* g_activeAssetLibrary = &g_assetLibrary; /****************************************************************************************/ +/* + * Set the active asset library + * + * \param p_assetLibrary The asset library to set g_activeAssetLibrary to. + */ +void SetActiveAssetLibrary(AssetLibrary* p_assetLibrary); + +/****************************************************************************************/ + class ScopedActiveAssetLibrary { public: ScopedActiveAssetLibrary(AssetLibrary* p_assetLibrary): m_previous(g_activeAssetLibrary) { - g_activeAssetLibrary = p_assetLibrary; + SetActiveAssetLibrary(p_assetLibrary); } ScopedActiveAssetLibrary(const ScopedActiveAssetLibrary& p_scopedActiveAssetLibrary) = default; @@ -177,7 +186,7 @@ class ScopedActiveAssetLibrary ~ScopedActiveAssetLibrary() { - g_activeAssetLibrary = m_previous; + SetActiveAssetLibrary(m_previous); } ScopedActiveAssetLibrary& operator=(const ScopedActiveAssetLibrary& p_scopedActiveAssetLibrary) = default; diff --git a/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp b/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp index 3bb39ce..a38ada2 100644 --- a/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp +++ b/Bigfoot/Sources/Utils/Include/Utils/Containers/SlotMap.hpp @@ -692,7 +692,7 @@ class StableSlotMap [[nodiscard]] T* Get(const SlotMapKey p_key) { - return const_cast(std::as_const(*this).Get(p_key)); + return Has(p_key) ? GetPageAt(GetPageIndex(p_key))->Get(GetDataOffset(p_key)) : nullptr; } [[nodiscard]] diff --git a/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp b/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp index c29da3d..b90bea6 100644 --- a/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp +++ b/Bigfoot/Tests/Engine/Asset/AssetContainer.cpp @@ -43,6 +43,46 @@ TEST_F(ContainerHandleFixture, GetAsset_ShouldReturnNullptrIfTheAssetHasBeenRemo /****************************************************************************************/ +TEST_F(ContainerHandleFixture, IncrementHardRefCount_ShouldIncrementTheHardRefCount) +{ + const std::uint32_t original = m_handle->GetHardRefCount(); + m_handle->IncrementHardRefCount(); + EXPECT_EQ(original + 1, m_handle->GetHardRefCount()); +} + +/****************************************************************************************/ + +TEST_F(ContainerHandleFixture, IncrementSoftRefCount_ShouldIncrementTheSoftRefCount) +{ + const std::uint32_t original = m_handle->GetSoftRefCount(); + m_handle->IncrementSoftRefCount(); + EXPECT_EQ(original + 1, m_handle->GetSoftRefCount()); +} + +/****************************************************************************************/ + +TEST_F(ContainerHandleFixture, DecrementHardRefCount_ShouldDecrementTheHardRefCount) +{ + m_handle->IncrementHardRefCount(); + + const std::uint32_t original = m_handle->GetHardRefCount(); + m_handle->DecrementHardRefCount(); + EXPECT_EQ(original - 1, m_handle->GetHardRefCount()); +} + +/****************************************************************************************/ + +TEST_F(ContainerHandleFixture, DecrementSoftRefCount_ShouldDecrementTheSoftRefCount) +{ + m_handle->IncrementSoftRefCount(); + + const std::uint32_t original = m_handle->GetSoftRefCount(); + m_handle->DecrementSoftRefCount(); + EXPECT_EQ(original - 1, m_handle->GetSoftRefCount()); +} + +/****************************************************************************************/ + class AssetContainerFixture: public ::testing::Test { protected: @@ -114,7 +154,7 @@ TEST_F(AssetContainerFixture, Get_ShouldReturnTheAssetWithTheSlotKey) const SlotMapKey slotKey = m_containerA.Insert(asset); EXPECT_EQ(m_containerA.Get(slotKey)->GetHeader().uuid, asset.GetHeader().uuid); - EXPECT_EQ(const_cast&>(m_containerA).Get(slotKey)->GetHeader().uuid, + EXPECT_EQ(std::as_const(m_containerA).Get(slotKey)->GetHeader().uuid, asset.GetHeader().uuid); } @@ -123,7 +163,7 @@ TEST_F(AssetContainerFixture, Get_ShouldReturnTheAssetWithTheSlotKey) TEST_F(AssetContainerFixture, Get_ShouldReturnNullptrIfSlotKeyNotInContainer) { EXPECT_EQ(m_containerA.Get(SlotMapKey<> {1, 0}), nullptr); - EXPECT_EQ(const_cast&>(m_containerA).Get(SlotMapKey<> {1, 0}), nullptr); + EXPECT_EQ(std::as_const(m_containerA).Get(SlotMapKey<> {1, 0}), nullptr); } /****************************************************************************************/ diff --git a/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp b/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp index 068e76b..faeefeb 100644 --- a/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp +++ b/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp @@ -58,12 +58,12 @@ TEST_F(AssetLibraryFixture, GetAssetContainer_ShouldReturnTheAssetContainerIfAva 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); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(), &m_containerA); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(GetAssetTypeID()), &m_containerA); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(), &m_containerB); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(GetAssetTypeID()), &m_containerB); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(), &m_containerC); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(GetAssetTypeID()), &m_containerC); } /****************************************************************************************/ @@ -75,8 +75,8 @@ TEST_F(AssetLibraryFixture, GetAssetContainer_ShouldReturnNullptrIfTheContainerD 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); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(), nullptr); + EXPECT_EQ(std::as_const(m_library).GetAssetContainer(GetAssetTypeID()), nullptr); } /****************************************************************************************/ @@ -99,9 +99,9 @@ TEST_F(AssetLibraryFixture, FindAssetContainerContainerForUUID_ShouldReturnTheAs 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); + EXPECT_EQ(std::as_const(m_library).FindAssetContainerContainerForUUID(uuidA), &m_containerA); + EXPECT_EQ(std::as_const(m_library).FindAssetContainerContainerForUUID(uuidB), &m_containerB); + EXPECT_EQ(std::as_const(m_library).FindAssetContainerContainerForUUID(uuidC), &m_containerC); } /****************************************************************************************/ @@ -118,7 +118,16 @@ TEST_F(AssetLibraryFixture, FindAssetContainerContainerForUUID_ShouldReturnNullp EXPECT_EQ(m_library.FindAssetContainerContainerForUUID(UUID {}), nullptr); - EXPECT_EQ(const_cast(m_library).FindAssetContainerContainerForUUID(UUID {}), nullptr); + EXPECT_EQ(std::as_const(m_library).FindAssetContainerContainerForUUID(UUID {}), nullptr); +} + +/****************************************************************************************/ + +TEST_F(AssetLibraryFixture, SetActiveAssetLibrary_ShouldSetTheActiveAssetLibraryToTheGivenLibrary) +{ + SetActiveAssetLibrary(&m_library); + EXPECT_EQ(g_activeAssetLibrary, &m_library); + SetActiveAssetLibrary(&g_assetLibrary); } /****************************************************************************************/ diff --git a/Bigfoot/Tests/Engine/Asset/Reference.cpp b/Bigfoot/Tests/Engine/Asset/Reference.cpp index e69de29..e8d1c4c 100644 --- a/Bigfoot/Tests/Engine/Asset/Reference.cpp +++ b/Bigfoot/Tests/Engine/Asset/Reference.cpp @@ -0,0 +1,125 @@ +/********************************************************************* + * \file AssetLibrary.cpp + * + * \author Romain BOULLARD + * \date July 2026 + *********************************************************************/ +#include + +#include +#include +#include +#include + +namespace Bigfoot +{ +class HardReferenceFixture: public ::testing::Test +{ + protected: + void SetUp() override + { + SetActiveAssetLibrary(&m_library); + + std::ignore = m_library.Insert(&m_containerA); + } + + void TearDown() override + { + SetActiveAssetLibrary(&g_assetLibrary); + } + + AssetContainer m_containerA; + + AssetLibrary m_library; +}; + +/****************************************************************************************/ + +TEST_F(HardReferenceFixture, GetUUID_ShouldReturnTheUUID) +{ + UUID uuid; + HardReference hardRef {uuid}; + + EXPECT_EQ(hardRef.GetUUID(), uuid); +} + +/****************************************************************************************/ + +TEST_F(HardReferenceFixture, Get_ShouldReturnTheAsset) +{ + const auto key = m_containerA.Insert(); + const UUID uuid = m_containerA.Get(key)->GetHeader().uuid; + + HardReference hardRef {uuid}; + + EXPECT_EQ(hardRef.Get(), m_containerA.Get(key)); + EXPECT_EQ(std::as_const(hardRef).Get(), std::as_const(m_containerA).Get(key)); +} + +/****************************************************************************************/ + +TEST_F(HardReferenceFixture, Get_ShouldReturnNullptrIfTheAssetDoesNotExist) +{ + UUID uuid; + HardReference hardRef {uuid}; + + EXPECT_EQ(hardRef.Get(), nullptr); + EXPECT_EQ(std::as_const(hardRef).Get(), nullptr); +} + +/****************************************************************************************/ + +class SoftReferenceFixture: public ::testing::Test +{ + protected: + void SetUp() override + { + SetActiveAssetLibrary(&m_library); + + std::ignore = m_library.Insert(&m_containerA); + } + + void TearDown() override + { + SetActiveAssetLibrary(&g_assetLibrary); + } + + AssetContainer m_containerA; + + AssetLibrary m_library; +}; + +/****************************************************************************************/ + +TEST_F(SoftReferenceFixture, GetUUID_ShouldReturnTheUUID) +{ + UUID uuid; + SoftReference hardRef {uuid}; + + EXPECT_EQ(hardRef.GetUUID(), uuid); +} + +/****************************************************************************************/ + +TEST_F(SoftReferenceFixture, Get_ShouldReturnTheAsset) +{ + const auto key = m_containerA.Insert(); + const UUID uuid = m_containerA.Get(key)->GetHeader().uuid; + + SoftReference hardRef {uuid}; + + EXPECT_EQ(hardRef.Get(), m_containerA.Get(key)); + EXPECT_EQ(std::as_const(hardRef).Get(), std::as_const(m_containerA).Get(key)); +} + +/****************************************************************************************/ + +TEST_F(SoftReferenceFixture, Get_ShouldReturnNullptrIfTheAssetDoesNotExist) +{ + UUID uuid; + SoftReference hardRef {uuid}; + + EXPECT_EQ(hardRef.Get(), nullptr); + EXPECT_EQ(std::as_const(hardRef).Get(), nullptr); +} +} // namespace Bigfoot diff --git a/Bigfoot/Tests/Utils/Containers/SlotMap.cpp b/Bigfoot/Tests/Utils/Containers/SlotMap.cpp index b0b81be..b767e80 100644 --- a/Bigfoot/Tests/Utils/Containers/SlotMap.cpp +++ b/Bigfoot/Tests/Utils/Containers/SlotMap.cpp @@ -274,7 +274,7 @@ TYPED_TEST(SlotMapFixture, Get_ShouldReturnTheValue) const SlotMapKey key = this->m_slotMap.Insert(42); EXPECT_EQ(*this->m_slotMap.Get(key), 42); - EXPECT_EQ(*const_cast(this->m_slotMap).Get(key), 42); + EXPECT_EQ(*std::as_const(this->m_slotMap).Get(key), 42); } /****************************************************************************************/ @@ -285,7 +285,7 @@ TYPED_TEST(SlotMapFixture, Get_ShouldNullptrIfTheValueIsNotInMap) const SlotMapKey key {1, 0}; EXPECT_EQ(this->m_slotMap.Get(key), nullptr); - EXPECT_EQ(const_cast(this->m_slotMap).Get(key), nullptr); + EXPECT_EQ(std::as_const(this->m_slotMap).Get(key), nullptr); } /****************************************************************************************/ @@ -299,7 +299,7 @@ TYPED_TEST(SlotMapFixture, Get_ShouldReturnNullptrForStaleKey) std::ignore = this->m_slotMap.Insert(69); EXPECT_EQ(this->m_slotMap.Get(staleKey), nullptr); - EXPECT_EQ(const_cast(this->m_slotMap).Get(staleKey), nullptr); + EXPECT_EQ(std::as_const(this->m_slotMap).Get(staleKey), nullptr); } /****************************************************************************************/