retdec
singleton.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_SUPPORT_SINGLETON_H
8 #define RETDEC_LLVMIR2HLL_SUPPORT_SINGLETON_H
9 
10 namespace retdec {
11 namespace llvmir2hll {
12 
33 template<class T>
34 class Singleton {
35 public:
42  static T &getInstance() {
43  // This variant of returning a static variable has a big advantage - we
44  // don't have to care about the destruction of the object being held
45  // (if it was using dynamic allocation or it was declared static as a
46  // private variable, we would need to take care of it). Sometimes it's
47  // called "Meyer's singleton", according to the author.
48  static T instance;
49  return instance;
50  }
51 
52 public:
53  // Disable both constructors, destructor, and assignment operator.
54  // They are declared public to make diagnostics messages more precise.
55  Singleton() = delete;
56  Singleton(const Singleton &) = delete;
57  ~Singleton() = delete;
58  Singleton &operator=(const Singleton &) = delete;
59 };
60 
61 } // namespace llvmir2hll
62 } // namespace retdec
63 
64 #endif
Implementation of the Singleton design pattern.
Definition: singleton.h:34
static T & getInstance()
Returns the instance of the object being held.
Definition: singleton.h:42
Singleton & operator=(const Singleton &)=delete
Singleton(const Singleton &)=delete
A library providing API for working with back-end IR.
Definition: archive_wrapper.h:19