dune-common  2.4.1
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 
9 
17 namespace Dune
18 {
54  template<class T>
55  class Singleton
56  {
57  protected:
58  /* @brief Private constructor. */
61  Singleton(const Singleton&){}
64 
65  public:
70  DUNE_EXPORT static T& instance()
71  {
72  /* Smartpointer to the instance. */
73  static std::unique_ptr<T> instance_;
74  if(instance_.get() == 0)
75  instance_ = std::unique_ptr<T>(new T());
76  return *instance_;
77  }
78  };
79 
80 } // namespace Dune
81 
82 #endif
Singleton()
Definition: singleton.hh:59
static DUNE_EXPORT T & instance()
Get the instance of the singleton.
Definition: singleton.hh:70
Singleton(const Singleton &)
Private copy constructor.
Definition: singleton.hh:61
Definition of macros controlling symbol visibility at the ABI level.
Singleton & operator=(const Singleton &)
Private assignment operator.
Definition: singleton.hh:63
An adapter to turn a class into a singleton.
Definition: singleton.hh:55
Dune namespace.
Definition: alignment.hh:9
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition: visibility.hh:18