All checks were successful
Bigfoot / Build & Test Debug (Unity Build: OFF) (push) Successful in 28s
Bigfoot / Build & Test Debug (Unity Build: ON) (push) Successful in 27s
Bigfoot / Build & Test RelWithDebInfo (Unity Build: OFF) (push) Successful in 24s
Bigfoot / Build & Test RelWithDebInfo (Unity Build: ON) (push) Successful in 26s
Bigfoot / Build & Test Release (Unity Build: OFF) (push) Successful in 30s
Bigfoot / Build & Test Release (Unity Build: ON) (push) Successful in 27s
Bigfoot / Clang Format Checks (push) Successful in 10s
110 lines
2.3 KiB
C++
110 lines
2.3 KiB
C++
/*********************************************************************
|
|
* \file Singleton.hpp
|
|
*
|
|
* \author Romain BOULLARD
|
|
* \date October 2025
|
|
*********************************************************************/
|
|
#ifndef BIGFOOT_UTILS_SINGLETON_HPP
|
|
#define BIGFOOT_UTILS_SINGLETON_HPP
|
|
|
|
#include <EASTL/array.h>
|
|
#include <EASTL/bit.h>
|
|
#include <EASTL/optional.h>
|
|
#include <EASTL/type_traits.h>
|
|
#include <EASTL/utility.h>
|
|
|
|
namespace Bigfoot
|
|
{
|
|
template<class TYPE>
|
|
class Singleton
|
|
{
|
|
public:
|
|
Singleton() = delete;
|
|
|
|
Singleton(const Singleton& p_singleton) = delete;
|
|
Singleton(Singleton&& p_singleton) = delete;
|
|
|
|
~Singleton() = delete;
|
|
|
|
/**
|
|
* Get the instance.
|
|
*
|
|
* \return The instance
|
|
*/
|
|
static constexpr TYPE& Instance()
|
|
{
|
|
return ms_instance.value();
|
|
}
|
|
|
|
/**
|
|
* Is the instance initialized
|
|
*
|
|
* \return True if initialized, false otherwise
|
|
*/
|
|
static constexpr bool HasInstance()
|
|
{
|
|
return ms_instance.has_value();
|
|
}
|
|
|
|
class Lifetime
|
|
{
|
|
public:
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* \param p_args Arguments for the singleton
|
|
*/
|
|
template<typename... ARGS>
|
|
explicit Lifetime(ARGS&&... p_args)
|
|
{
|
|
Initialize(std::forward<ARGS>(p_args)...);
|
|
}
|
|
|
|
Lifetime(const Lifetime& p_lifetime) = delete;
|
|
Lifetime(Lifetime&& p_lifetime) = delete;
|
|
|
|
~Lifetime()
|
|
{
|
|
Finalize();
|
|
}
|
|
|
|
[[nodiscard]]
|
|
Lifetime& operator=(const Lifetime& p_lifetime) = delete;
|
|
[[nodiscard]]
|
|
Lifetime& operator=(Lifetime&& p_lifetime) = delete;
|
|
};
|
|
|
|
[[nodiscard]]
|
|
Singleton& operator=(const Singleton& p_singleton) = delete;
|
|
[[nodiscard]]
|
|
Singleton& operator=(Singleton&& p_singleton) = delete;
|
|
|
|
private:
|
|
/**
|
|
* Initialize the singleton.
|
|
*
|
|
* \param p_args Arguments for the singleton
|
|
*/
|
|
template<typename... ARGS>
|
|
static void Initialize(ARGS&&... p_args)
|
|
{
|
|
ms_instance.emplace(std::forward<ARGS>(p_args)...);
|
|
}
|
|
|
|
/**
|
|
* Finalize the singleton.
|
|
*
|
|
*/
|
|
static void Finalize()
|
|
{
|
|
ms_instance.reset();
|
|
}
|
|
|
|
/**
|
|
* The singleton.
|
|
*/
|
|
inline static eastl::optional<TYPE> ms_instance;
|
|
};
|
|
} // namespace Bigfoot
|
|
#endif
|