ORIGINAL
Loading...
Searching...
No Matches
singleton.h
Go to the documentation of this file.
1#ifndef ORIGINAL_SINGLETON_H
2#define ORIGINAL_SINGLETON_H
3#include "ownerPtr.h"
4
5
47namespace original {
66 template<typename TYPE>
67 class singleton {
68 static ownerPtr<TYPE> instance_;
69
73 singleton() = default;
74
78 ~singleton() = default;
79
80 public:
84 singleton(const singleton&) = delete;
85
89 singleton& operator=(const singleton&) = delete;
90
94 singleton(singleton&&) = delete;
95
100
105 static bool exist();
106
117 template<typename... Args>
118 static void init(Args&&... args);
119
130 static TYPE& instance();
131
137 static void clear();
138
149 template<typename... Args>
150 static void reset(Args&&... args);
151 };
152}
153
154template<typename TYPE>
156
157template <typename TYPE>
159 return instance_ != nullptr;
160}
161
162template <typename TYPE>
163template <typename ... Args>
165 if (instance_ == nullptr) {
166 instance_ = std::move(makeOwnerPtr<TYPE>(std::forward<Args>(args)...));
167 } else {
168 throw valueError("Instance already exists, do you mean reset(args...)?");
169 }
170}
171
172template <typename TYPE>
174 if (instance_ == nullptr) {
175 throw nullPointerError("Instance not exist, call init(args...) first");
176 }
177 return *instance_;
178}
179
180template <typename TYPE>
182 if (instance_ != nullptr) {
183 instance_ = std::move(original::ownerPtr<TYPE>());
184 }
185}
186
187template <typename TYPE>
188template <typename ... Args>
190 instance_ = std::move(makeOwnerPtr<TYPE>(std::forward<Args>(args)...));
191}
192
193#endif //ORIGINAL_SINGLETON_H
Exception for null pointer dereference attempts.
Definition error.h:245
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Thread-safe singleton pattern implementation with ownership management.
Definition singleton.h:67
singleton(singleton &&)=delete
Deleted move constructor to prevent moving.
singleton(const singleton &)=delete
Deleted copy constructor to prevent copying.
static void clear()
Clears the singleton instance.
Definition singleton.h:181
static bool exist()
Checks if the singleton instance exists.
Definition singleton.h:158
singleton & operator=(const singleton &)=delete
Deleted copy assignment operator to prevent copying.
static void reset(Args &&... args)
Resets the singleton instance with new arguments.
Definition singleton.h:189
static TYPE & instance()
Provides access to the singleton instance.
Definition singleton.h:173
singleton & operator=(singleton &&)=delete
Deleted move assignment operator to prevent moving.
static void init(Args &&... args)
Initializes the singleton instance with provided arguments.
Definition singleton.h:164
Exception for invalid parameter values.
Definition error.h:219
Main namespace for the project Original.
Definition algorithms.h:21
Exclusive-ownership smart pointer implementation.