-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTSingleton.h
More file actions
executable file
·158 lines (130 loc) · 4.67 KB
/
Copy pathTSingleton.h
File metadata and controls
executable file
·158 lines (130 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//==============================================================================
// Copyright (c) 2012-2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief A simple template based singleton class wrapper.
//==============================================================================
#ifndef _TSINGLETON_H_
#define _TSINGLETON_H_
// The main use for lazy allocation version here is to aid in debugging problems.
// Ctors for globally allocated things can be a pain to debug.
#if defined(USE_POINTER_SINGLETON)
//=============================================================================
/// Singleton base class.
///
/// Implemented using a pointer to the instance. Requires calling DeleteInstance
/// to clean up.
//=============================================================================
template< class C >
class TSingleton
{
private:
// Static current instance
static C* m_pInstance;
protected:
//=============================================================================
/// Hidden constructor
//=============================================================================
TSingleton()
{
}
//=============================================================================
/// Hidden destructor
//=============================================================================
virtual ~TSingleton()
{
DeleteInstance();
}
public:
//=============================================================================
// Current instance getter
//=============================================================================
static C* Instance()
{
if (m_pInstance == nullptr)
{
m_pInstance = new C;
}
return m_pInstance;
}
//=============================================================================
// Delete current instance
//=============================================================================
static void DeleteInstance()
{
if (m_pInstance != nullptr)
{
// ~TSingleton and DeleteInstance will loop
// unless we set m_pInstance to nullptr
// before calling delete.
C* copyOfPInstance = m_pInstance;
m_pInstance = nullptr;
delete copyOfPInstance;
}
}
};
// Initialize the static member CurrentInstance
template< class C >
C* TSingleton<C>::m_pInstance = nullptr;
#else
//=============================================================================
/// Singleton base class.
///
/// This singleton does not require deletion as it uses a static object.
//=============================================================================
template< class C >
class TSingleton
{
private:
/// The underlying instance of this singleton object
static C m_Instance;
/// A pointer to the singleton instance that gets set to nullptr when the object is destroyed.
static C* m_pInstance;
protected:
//=============================================================================
/// Hidden constructor
//=============================================================================
TSingleton()
{
m_pInstance = &m_Instance;
}
//=============================================================================
/// Hidden destructor
//=============================================================================
virtual ~TSingleton()
{
m_pInstance = 0;
}
public:
//=============================================================================
/// Accessor to the instance.
/// \return A pointer to the instance.
//=============================================================================
static C* Instance()
{
return m_pInstance;
}
//=============================================================================
/// Accessor to a reference of the instance.
/// \return A reference to the instance.
//=============================================================================
static C& Ref()
{
return m_Instance;
}
//=============================================================================
/// Supplied for compatability with pointer based version ( see: USE_POINTER_SINGLETON ).
//=============================================================================
static void DeleteInstance()
{
/// Do nothing
}
};
/// Initialize the instance.
template< class C >
C TSingleton<C>::m_Instance;
/// Initialize the pointer to the instance
template< class C >
C* TSingleton<C>::m_pInstance = &TSingleton<C>::m_Instance;
#endif
#endif // _TSINGLETON_H_