00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef _Singleton_h
00015 #define _Singleton_h
00016
00017 #include "Destroyer.h"
00018
00019 namespace ASSA {
00020
00041 template <class T>
00042 class Singleton
00043 {
00044 public:
00046
00047 static T* get_instance () {
00048 if (m_instance == 0) {
00049 m_instance = new T;
00050 m_destroyer.setGuard (m_instance);
00051 }
00052 return m_instance;
00053 }
00054
00055 protected:
00057 Singleton() {}
00058
00059 friend class Destroyer<T>;
00060
00062 virtual ~Singleton () {}
00063
00064 private:
00066 static T* m_instance;
00067
00069 static Destroyer<T> m_destroyer;
00070 };
00071
00072 }
00073
00074
00082 #define ASSA_DECL_SINGLETON(K) \
00083 template <> K* ASSA::Singleton<K>::m_instance = NULL; \
00084 template <class T> ASSA::Destroyer<T> ASSA::Singleton<T>::m_destroyer; \
00085 template ASSA::Destroyer<K> ASSA::Singleton<K>::m_destroyer;
00086
00087 #endif