qxLib
singleton.h
Go to the documentation of this file.
1 /**
2 
3  @file singleton.h
4  @author Khrapov
5  @date 17.06.2019
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <memory>
12 #include <mutex>
13 #include <type_traits>
14 #include <vector>
15 
16 namespace qx
17 {
18 
19 /**
20 
21  @class base_singleton
22  @brief Base singleton class
23  @author Khrapov
24  @date 18.09.2025
25 
26 **/
28 {
29  template<class T, class... dependencies_t>
30  requires(std::is_base_of_v<base_singleton, dependencies_t> && ...)
31  friend class singleton;
32 
33 public:
34  virtual ~base_singleton() = default;
35 
36 private:
37  base_singleton() = default;
38 };
39 
40 /**
41 
42  @class singletons_manager
43  @brief The class provides a solution to the fiasco static deinitialization for qx::singleton
44  by storing all singletons in one place and removing them in the reverse order of addition.
45  @author Khrapov
46  @date 18.09.2025
47 
48 **/
50 {
51  template<class T, class... dependencies_t>
52  requires(std::is_base_of_v<base_singleton, dependencies_t> && ...)
53  friend class singleton;
54 
55 public:
56  singletons_manager(const singletons_manager&) = delete;
58  const singletons_manager& operator=(const singletons_manager&) = delete;
59  const singletons_manager& operator=(singletons_manager&&) = delete;
60 
61 private:
62  singletons_manager() noexcept = default;
63  ~singletons_manager() noexcept;
64 
65  /**
66  @brief Returns singletons manager instance
67  @retval - singletons manager instance
68  **/
69  static singletons_manager& get_instance() noexcept;
70 
71  /**
72  @brief Add a new singleton
73  @note Thread safe
74  @param pSingleton - new singleton
75  **/
76  void add(std::unique_ptr<base_singleton> pSingleton) noexcept;
77 
78 private:
79  std::mutex m_SingletonsMutex;
80  std::vector<std::unique_ptr<base_singleton>> m_Singletons;
81 };
82 
83 /**
84 
85  @class singleton
86  @brief Inherit the necessary singleton class from this
87  @details You can override init() and a destructor to control object creation and destruction events.
88  You can use multiple inheritance with this class, but T must be final.
89  @tparam T - singleton type
90  @tparam dependencies_t - singleton dependencies, they will be initialized before this one,
91  unless they have already been initialized
92  @author Khrapov
93  @date 18.09.2025
94 
95 **/
96 template<class T, class... dependencies_t>
97  requires(std::is_base_of_v<base_singleton, dependencies_t> && ...)
98 class singleton : public base_singleton
99 {
100  friend T;
101 
102 public:
103  /**
104  @brief Get singleton instance
105  @retval - singleton instance
106  **/
107  static T& get_instance();
108 
109  /**
110  @brief Init the singleton (called on the first get_instance())
111  **/
112  virtual void init();
113 
114  singleton(const singleton&) = delete;
115  singleton(singleton&&) = delete;
116  singleton& operator=(const singleton&) = delete;
117  singleton& operator=(singleton&&) = delete;
118 
119 private:
120  singleton() noexcept = default;
121 };
122 
123 } // namespace qx
124 
125 #include <qx/patterns/singleton.inl>
Base singleton class.
Definition: singleton.h:28
The class provides a solution to the fiasco static deinitialization for qx::singleton by storing all ...
Definition: singleton.h:50
Inherit the necessary singleton class from this.