dune-common  2.3.0
singleton.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_SINGLETON_HH
4 #define DUNE_SINGLETON_HH
5 
6 #include <memory>
7 
15 namespace Dune
16 {
52  template<class T>
53  class Singleton
54  {
56  static std::auto_ptr<T> instance_;
57  protected:
58  /* @brief Private constructor. */
61  Singleton(const Singleton&){}
64 
65  public:
70  static T& instance()
71  {
72  if(instance_.get() == 0)
73  instance_ = std::auto_ptr<T>(new T());
74  return *instance_;
75  }
76  };
77 
78  template<class T>
79  typename std::auto_ptr<T> Singleton<T>::instance_;
80 
81 } // namespace Dune
82 
83 #endif