Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Singleton.h
Go to the documentation of this file.
1 #ifndef Protium_Singleton_h_
2 #define Protium_Singleton_h_
3 
8 
9 namespace Protium{
10 
13  namespace Singleton{
14 
22  template< class Host,
23  template <class> class CreationPolicy = CreateNew,
24  template <class> class DeletionPolicy = DeleteNever,
25  template <class, class> class ThreadingPolicy = Protium::Threads::InstanceLocked,
26  class MutexPolicy = Protium::Threads::Mutex>
27  class Singleton{
28  public:
30  typedef Host ObjectType;
32  static Host& Instance();
33 
34  private:
36  static void MakeInstance();
38  static void DestroySingleton();
39 
41  Singleton();
43  Singleton(const Singleton&) {}
46 
48  typedef typename ThreadingPolicy<ObjectType*,MutexPolicy>::VolatileType PtrInstanceType;
52  static bool fDestroyed;
53  };
54 
55 
56 
57 
58 
59  template< class Host,
60  template <class> class CreationPolicy,
61  template <class> class DeletionPolicy,
62  template <class, class> class ThreadingPolicy,
63  class MutexPolicy >
66 
67  template< class Host,
68  template <class> class CreationPolicy,
69  template <class> class DeletionPolicy,
70  template <class, class> class ThreadingPolicy,
71  class MutexPolicy >
73 
75  // Singleton::Instance
77 
78  template< class Host,
79  template <class> class CreationPolicy,
80  template <class> class DeletionPolicy,
81  template <class, class> class ThreadingPolicy,
82  class MutexPolicy >
84  if (!fInstance)
85  MakeInstance();
86  return *fInstance;
87  }
88 
90  // Singleton::MakeInstance (helper for Instance)
92 
93  template< class Host,
94  template <class> class CreationPolicy,
95  template <class> class DeletionPolicy,
96  template <class, class> class ThreadingPolicy,
97  class MutexPolicy >
99 
100  typename ThreadingPolicy<Singleton,MutexPolicy>::Lock lock;
101 
102  (void)lock;
103 
104  if (!fInstance){
105  if (fDestroyed){
106  fDestroyed = false;
107  DeletionPolicy<Host>::OnDeadReference();
108  }
109  fInstance = CreationPolicy<Host>::Create();
110  DeletionPolicy<Host>::ScheduleDestruction(fInstance, &DestroySingleton);
111  }
112  }
113 
114  template< class Host,
115  template <class> class CreationPolicy,
116  template <class> class DeletionPolicy,
117  template <class, class> class ThreadingPolicy,
118  class MutexPolicy >
120  assert(!fDestroyed);
121  CreationPolicy<Host>::Destroy(fInstance);
122  fInstance = NULL;
123  fDestroyed = true;
124  }
125  }
126 }
127 
128 
129 #endif //File Guardian
static Host & Instance()
Use to get instance.
Definition: Singleton.h:83
static PtrInstanceType fInstance
Pointer to the interior data.
Definition: Singleton.h:50
Use to specify instance-locked items.
static void MakeInstance()
Helper Function to make instance (Uses CreationPolicy)
Definition: Singleton.h:98
Singleton(const Singleton &)
Declared Private so can not be used.
Definition: Singleton.h:43
Wrapper class for pthreads-style mutexes.
Definition: Mutex.h:22
static void DestroySingleton()
Helper Function to destroy instance (Uses Destruction policy)
Definition: Singleton.h:119
Singleton & operator=(const Singleton &)
Declared Private so can not be used.
Definition: Singleton.h:45
Defines mutexes and policies to be used by threaded objects.
Singleton()
Declared Private so can not be used.
static bool fDestroyed
Flag for whether or not the instance has been killed (yet)
Definition: Singleton.h:52
ThreadingPolicy< ObjectType *, MutexPolicy >::VolatileType PtrInstanceType
Declaration of data interior to class.
Definition: Singleton.h:48
Host ObjectType
Declaration of the host interior to the class.
Definition: Singleton.h:30