00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _psingleton_h_
00021 #define _psingleton_h_
00022
00023 #include <pclasses/plocktraits.h>
00024
00025 namespace P {
00026
00028
00032 template <typename _T, class _Mutex = VoidMutex>
00033 class Singleton {
00034 public:
00035 typedef typename LockTraits<_Mutex>::MutexType MutexType;
00036 typedef typename LockTraits<_Mutex>::WriteLock WriteLock;
00037
00038 static _T& instance()
00039 {
00040 WriteLock l(_mutex);
00041 if(!_instance)
00042 _instance = new _T();
00043
00044 return *_instance;
00045 }
00046
00047 protected:
00048 Singleton() {}
00049 ~Singleton() {}
00050
00051 private:
00052 static MutexType _mutex;
00053 static _T* _instance;
00054 };
00055
00056 template <class _T, class _Mutex>
00057 typename Singleton<_T,_Mutex>::MutexType Singleton<_T,_Mutex>::_mutex;
00058
00059 template <class _T, class _Mutex>
00060 _T* Singleton<_T,_Mutex>::_instance = 0;
00061
00062 }
00063
00064 #endif