00001 #ifndef __SINGLETON_H__ 00002 #define __SINGLETON_H__ 00003 00004 #ifndef _DEBUG 00005 #define BUGFIX 1 00006 #endif 00007 00008 #include <typeinfo> 00009 00010 template <typename T> 00011 class Singleton 00012 { 00013 public: 00014 static T* Instance(void); 00015 00016 protected: 00017 #if BUGFIX 00018 static T* instance; 00019 #endif 00020 00021 Singleton(void) 00022 { 00023 } 00024 00025 virtual ~Singleton(void) 00026 { 00027 #if BUGFIX 00028 if (instance != NULL) 00029 { 00030 delete instance; 00031 instance = NULL; 00032 } 00033 if (instance) 00034 { 00035 instance->~T(); 00036 instance=NULL; 00037 } 00038 #endif 00039 } 00040 00041 private: 00042 Singleton(const Singleton &); 00043 Singleton& operator= (const Singleton&); 00044 }; 00045 00046 template <class T> 00047 T* Singleton<T>::Instance(void) 00048 { 00049 #if BUGFIX 00050 if (instance==NULL) 00051 { 00052 instance=new T; 00053 } 00054 return instance; 00055 #else 00056 static T instance; 00057 return &instance; 00058 #endif 00059 } 00060 00061 #if BUGFIX 00062 template <class T> 00063 T* Singleton<T>::instance=NULL; 00064 #endif 00065 00066 #endif