-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleton.h
55 lines (47 loc) · 979 Bytes
/
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
/** \file singleton.h
\brief Singleton object creational pattern template
*/
/** \note
Usage:
class CSth : public CSingleton<CSth>
{
friend CSingleton<CSth>;
private:
CSth();
~CSth();
...
}
*/
#ifndef SingletonH
#define SingletonH
/** \brief Singleton pattern template class
*/
template <typename T> class CSingleton
{
public:
/** \brief Get pointer to object
*/
static T* Instance()
{
if (instance == NULL) instance = new T;
return instance;
};
/** \brief Destroy object. This may seem unnecessary, but it makes runtime error checking happy.
*/
static void Destroy()
{
delete instance;
instance = NULL;
};
protected:
CSingleton() {};
virtual ~CSingleton() {};
private:
CSingleton(const CSingleton& source) {
};
CSingleton& operator=(const CSingleton&);
static T* instance;
};
// static initialization
template <typename T> T* CSingleton<T>::instance = NULL;
#endif