base-logging
Singleton.hpp
Go to the documentation of this file.
1 #ifndef _BASE_SINGLETON_H_
2 #define _BASE_SINGLETON_H_
3 
4 namespace base {
5 
6 template<class Derived>
7 class Singleton
8 {
9 
10 private:
11  static Derived* msInstance;
12 
13 protected:
14  Singleton() {}
15 
16 public:
17  static Derived* getInstance()
18  {
19  static CGuard g;
20 
21  if(msInstance == 0)
22  {
23  msInstance = new Derived();
24  }
25 
26  return msInstance;
27  }
28 
29  virtual ~Singleton()
30  {
31  }
32 
33 
34  // Nested singleton helper class
35  class CGuard
36  {
37  public:
39  {
40  if(msInstance != 0)
41  {
42  delete msInstance;
43  msInstance = 0;
44  }
45  }
46 
47  };
48 
49  friend class CGuard;
50 
51 };
52 
53 template<typename Derived> Derived* Singleton<Derived>::msInstance = 0;
54 
55 } // end namespace base;
56 
57 
58 #endif // _BASE_SINGLETON_H_
Definition: Singleton.hpp:7
virtual ~Singleton()
Definition: Singleton.hpp:29
friend class CGuard
Definition: Singleton.hpp:49
~CGuard()
Definition: Singleton.hpp:38
Singleton()
Definition: Singleton.hpp:14
Definition: Singleton.hpp:35
static Derived * getInstance()
Definition: Singleton.hpp:17