carl  24.04
Computer ARithmetic Library
Singleton.h
Go to the documentation of this file.
1 /**
2  * @file Singleton.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  */
5 
6 #pragma once
7 
8 namespace carl
9 {
10 
11 /**
12  * Base class that implements a singleton.
13  *
14  * A class that shall be a singleton can inherit from this class (the template argument being the class itself, see CRTP for this).
15  * It takes care of
16  * <ul>
17  * <li>deleting the copy constructor and the assignment operator,</li>
18  * <li>providing a protected default constructor and a virtual destructor and</li>
19  * <li>providing getInstance() that returns the one single object of this type.</li>
20  * </ul>
21  */
22 template<typename T>
23 class Singleton
24 {
25 protected:
26  /**
27  * Protected default constructor.
28  */
29  Singleton() = default;
30 
31 public:
32  Singleton(const Singleton&) = delete;
33  Singleton(Singleton&&) = delete;
34  Singleton& operator=(const Singleton&) = delete;
36 
37  /**
38  * Virtual destructor.
39  */
40  virtual ~Singleton() noexcept = default;
41  /**
42  * Returns the single instance of this class by reference.
43  * If there is no instance yet, a new one is created.
44  */
45  static T& getInstance() {
46  static T t;
47  return t;
48  }
49 };
50 
51 }
carl is the main namespace for the library.
Base class that implements a singleton.
Definition: Singleton.h:24
Singleton()=default
Protected default constructor.
Singleton & operator=(const Singleton &)=delete
virtual ~Singleton() noexcept=default
Virtual destructor.
static T & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45
Singleton & operator=(Singleton &&)=delete
Singleton(const Singleton &)=delete
Singleton(Singleton &&)=delete