-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.h
423 lines (343 loc) · 9.28 KB
/
Singleton.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#ifndef BaseLib_Singleton_h_IsIncluded
#define BaseLib_Singleton_h_IsIncluded
#include"BaseLib/CommonDefines.h"
#include"BaseLib/MutexBare.h"
#include"BaseLib/MutexLocker.h"
namespace BaseLib
{
/**
@description Implementation of Singleton software pattern.
class Foo : public Singleton<Foo> {};
Foo& foo1 = Foo::GetRef();
Foo* foo2 = Foo::GetPtr();
Foo::Destroy();
@url http://stackoverflow.com/questions/2346091/c-templated-class-implementation-of-the-multiton-pattern
TODO: For thread-safe implementation use Double-checked locking pattern
http://en.wikipedia.org/wiki/Double-checked_locking
*/
template <typename T>
class Singleton
{
public:
Singleton()
: instance_(NULL)
, isInitialized_(false)
, mutex_(MutexPolicy::No())
{ }
virtual ~Singleton()
{
Destroy();
}
void Destroy()
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(isInitialized_)
{
delete instance_;
instance_ = NULL;
isInitialized_ = false;
}
}
T* GetPtr()
{
if(isInitialized_ == false)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(isInitialized_ == false)
{
instance_ = new T;
isInitialized_ = true;
}
}
return instance_;
}
T& GetRef()
{
if(isInitialized_ == false)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(isInitialized_ == false)
{
instance_ = new T;
isInitialized_ = true;
}
}
return *instance_;;
}
bool IsNull()
{
if(isInitialized_ == true)
return isInitialized_;
MutexTypeLocker<MutexBare> mutex(&mutex_);
return isInitialized_;
}
private:
Singleton(const Singleton&) {}
Singleton& operator = (const Singleton&) { return *this; }
private:
T* instance_;
bool isInitialized_;
MutexBare mutex_;
};
template <typename T>
class StaticSingleton
{
public:
StaticSingleton()
{ }
virtual ~StaticSingleton()
{
destroy();
}
static T* InstancePtr()
{
if(!isInitialized_)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(!isInitialized_)
{
instance_ = new T;
isInitialized_ = true;
}
}
return instance_;
}
static T& Instance()
{
if(!isInitialized_)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(!isInitialized_)
{
instance_ = new T;
isInitialized_ = true;
}
}
return *instance_;
}
static bool IsInitialized()
{
if(isInitialized_)
return isInitialized_;
MutexTypeLocker<MutexBare> mutex(&mutex_);
return isInitialized_;
}
private:
static void destroy()
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(isInitialized_)
{
delete instance_;
instance_ = nullptr;
isInitialized_ = false;
}
}
private:
StaticSingleton(const StaticSingleton<T>&) {}
StaticSingleton& operator = (const StaticSingleton<T>&) { return *this; }
private:
static T* instance_;
static bool isInitialized_;
static MutexBare mutex_;
};
template <typename T>
T* StaticSingleton<T>::instance_ = nullptr;
template <typename T>
bool StaticSingleton<T>::isInitialized_ = false;
template <typename T>
MutexBare StaticSingleton<T>::mutex_ = MutexBare();
template <typename T>
class StaticSingletonPtr
{
public:
StaticSingletonPtr()
{ }
virtual ~StaticSingletonPtr()
{
//destroy();
}
static std::shared_ptr<T> InstancePtr()
{
if(!isInitialized_)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(!isInitialized_)
{
instance_ = std::shared_ptr<T>(new T);
isInitialized_ = true;
}
}
return instance_;
}
template <typename U>
static std::shared_ptr<U> InstancePtr()
{
if(!isInitialized_)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(!isInitialized_)
{
instance_ = std::shared_ptr<T>(new T);
isInitialized_ = true;
}
}
return std::dynamic_pointer_cast<U>(instance_);
}
static T& Instance()
{
if(!isInitialized_)
{
MutexTypeLocker<MutexBare> mutex(&mutex_);
if(!isInitialized_)
{
instance_ = std::shared_ptr<T>(new T);
isInitialized_ = true;
}
}
return *instance_;
}
static bool IsInitialized()
{
if(isInitialized_)
return isInitialized_;
MutexTypeLocker<MutexBare> mutex(&mutex_);
return isInitialized_;
}
//private:
// static void destroy()
// {
// Locker mutex(&mutex_);
// if(isInitialized_)
// {
// instance_ = nullptr;
// isInitialized_ = false;
// }
// }
private:
StaticSingletonPtr(const StaticSingletonPtr<T>&) {}
StaticSingletonPtr& operator = (const StaticSingletonPtr<T>&) { return *this; }
private:
static std::shared_ptr<T> instance_;
static bool isInitialized_;
static MutexBare mutex_;
};
template <typename T>
std::shared_ptr<T> StaticSingletonPtr<T>::instance_ = nullptr;
template <typename T>
bool StaticSingletonPtr<T>::isInitialized_ = false;
template <typename T>
MutexBare StaticSingletonPtr<T>::mutex_ = MutexBare();
/*------------------------------------------------------------------------------
SingletonConstruct -
Template class for constructing a singleton in an efficient and thread safe
manner. This object must be a static member of the class that you want to be
a singleton - this ensures m_p will be zero'd out during global construction
(which is single threaded)
NOTE: Do not use your singleton in a global object constructor or destructor.
------------------------------------------------------------------------------*/
template <typename T>
class SingletonConstruct //: public no_copy
{
public:
SingletonConstruct() : instance_(0)
{
}
~SingletonConstruct()
{
Unload();
}
// NOTE: caller must guarantee that no references are being held
void Unload()
{
delete instance_;
}
T& Instance()
{
// atomic read with acquire member semantics
// TODO: InterLockedReadAcquire
// TODO: Use boost atomic read / write
volatile T *p = &instance_;
if(!p)
{
MutexTypeLocker<MutexBare> lock(&mutex_);
p = instance_;
if(!p)
{
p = new T;
// atomic write with release membar semantics
// TODO: InterlockedWriteRelease(
&instance_ = p;
}
}
return *p;
}
private:
SingletonConstruct(const SingletonConstruct&) {}
SingletonConstruct& operator = (const SingletonConstruct&) { return *this; }
private:
T *instance_;
MutexBare mutex_;
};
/*
// Example usage SingletonConstruct
class foo : public no_copy
{
static SingletonConstruct<foo> m_foo_sc;
friend class SingletonConstruct<foo>;
foo() {}
~foo() {}
public:
static foo& Instance() {return m_foo_sc.Instance();}
void display() {std::cout << "I'm the foo instance!" << std::endl;}
};//foo
// m_foo_sc is global which ensures it's construction prior to threading
SingletonConstruct<foo> foo::m_foo_sc;
*/
/*------------------------------------------------------------------------------
tlsSingletonConstruct -
Demonstrates singleton construction in an efficient and thread safe manner
using TLS. It is assumed that TLS memory access is more efficient than
acquiring a lock. A Posix compliant implementation is possible using this
model.
------------------------------------------------------------------------------*/
/*template <typename T>
class tlsSingletonConstruct : public no_copy
{
public:
tlsSingletonConstruct() : m_p(0)
{
}
~tlsSingletonConstruct()
{
Unload();
}
// NOTE - caller must guarantee that no references are being held
void Unload()
{
delete m_p;
}
T& Instance()
{
// "__thread" represents TLS which is guaranteed to be initialized
// "early". An actual implementation may use compiler extensions to
// declare the static TLS here, or acquire TLS storage via API's within
// tlsSingletonConstruct's constructor.
static __thread bool g_created = false;
if (g_created)
return *m_p;
Locker lock(&mutex);
if (!m_p)
m_p = new T;
g_created = true;
return *m_p;
}
private:
tlsSingletonConstruct(const tlsSingletonConstruct&) {}
tlsSingletonConstruct& operator = (const tlsSingletonConstruct&) { return *this; }
private:
T *m_p;
Mutex mutex;
};*/
} // namespace BaseLib
#endif // BaseLib_Singleton_h_IsIncluded