libassa 3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // Singleton.h 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 1997-2002,2005 Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 // Created: 02/22/99 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 } // end namespace ASSA 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