xmscore  1.0
Singleton.h
Go to the documentation of this file.
1 #pragma once
2 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 
9 // 3. Standard Library Headers
10 
11 // 4. External Library Headers
12 #include <boost/shared_ptr.hpp>
13 
14 // 5. Shared Headers
15 
16 // 6. Non-shared Headers
17 
18 //----- Namespace declaration --------------------------------------------------
19 
20 namespace xms
21 {
25 template <typename T>
26 class Singleton
27 {
28 public:
29  //----------------------------------------------------------------------------
36  //----------------------------------------------------------------------------
37  static T& Instance(bool a_delete = false, T* a_new = NULL)
38  {
39  static boost::shared_ptr<T> theSingleInstance;
40 
41  if (a_delete)
42  {
43  if (theSingleInstance)
44  {
45  theSingleInstance.reset();
46  }
47  }
48  else
49  {
50  if (NULL == theSingleInstance)
51  {
52  theSingleInstance.reset(a_new ? a_new : new T);
53  }
54  }
55  if (theSingleInstance)
56  {
57  return *theSingleInstance;
58  }
59  else
60  {
61  return *theSingleInstance.get();
62  }
63  } // Instance
64 
65  //----------------------------------------------------------------------------
67  //----------------------------------------------------------------------------
68  static void Delete() { Instance(true, boost::shared_ptr<T>()); }
69 }; // class Singleton
81 template <typename T>
83 {
84 public:
85  typedef boost::shared_ptr<T> Ptr;
86 
87  //----------------------------------------------------------------------------
94  static Ptr Instance(Ptr a_new = Ptr(), bool a_delete = false)
95  {
96  static boost::shared_ptr<T> theSingleInstance;
97 
98  if (a_delete)
99  {
100  if (theSingleInstance)
101  {
102  theSingleInstance.reset();
103  }
104  }
105  else
106  {
107  // assert((NULL == theSingleInstance) ^ (NULL == a_new)); // init iff not
108  // Above commented because need to return whether initialized or not
109  if (NULL == theSingleInstance) // init because not
110  {
111  theSingleInstance = a_new;
112  }
113  }
114  return theSingleInstance;
115  } // Instance
116 
117  //----------------------------------------------------------------------------
119  //----------------------------------------------------------------------------
120  static void Delete() { Instance(Ptr(), true); }
121 }; // class SharedSingleton
122 
123 } // namespace xms
Singleton which can share the same instance with other singletons.
Definition: Singleton.h:82
static void Delete()
Delete the singleton instance.
Definition: Singleton.h:68
static void Delete()
Delete the current instance.
Definition: Singleton.h:120
static Ptr Instance(Ptr a_new=Ptr(), bool a_delete=false)
Get a singleton instance.
Definition: Singleton.h:94
Base class for classes that follow the singleton pattern.
Definition: Singleton.h:26
static T & Instance(bool a_delete=false, T *a_new=NULL)
Get the instance of the singleton.
Definition: Singleton.h:37
boost::shared_ptr< T > Ptr
Instance pointer.
Definition: Singleton.h:85