1 #ifndef _singleton_h_ 2 #define _singleton_h_ 3 4 #include "AmThread.h" 5 6 template<class T> 7 class singleton 8 : public T 9 { singleton()10 singleton() : T() {} ~singleton()11 ~singleton() {} 12 13 public: instance()14 static singleton<T>* instance() 15 { 16 _inst_m.lock(); 17 if(NULL == _instance) { 18 _instance = new singleton<T>(); 19 } 20 _inst_m.unlock(); 21 22 return _instance; 23 } 24 haveInstance()25 static bool haveInstance() 26 { 27 bool res = false; 28 _inst_m.lock(); 29 res = _instance != NULL; 30 _inst_m.unlock(); 31 return res; 32 } 33 dispose()34 static void dispose() 35 { 36 _inst_m.lock(); 37 if(_instance != NULL){ 38 _instance->T::dispose(); 39 delete _instance; 40 _instance = NULL; 41 } 42 _inst_m.unlock(); 43 } 44 45 private: 46 static singleton<T>* _instance; 47 static AmMutex _inst_m; 48 }; 49 50 template<class T> 51 singleton<T>* singleton<T>::_instance = NULL; 52 53 template<class T> 54 AmMutex singleton<T>::_inst_m; 55 56 #endif 57