Singleton.h
Go to the documentation of this file.
1 #ifndef _utl_Singleton_h_
2 #define _utl_Singleton_h_
3 
4 
5 namespace utl {
6 
7 #ifndef OFFLINE_USE_GAMMA_SINGLETON
8 
35  template<typename T>
36  class Singleton {
37  public:
38  static
39  T&
40  GetInstance()
41 # ifdef __MAKECINT__
42  ;
43 # else
44  {
45  static T instance;
46  return instance;
47  }
48 # endif
49 
50  protected:
51  // derived class can call ctor and dtor
52  Singleton() { }
53  ~Singleton() { }
54 
55  private:
56  // no one should do copies
57  Singleton(const Singleton&);
58  Singleton& operator=(const Singleton&);
59  };
60 
61 #else
62 
64  template<typename T>
65  class Singleton {
66  public:
67  static
68  T&
69  GetInstance()
70  {
71  if (!fgInstance)
72  fgInstance = new T;
73  return *fgInstance;
74  }
75 
76  protected:
77  // derived class can call ctor and dtor
78  Singleton() { }
79  ~Singleton() { }
80 
81  private:
82  // no one should do copies
83  Singleton(const Singleton&);
84  Singleton& operator=(const Singleton&);
85 
86  static T* fgInstance = 0;
87  };
88 
89 #endif
90 
91 
123  template<class T>
125  public:
126  static
127  T&
129  {
130  static T* const instance = new T;
131  return *instance;
132  }
133 
134  protected:
135  // derived class can call ctor and dtor
138 
139  private:
140  // no one should do copies
143  };
144 
145 }
146 
147 
148 #endif
LeakingSingleton & operator=(const LeakingSingleton &)
CRTP for leaking singleton.
Definition: Singleton.h:124
static T & GetInstance()
Definition: Singleton.h:128
Singleton & operator=(const Singleton &)
Curiously Recurring Template Pattern (CRTP) for Meyers singleton.
Definition: Singleton.h:36
static T &return instance
Definition: Singleton.h:44

, generated on Tue Sep 26 2023.