dune-common 2.1.1
|
00001 #ifndef DUNE_SINGLETON_HH 00002 #define DUNE_SINGLETON_HH 00003 00010 namespace Dune 00011 { 00048 template<class T> 00049 class Singleton 00050 { 00051 public: 00056 class InstancePointer 00057 { 00058 public: 00060 InstancePointer() : pointer_(0) 00061 {} 00063 ~InstancePointer() 00064 { 00065 if(pointer_ != 0) 00066 delete pointer_; 00067 } 00072 T* get() 00073 { 00074 return pointer_; 00075 } 00080 void set(T* pointer) 00081 { 00082 if(pointer != 0){ 00083 delete pointer_; 00084 pointer_ = pointer; 00085 } 00086 } 00087 private: 00088 T* pointer_; 00089 }; 00090 private: 00092 static InstancePointer instance_; 00093 protected: 00094 /* @brief Private constructor. */ 00095 Singleton(){} 00097 Singleton(const Singleton&){} 00099 Singleton& operator=(const Singleton&){} 00100 00101 public: 00106 static T& instance() 00107 { 00108 if(instance_.get() == 0) 00109 instance_.set(new T()); 00110 return *instance_.get(); 00111 } 00112 }; 00113 00114 template<class T> 00115 typename Singleton<T>::InstancePointer Singleton<T>::instance_; 00116 00117 } // namespace Dune 00118 00119 #endif