OpenBarnyard
 
Loading...
Searching...
No Matches
TSingleton.h
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3
5
6template <typename T>
8{
9public:
10 TSingleton( const TSingleton& ) = delete;
11 TSingleton( const TSingleton&& ) = delete;
12 TSingleton& operator=( const TSingleton& ) = delete;
13 TSingleton& operator=( const TSingleton&& ) = delete;
14
15 // Creates Singleton instance and returns it
16 template <typename... Args>
17 TFORCEINLINE static T* CreateSingleton( Args&&... args )
18 {
19 return new T( std::forward<Args>( args )... );
20 }
21
22 // Creates Singleton instance for a derived class and returns it
23 template <typename D, typename... Args>
24 TFORCEINLINE static D* CreateSingleton( Args&&... args )
25 {
26 TSTATICASSERT( std::is_base_of_v<T, D> == TTRUE );
27 return new D( std::forward<Args>( args )... );
28 }
29
31 {
32 delete GetSingletonSafe();
34 }
35
36 // Returns pointer and asserts if it's not allocated
38 {
39 TASSERT( ms_pSingleton != TNULL, "Singleton instance is not created" );
40 return ms_pSingleton;
41 }
42
44 {
45 return ms_pSingleton != TNULL;
46 }
47
48 // Returns pointer even if it's not allocated
50 {
51 return ms_pSingleton;
52 }
53
54#if defined( TOSHI_MODLOADER ) || defined( TOSHI_MODLOADER_CLIENT )
55 TFORCEINLINE static T* SetSingletonExplicit( T* a_pPtr )
56 {
57 ms_pSingleton = a_pPtr;
58 return ms_pSingleton;
59 }
60#endif
61
62protected:
64 {
65 TASSERT( ms_pSingleton == TNULL, "Singleton instance is already created" );
66 ms_pSingleton = static_cast<T*>( this );
67 }
68
70
71 static T* ms_pSingleton;
72};
73
74template <typename T>
76
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TFORCEINLINE
Definition Defines.h:74
#define TSTATICASSERT(...)
Definition Defines.h:67
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
T * TSingleton< T >::ms_pSingleton
Definition TSingleton.h:75
#define TNULL
Definition Typedefs.h:23
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TSingleton(const TSingleton &)=delete
TSingleton & operator=(const TSingleton &)=delete
static T * ms_pSingleton
Definition TSingleton.h:71
static TFORCEINLINE T * CreateSingleton(Args &&... args)
Definition TSingleton.h:17
static TFORCEINLINE void DestroySingleton()
Definition TSingleton.h:30
TSingleton(const TSingleton &&)=delete
static TFORCEINLINE D * CreateSingleton(Args &&... args)
Definition TSingleton.h:24
TSingleton & operator=(const TSingleton &&)=delete
static TFORCEINLINE TBOOL IsSingletonCreated()
Definition TSingleton.h:43
TFORCEINLINE TSingleton()
Definition TSingleton.h:63
static TFORCEINLINE T * GetSingleton()
Definition TSingleton.h:49
static TFORCEINLINE T * GetSingletonSafe()
Definition TSingleton.h:37
TFORCEINLINE ~TSingleton()
Definition TSingleton.h:69