ezEngine  Milestone 9
Singleton.h File Reference
#include <Foundation/Basics.h>
#include <Foundation/Containers/Map.h>
#include <Foundation/Strings/String.h>

Go to the source code of this file.

Classes

class  ezSingletonRegistry
 ezSingletonRegistry knows about all singleton instances of classes that use EZ_DECLARE_SINGLETON. More...
 
class  ezSingletonRegistrar< TYPE >
 [internal] Helper class to implement ezSingletonRegistry and EZ_DECLARE_SINGLETON More...
 

Macros

#define EZ_DECLARE_SINGLETON(self)
 Insert this into a class declaration to turn the class into a singleton. More...
 
#define EZ_DECLARE_SINGLETON_OF_INTERFACE(self, interface)
 Insert this into a class declaration to turn the class into a singleton. More...
 
#define EZ_IMPLEMENT_SINGLETON(self)   self* self::s_pSingleton = nullptr
 Put this into the cpp of a singleton class.
 

Macro Definition Documentation

◆ EZ_DECLARE_SINGLETON

#define EZ_DECLARE_SINGLETON (   self)
Value:
public: \
EZ_ALWAYS_INLINE static self* GetSingleton() { return s_pSingleton; } \
\
private: \
EZ_DISALLOW_COPY_AND_ASSIGN(self); \
void RegisterSingleton() \
{ \
s_pSingleton = this; \
} \
static void UnregisterSingleton() \
{ \
if (s_pSingleton) \
{ \
s_pSingleton = nullptr; \
} \
} \
friend class ezSingletonRegistrar<self>; \
ezSingletonRegistrar<self> m_SingletonRegistrar; \
static self* s_pSingleton
static void Register(void *pSingletonInstance, const char *szTypeName)
Registers a singleton instance under a given type name. This is automatically called by ezSingletonRe...
Definition: Singleton.cpp:17
static void Unregister(const char *szTypeName)
Unregisters a singleton instance. This is automatically called by ezSingletonRegistrar.
Definition: Singleton.cpp:25
[internal] Helper class to implement ezSingletonRegistry and EZ_DECLARE_SINGLETON ...
Definition: Singleton.h:135

Insert this into a class declaration to turn the class into a singleton.

You can access the singleton instance in two ways. By calling the static GetSingleton() function on the specific type. By querying the instance through ezSingletonRegistry giving the class type as a string. The latter allows to get the implementation of an interface that is only declared through a simple header but was not linked against.

Use EZ_DECLARE_SINGLETON for a typical singleton class. Use EZ_DECLARE_SINGLETON_OF_INTERFACE for a singleton class that implements a specific interface, which is itself not declared as a singleton and thus does not support to get to the interface implementation through GetSingleton(). This is necessary, if you want to decouple library link dependencies and thus not put any singleton code into the interface declaration, to keep it a pure virtual interface. You can then query that class pointer also through the name of the interface using ezSingletonRegistry.

◆ EZ_DECLARE_SINGLETON_OF_INTERFACE

#define EZ_DECLARE_SINGLETON_OF_INTERFACE (   self,
  interface 
)
Value:
public: \
EZ_ALWAYS_INLINE static self* GetSingleton() { return s_pSingleton; } \
\
private: \
void RegisterSingleton() \
{ \
s_pSingleton = this; \
ezSingletonRegistry::Register(this, #interface); \
} \
static void UnregisterSingleton() \
{ \
if (s_pSingleton) \
{ \
s_pSingleton = nullptr; \
} \
} \
friend class ezSingletonRegistrar<self>; \
ezSingletonRegistrar<self> m_SingletonRegistrar; \
static self* s_pSingleton
static void Register(void *pSingletonInstance, const char *szTypeName)
Registers a singleton instance under a given type name. This is automatically called by ezSingletonRe...
Definition: Singleton.cpp:17
static void Unregister(const char *szTypeName)
Unregisters a singleton instance. This is automatically called by ezSingletonRegistrar.
Definition: Singleton.cpp:25
[internal] Helper class to implement ezSingletonRegistry and EZ_DECLARE_SINGLETON ...
Definition: Singleton.h:135

Insert this into a class declaration to turn the class into a singleton.

You can access the singleton instance in two ways. By calling the static GetSingleton() function on the specific type. By querying the instance through ezSingletonRegistry giving the class type as a string. The latter allows to get the implementation of an interface that is only declared through a simple header but was not linked against.

Use EZ_DECLARE_SINGLETON for a typical singleton class. Use EZ_DECLARE_SINGLETON_OF_INTERFACE for a singleton class that implements a specific interface, which is itself not declared as a singleton and thus does not support to get to the interface implementation through GetSingleton(). This is necessary, if you want to decouple library link dependencies and thus not put any singleton code into the interface declaration, to keep it a pure virtual interface. You can then query that class pointer also through the name of the interface using ezSingletonRegistry.