Use optional for Singleton
All checks were successful
Bigfoot / Build & Test Debug (Unity Build: OFF) (push) Successful in 26s
Bigfoot / Build & Test Debug (Unity Build: ON) (push) Successful in 25s
Bigfoot / Build & Test RelWithDebInfo (Unity Build: OFF) (push) Successful in 24s
Bigfoot / Build & Test RelWithDebInfo (Unity Build: ON) (push) Successful in 25s
Bigfoot / Build & Test Release (Unity Build: OFF) (push) Successful in 20s
Bigfoot / Build & Test Release (Unity Build: ON) (push) Successful in 19s
Bigfoot / Clang Format Checks (push) Successful in 10s
Bigfoot / Sonarqube (push) Successful in 1m4s

This commit is contained in:
2026-02-02 08:13:26 +01:00
parent a062a058cd
commit 5829530652

View File

@@ -9,6 +9,7 @@
#include <EASTL/array.h> #include <EASTL/array.h>
#include <EASTL/bit.h> #include <EASTL/bit.h>
#include <EASTL/optional.h>
#include <EASTL/type_traits.h> #include <EASTL/type_traits.h>
#include <EASTL/utility.h> #include <EASTL/utility.h>
@@ -32,7 +33,7 @@ class Singleton
*/ */
static constexpr TYPE& Instance() static constexpr TYPE& Instance()
{ {
return *eastl::bit_cast<TYPE*>(ms_instance.data()); return ms_instance.value();
} }
class Lifetime class Lifetime
@@ -78,9 +79,7 @@ class Singleton
template<typename... ARGS> template<typename... ARGS>
static void Initialize(ARGS&&... p_args) static void Initialize(ARGS&&... p_args)
{ {
new (ms_instance.data()) TYPE(eastl::forward<ARGS>(p_args)...); ms_instance.emplace(eastl::forward<ARGS>(p_args)...);
ms_initialized = true;
} }
/** /**
@@ -89,20 +88,13 @@ class Singleton
*/ */
static void Finalize() static void Finalize()
{ {
eastl::bit_cast<TYPE*>(ms_instance.data())->~TYPE(); ms_instance.reset();
ms_initialized = false;
} }
/** /**
* The singleton. * The singleton.
*/ */
alignas(alignof(TYPE)) inline static eastl::array<std::byte, sizeof(TYPE)> ms_instance; inline static eastl::optional<TYPE> ms_instance;
/**
* Is the singleton initialized?
*/
inline static bool ms_initialized = false;
}; };
} // namespace Bigfoot } // namespace Bigfoot
#endif #endif