3#include <Foundation/Basics.h>
4#include <Foundation/Containers/Map.h>
5#include <Foundation/Strings/String.h>
25 void* m_pInstance =
nullptr;
31 template <
typename Interface>
34 return static_cast<Interface*
>(s_Singletons.GetValueOrDefault(GetHash<Interface>(), {
"",
nullptr}).m_pInstance);
38 template <
typename Interface>
41 auto value = GetSingletonInstance<Interface>();
42 PL_ASSERT_ALWAYS(value,
"No instance of singleton type \"{0}\" has been registered!",
typeid(Interface).name());
50 template <
typename Interface>
51 inline static void Register(Interface* pSingletonInstance)
53 PL_ASSERT_DEV(pSingletonInstance !=
nullptr,
"Invalid singleton instance pointer");
55 s_Singletons[GetHash<Interface>()].m_pInstance ==
nullptr,
"Singleton for type '{0}' has already been registered",
typeid(Interface).name());
57 s_Singletons[GetHash<Interface>()] = {
typeid(Interface).name(), pSingletonInstance};
61 template <
typename Interface>
65 s_Singletons[GetHash<Interface>()].m_pInstance !=
nullptr,
"Singleton for type '{0}' is currently not registered",
typeid(Interface).name());
67 s_Singletons.Remove(GetHash<Interface>());
74 template <
typename Interface>
75 inline static size_t GetHash()
77 static const size_t hash =
typeid(Interface).hash_code();
99#define PL_DECLARE_SINGLETON(self) \
101 PL_ALWAYS_INLINE static self* GetSingleton() \
103 return s_pSingleton; \
107 PL_DISALLOW_COPY_AND_ASSIGN(self); \
108 void RegisterSingleton() \
110 s_pSingleton = this; \
111 plSingletonRegistry::Register<self>(this); \
113 static void UnregisterSingleton() \
117 plSingletonRegistry::Unregister<self>(); \
118 s_pSingleton = nullptr; \
121 friend class plSingletonRegistrar<self>; \
122 plSingletonRegistrar<self> m_SingletonRegistrar; \
123 static self* s_pSingleton
139#define PL_DECLARE_SINGLETON_OF_INTERFACE(self, interface) \
141 PL_ALWAYS_INLINE static self* GetSingleton() \
143 return s_pSingleton; \
147 PL_DISALLOW_COPY_AND_ASSIGN(self); \
148 void RegisterSingleton() \
150 s_pSingleton = this; \
151 plSingletonRegistry::Register<self>(this); \
152 plSingletonRegistry::Register<interface>(this); \
154 static void UnregisterSingleton() \
158 plSingletonRegistry::Unregister<interface>(); \
159 plSingletonRegistry::Unregister<self>(); \
160 s_pSingleton = nullptr; \
163 friend class plSingletonRegistrar<self>; \
164 plSingletonRegistrar<self> m_SingletonRegistrar; \
165 static self* s_pSingleton
169#define PL_IMPLEMENT_SINGLETON(self) self* self::s_pSingleton = nullptr
183 pType->RegisterSingleton();
188 TYPE::UnregisterSingleton();
[internal] Helper class to implement plSingletonRegistry and PL_DECLARE_SINGLETON
Definition Singleton.h:179
plSingletonRegistry knows about all singleton instances of classes that use PL_DECLARE_SINGLETON.
Definition Singleton.h:20
static Interface * GetRequiredSingletonInstance()
Retrieves a singleton instance by type name. Asserts if no singleton instance is available.
Definition Singleton.h:39
static void Unregister()
Unregisters a singleton instance. This is automatically called by plSingletonRegistrar.
Definition Singleton.h:62
static void Register(Interface *pSingletonInstance)
Registers a singleton instance under a given type name. This is automatically called by plSingletonRe...
Definition Singleton.h:51
static Interface * GetSingletonInstance()
Retrieves a singleton instance by type name. Returns nullptr if no singleton instance is available.
Definition Singleton.h:32
Definition Singleton.h:23