-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.hpp
255 lines (215 loc) · 7.56 KB
/
Singleton.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**************************************************************************
* Singleton v1.0.2
*
* Singleton is a C/CPP library for creating portable singleton objects.
* It is designed to be easy to use and easy to integrate into your project
* using a single header file (stb style).
*
* This file is part of the Singleton project that is under Anti-NN License.
* https://github.com/MasterLaplace/Anti-NN_LICENSE
* Copyright © 2024 by @MasterLaplace, All rights reserved.
*
* Singleton is a free software: you can redistribute it and/or modify
* it under the terms of the Anti-NN License as published by MasterLaplace.
* See the Anti-NN License for more details.
*
* @file Singleton.hpp
* @brief A thread-safe Singleton template class.
*
* @author @MasterLaplace
* @version 1.0.2
* @date 2024-11-06
**************************************************************************/
// clang-format off
#ifndef SINGLETON_HPP_
#define SINGLETON_HPP_
#ifndef SINGLETON_STANDALONE
#include "singleton_config.h"
#else
#if defined(__cplusplus) && __cplusplus >= 201103L
#define lpl_nullptr nullptr
#elif !defined(NULL)
#define lpl_nullptr ((void*)0)
#else
#define lpl_nullptr NULL
#endif
#if __cplusplus >= 201703L
#define SINGLETON_CPP17(_) _
#else
#define SINGLETON_CPP17(_)
#endif
#endif /* !SINGLETON_STANDALONE */
#include <iostream>
#include <cassert>
#ifndef SINGLETON_NO_THREAD_SAFETY
#define SINGLETON_THREAD_SAFE(_) _
#include <mutex>
#else
#define SINGLETON_THREAD_SAFE(_)
#endif /* !SINGLETON_NO_THREAD_SAFETY */
/**
* @brief A thread-safe Singleton template class.
*
* This class provides a thread-safe implementation of the Singleton design pattern.
* You can inherit from this class to create a Singleton object.
* It ensures that only one instance of the class is created and provides global
* access to that instance.
*
* @note This class is thread-safe by default. You can desable thread-safety
* by defining SINGLETON_NO_THREAD_SAFETY before including this header.
*
* @tparam T The type of the Singleton class.
*/
template <typename T>
class Singleton {
public:
/**
* @brief Create an instance of the Singleton class.
*
* This method creates an instance of the Singleton class with the
* provided arguments. If an instance already exists, it will assert.
*
* @tparam Args The types of the arguments to pass to the constructor.
* @param args The arguments to pass to the constructor.
*/
template <typename... Args>
static inline void CreateInstance(Args &&...args);
/**
* @brief Reset the Singleton instance with new arguments.
*
* This method deletes the current instance and creates a new one with
* the provided arguments. If no instance exists, it will assert.
*
* @tparam Args The types of the arguments to pass to the constructor.
* @param args The arguments to pass to the constructor.
*/
template <typename... Args>
static inline void ResetInstance(Args &&...args);
/**
* @brief Get the Singleton instance.
*
* This method returns a reference to the Singleton instance.
* If no instance exists, it will assert.
*
* @note This method locks the instance mutex. Make sure to call
* UnlockInstance after use. If SINGLETON_NO_THREAD_SAFETY is
* defined, this method will not lock the instance mutex.
*
* @return T& A reference to the Singleton instance.
*/
static inline T &GetInstance();
/**
* @brief Unlock the Singleton instance.
*
* This method unlocks the instance mutex after use.
* If SINGLETON_NO_THREAD_SAFETY is defined, this method does nothing.
*
* @note Make sure to call this method after calling GetInstance.
*/
static inline void UnlockInstance();
/**
* @brief Destroy the Singleton instance.
*
* This method deletes the Singleton instance and sets the instance
* pointer to nullptr. If no instance exists, it will assert.
*/
static inline void DestroyInstance();
/**
* @brief Deleted copy constructor.
*
* This prevents copying of the Singleton instance.
*
* @param copy The instance to copy.
*/
Singleton(const T ©) = delete;
/**
* @brief Deleted copy assignment operator.
*
* This prevents assignment of the Singleton instance.
*
* @param copy The instance to assign.
* @return T& A reference to the assigned instance.
*/
T &operator=(const T ©) = delete;
protected:
/**
* @brief Protected default constructor.
*
* This ensures that the Singleton class can only be instantiated through
* the CreateInstance method.
*/
Singleton() = default;
/**
* @brief Protected virtual destructor.
*
* This ensures that the Singleton class can be properly destroyed.
*/
virtual ~Singleton() = default;
private:
/**
* @brief Pointer to the Singleton instance.
*/
static SINGLETON_CPP17(inline) T *_instance SINGLETON_CPP17(= lpl_nullptr);
#ifndef SINGLETON_NO_THREAD_SAFETY
/**
* @brief Mutex for thread-safe access to the Singleton instance.
*/
static SINGLETON_CPP17(inline) std::mutex _mutex;
#endif /* !SINGLETON_NO_THREAD_SAFETY */
};
#endif /* !SINGLETON_HPP_ */
////////////////////////////////////////////////////////////////////////////////
/* IMPLEMENTATION */
////////////////////////////////////////////////////////////////////////////////
#ifdef SINGLETON_IMPLEMENTATION
#ifndef SINGLETON_CPP_ONCE
#define SINGLETON_CPP_ONCE
#if __cplusplus < 201703L
template <typename T>
T *Singleton<T>::_instance;
#ifndef SINGLETON_NO_THREAD_SAFETY
template <typename T>
std::mutex Singleton<T>::_mutex;
#endif /* !SINGLETON_NO_THREAD_SAFETY */
#endif /* __cplusplus < 201703L */
template <typename T>
template <typename... Args>
inline void Singleton<T>::CreateInstance(Args &&...args)
{
assert(!_instance && "Singleton instance is already created! Use ResetInstance() to reset it.");
SINGLETON_THREAD_SAFE(std::lock_guard<std::mutex> lock(_mutex));
_instance = new T(std::forward<Args>(args)...);
}
template <typename T>
template <typename... Args>
inline void Singleton<T>::ResetInstance(Args &&...args)
{
assert(_instance && "Singleton instance is not created yet!");
SINGLETON_THREAD_SAFE(std::lock_guard<std::mutex> lock(_mutex));
delete _instance;
_instance = new T(std::forward<Args>(args)...);
}
template <typename T>
inline T &Singleton<T>::GetInstance()
{
assert(_instance && "Singleton instance is not created yet!");
SINGLETON_THREAD_SAFE(_mutex.lock());
return *_instance;
}
template <typename T>
inline void Singleton<T>::UnlockInstance()
{
assert(_instance && "Singleton instance is not created yet!");
SINGLETON_THREAD_SAFE(_mutex.unlock());
}
template <typename T>
inline void Singleton<T>::DestroyInstance()
{
assert(_instance && "Singleton instance is not created yet!");
SINGLETON_THREAD_SAFE(std::lock_guard<std::mutex> lock(_mutex));
delete _instance;
_instance = lpl_nullptr;
}
#endif /* !SINGLETON_CPP_ONCE */
#endif /* !SINGLETON_IMPLEMENTATION */
// clang-format on