StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StHyperSingleton.h
1 #ifndef __ST_HYPERSINGLETON_H
2 #define __ST_HYPERSINGLETON_H
3 
4 // taken from :
5 // http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-2/3/
6 //
7 // Meyers Singleton is an implementation that prevents memory leakage. The Gamma Singleton, on the other hand, doesn’t clean up after itself
8 // and this comes in handy with our destruction sequence problem. The best way to guarantee our singleton will remain alive to the very end
9 // is by simply not destroying it!
10 //
11 
12 // This is how a Gamma Singleton would instantiate its object.
13 template <class T> struct StHyperCreateGamma {
14  static T* Create() {
15  return new T;
16  }
17 };
18 
19 // This is how a Meyers Singleton would instantiate its object.
20 template <class T> struct StHyperCreateMeyers {
21  static T* Create() {
22  static T _instance;
23  return &_instance;
24  }
25 };
26 
27 // This Singleton class accepts different creation policies.
28 template <class T, template<class> class StHyperCreationPolicy=StHyperCreateMeyers>
30 {
31 public:
32  static T& Instance() {
33  if (!m_pInstance)
34  m_pInstance=StHyperCreationPolicy<T>::Create();
35  return *m_pInstance;
36  }
37 
38 private:
39  StHyperSingleton(); // ctor hidden
40  ~StHyperSingleton(); // dtor hidden
41  StHyperSingleton(StHyperSingleton const&); // copy ctor hidden
42  StHyperSingleton& operator=(StHyperSingleton const&); // assign op hidden
43 
44  static T* m_pInstance;
45 };
46 
47 template <class T, template<class> class C>
49 
50 #endif // __ST_HYPERSINGLETON_H
51 // eof
52