Singleton.hpp
1 
8 #ifndef EMX_Singleton_HPP
9 #define EMX_Singleton_HPP
10 
11 #include <iostream>
12 #include <memory>
13 #include <mutex>
14 
15 namespace Emx {
20  template <typename T>
21  class Singleton {
22  public:
23  Singleton(const Singleton&) = delete;
24  Singleton& operator=(const Singleton&) = delete;
25 
26  static T& Get() {
27  if (m_instance == nullptr) {
28  std::lock_guard<std::mutex> lock(m_locker);
29  if (m_instance == nullptr) {
30  m_instance.reset(new T());
31  }
32  }
33  return *m_instance;
34  }
35 
36  static void Release() {
37  std::lock_guard<std::mutex> lock(m_locker);
38  m_instance.reset(nullptr);
39  }
40 
41  protected:
42  Singleton() = default;
43  virtual ~Singleton() = default;
44 
45  private:
46  static std::mutex m_locker;
47  static std::unique_ptr<T> m_instance;
48  };
49 
50  template <typename T>
51  std::mutex Singleton<T>::m_locker;
52 
53  template <typename T>
54  std::unique_ptr<T> Singleton<T>::m_instance = nullptr;
56 }
57 
58 #endif //EMX_Singleton_HPP
Definition: Singleton.hpp:21
Singleton & operator=(const Singleton &)=delete
static void Release()
Definition: Singleton.hpp:36
Singleton()=default
Singleton(const Singleton &)=delete
static T & Get()
Definition: Singleton.hpp:26
virtual ~Singleton()=default
Definition: EmxGpio.hpp:10