-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConstSingleton_T.h
50 lines (36 loc) · 1021 Bytes
/
ConstSingleton_T.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
/*
Derived from the code on
http://cboard.cprogramming.com/cplusplus-programming/117560-singleton-base-template-class-how-do-you-do.html
from "Sebastiani", 2001
*/
#ifndef COMSTSINGLETON_T_H
#define COMSTSINGLETON_T_H
template < typename T >
struct ConstSingleton_T
{
protected:
ConstSingleton_T( void )
{
if (!instance_) instance_ = (T*)this;
}
virtual ~ConstSingleton_T( void )
{
instance_ = 0;
}
public:
static inline const T& instance( void ) // lca - why is this needed?
{
return *instance_;
}
static inline bool exists( void )
{
return instance_ != 0;
}
static T * instance_;
private:
ConstSingleton_T operator=(const ConstSingleton_T& ) const; // do not implement -- compiler will complain if you try to assign to the singleton
ConstSingleton_T(const ConstSingleton_T& ); // do not implement -- compiler will complain if you try to copy the singleton
};
template < typename T >
T* ConstSingleton_T< T >::instance_ = 0;
#endif