ORIGINAL
Loading...
Searching...
No Matches
singleton.h
1#ifndef ORIGINAL_SINGLETON_H
2#define ORIGINAL_SINGLETON_H
3#include "ownerPtr.h"
4
5namespace original {
24 template<typename TYPE>
25 class singleton {
26 static ownerPtr<TYPE> instance_;
27
31 singleton() = default;
32
36 ~singleton() = default;
37
38 public:
42 singleton(const singleton&) = delete;
43
47 singleton& operator=(const singleton&) = delete;
48
52 singleton(singleton&&) = delete;
53
58
63 static bool exist();
64
75 template<typename... Args>
76 static void init(Args&&... args);
77
88 static TYPE& instance();
89
95 static void clear();
96
107 template<typename... Args>
108 static void reset(Args&&... args);
109 };
110}
111
112template<typename TYPE>
114
115template <typename TYPE>
117 return instance_ != nullptr;
118}
119
120template <typename TYPE>
121template <typename ... Args>
123 if (instance_ == nullptr) {
124 instance_ = std::move(makeOwnerPtr<TYPE>(std::forward<Args>(args)...));
125 } else {
126 throw valueError("Instance already exists, do you mean reset(args...)?");
127 }
128}
129
130template <typename TYPE>
132 if (instance_ == nullptr) {
133 throw nullPointerError("Instance not exist, call init(args...) first");
134 }
135 return *instance_;
136}
137
138template <typename TYPE>
140 if (instance_ != nullptr) {
141 instance_ = std::move(original::ownerPtr<TYPE>());
142 }
143}
144
145template <typename TYPE>
146template <typename ... Args>
148 instance_ = std::move(makeOwnerPtr<TYPE>(std::forward<Args>(args)...));
149}
150
151#endif //ORIGINAL_SINGLETON_H
Exception for null pointer dereference attempts.
Definition error.h:171
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Thread-safe singleton pattern implementation with ownership management.
Definition singleton.h:25
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:139
static bool exist()
Checks if the singleton instance exists.
Definition singleton.h:116
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:147
static TYPE & instance()
Provides access to the singleton instance.
Definition singleton.h:131
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:122
Exception for invalid parameter values.
Definition error.h:150
Main namespace for the project Original.
Definition algorithms.h:21
Exclusive-ownership smart pointer implementation.