dune-common  2.2.0
singleton.hh
Go to the documentation of this file.
00001 #ifndef DUNE_SINGLETON_HH
00002 #define DUNE_SINGLETON_HH
00003 
00004 #include <memory>
00005 
00013 namespace Dune
00014 {
00050   template<class T>
00051   class Singleton
00052   {
00054     static std::auto_ptr<T> instance_;
00055   protected:
00056     /* @brief Private constructor. */
00057     Singleton(){}
00059     Singleton(const Singleton&){}
00061     Singleton& operator=(const Singleton&){}
00062     
00063   public:
00068     static T& instance()
00069     {
00070       if(instance_.get() == 0)
00071         instance_ = std::auto_ptr<T>(new T());
00072       return *instance_;
00073     }
00074   };   
00075 
00076   template<class T> 
00077   typename std::auto_ptr<T> Singleton<T>::instance_;
00078   
00079 } // namespace Dune
00080 
00081 #endif