diff --git a/Bigfoot/Benchmarks/Engine/Asset/Reference.cpp b/Bigfoot/Benchmarks/Engine/Asset/Reference.cpp index 6086e51..1c1f2fd 100644 --- a/Bigfoot/Benchmarks/Engine/Asset/Reference.cpp +++ b/Bigfoot/Benchmarks/Engine/Asset/Reference.cpp @@ -36,7 +36,7 @@ class ReferenceFixture: public benchmark::Fixture void TearDown(benchmark::State&) override { - SetActiveAssetLibrary(&g_assetLibrary); + SetActiveAssetLibrary(g_assetLibrary); } AssetLibrary m_library; diff --git a/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetHelper.hpp b/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetHelper.hpp index e32fb9b..9edb588 100644 --- a/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetHelper.hpp +++ b/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetHelper.hpp @@ -19,13 +19,14 @@ return AssetType::GetTypeID(); \ } \ AssetContainer g_containerFor##AssetName; \ - const bool g_insertedContainerFor##AssetName = []() \ + bool RegisterAssetContainerFor##AssetName() \ { \ - const bool inserted = g_assetLibrary.Insert(&g_containerFor##AssetName); \ + const bool inserted = g_assetLibrary->Insert(&g_containerFor##AssetName); \ CRITICAL_ASSERT(EngineAssertHandler, \ inserted, \ "AssetTypeID collision or duplicate registration for asset type '" #AssetName "'"); \ return inserted; \ - }(); \ + } \ + const bool g_insertedContainerFor##AssetName = RegisterAssetContainerFor##AssetName(); \ } #endif diff --git a/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetLibrary.hpp b/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetLibrary.hpp index 91e7078..be958c4 100644 --- a/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetLibrary.hpp +++ b/Bigfoot/Libs/Engine/Include/Engine/Asset/AssetLibrary.hpp @@ -10,6 +10,8 @@ #include #include +#include + #include namespace Bigfoot @@ -109,8 +111,44 @@ class AssetLibrary /****************************************************************************************/ // The global asset library for Bigfoot -inline AssetLibrary g_assetLibrary; -inline AssetLibrary* g_activeAssetLibrary = &g_assetLibrary; +inline LazyGlobal g_assetLibrary; + +class ActiveAssetLibrary +{ + public: + ActiveAssetLibrary& operator=(AssetLibrary* p_assetLibrary) + { + Get() = p_assetLibrary; + return *this; + } + + [[nodiscard]] + AssetLibrary* operator->() const + { + return Get(); + } + + [[nodiscard]] + operator AssetLibrary*() const + { + return Get(); + } + + private: + /** + * Get the ActiveAssetLibrary + * + * \return The ActiveAssetLibrary + */ + [[nodiscard]] + static AssetLibrary*& Get() + { + static AssetLibrary* activeLibrary = g_assetLibrary; + return activeLibrary; + } +}; + +inline ActiveAssetLibrary g_activeAssetLibrary; /****************************************************************************************/ diff --git a/Bigfoot/Tests/Engine/Asset/Asset.cpp b/Bigfoot/Tests/Engine/Asset/Asset.cpp index d1d6c82..213fa04 100644 --- a/Bigfoot/Tests/Engine/Asset/Asset.cpp +++ b/Bigfoot/Tests/Engine/Asset/Asset.cpp @@ -41,7 +41,7 @@ class AssetFixture: public ::testing::Test void TearDown() override { - SetActiveAssetLibrary(&g_assetLibrary); + SetActiveAssetLibrary(g_assetLibrary); } AssetLibrary m_library; diff --git a/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp b/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp index 53ba2a4..0e42a62 100644 --- a/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp +++ b/Bigfoot/Tests/Engine/Asset/AssetLibrary.cpp @@ -244,18 +244,18 @@ TEST_F(AssetLibraryFixture, SetActiveAssetLibrary_ShouldSetTheActiveAssetLibrary { SetActiveAssetLibrary(&m_library); EXPECT_EQ(g_activeAssetLibrary, &m_library); - SetActiveAssetLibrary(&g_assetLibrary); + SetActiveAssetLibrary(g_assetLibrary); } /****************************************************************************************/ TEST_F(AssetLibraryFixture, ScopedActiveAssetLibrary_ShouldChangeTheActiveAssetLibraryInAScopedWay) { - EXPECT_EQ(g_activeAssetLibrary, &g_assetLibrary); + EXPECT_EQ(g_activeAssetLibrary, g_assetLibrary); { ScopedActiveAssetLibrary activeAssetLibraryScoped {&m_library}; EXPECT_EQ(g_activeAssetLibrary, &m_library); } - EXPECT_EQ(g_activeAssetLibrary, &g_assetLibrary); + EXPECT_EQ(g_activeAssetLibrary, g_assetLibrary); } } // namespace Bigfoot diff --git a/Bigfoot/Tests/Engine/Asset/Reference.cpp b/Bigfoot/Tests/Engine/Asset/Reference.cpp index 2732bc0..00a675d 100644 --- a/Bigfoot/Tests/Engine/Asset/Reference.cpp +++ b/Bigfoot/Tests/Engine/Asset/Reference.cpp @@ -49,7 +49,7 @@ class ReferenceFixture: public ::testing::Test void TearDown() override { - SetActiveAssetLibrary(&g_assetLibrary); + SetActiveAssetLibrary(g_assetLibrary); } [[nodiscard]] diff --git a/Bigfoot/Tests/Engine/SerializationManager/SerializationManager.cpp b/Bigfoot/Tests/Engine/SerializationManager/SerializationManager.cpp index 2335f68..3ccf42a 100644 --- a/Bigfoot/Tests/Engine/SerializationManager/SerializationManager.cpp +++ b/Bigfoot/Tests/Engine/SerializationManager/SerializationManager.cpp @@ -111,7 +111,7 @@ class SerializationManagerFixture: public ::testing::Test void TearDown() override { SetDataAccessor(m_previousDataAccessor); - SetActiveAssetLibrary(&g_assetLibrary); + SetActiveAssetLibrary(g_assetLibrary); } /*