MetriVis | Overview | Download | User Manual | Development |
Reference | Overview | Design Documentation | Reference Backend | Reference Frontend |
00001 /* 00002 * MetriVis - Metrics Visualization Application 00003 * 00004 * 00005 * License notice: 00006 * 00007 * This program is free software: you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation, either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 * 00020 */ 00021 00022 00023 #ifndef _METRIVIS_SINGLETON_H 00024 #define _METRIVIS_SINGLETON_H 00025 00026 00033 #include <boost/thread/mutex.hpp> 00034 00035 namespace metrivis { 00036 00045 template<class T> 00046 class Singleton { 00047 00048 public: 00049 00054 static T& Instance(); 00055 00061 static void Destroy(); 00062 00063 private: 00064 Singleton(); 00065 Singleton(const Singleton& singleton); 00066 Singleton& operator=(const Singleton& singleton); 00067 virtual ~Singleton(); 00068 00069 00070 private: 00071 static T* volatile singleton_instance_; 00072 00073 static boost::mutex instance_mutex_; 00074 }; 00075 00076 00077 template<class T> 00078 T* volatile Singleton<T>::singleton_instance_ = 0; 00079 00080 template<class T> 00081 boost::mutex Singleton<T>::instance_mutex_; 00082 00083 00084 template<class T> 00085 T& Singleton<T>::Instance() { 00086 if( singleton_instance_ == NULL) { 00087 boost::mutex::scoped_lock lock(instance_mutex_); 00088 if ( singleton_instance_ == NULL) { 00089 singleton_instance_ = new T; 00090 } 00091 } 00092 return *singleton_instance_; 00093 } 00094 00095 00096 template<class T> 00097 void Singleton<T>::Destroy() { 00098 // Lock mutex since it is a critical section. 00099 boost::mutex::scoped_lock lock(instance_mutex_); 00100 00101 // Delete the singleton instance. 00102 if( singleton_instance_ != NULL) { 00103 delete singleton_instance_; 00104 singleton_instance_ = NULL; 00105 } 00106 } 00107 00108 } // namespace metrivis 00109 00110 00111 #endif