-
Notifications
You must be signed in to change notification settings - Fork 68
/
SingletonHandler.h
60 lines (52 loc) · 881 Bytes
/
SingletonHandler.h
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
#include <mutex>
namespace ustdex
{
template <typename T>
class Singleton //: private T
{
private:
Singleton();
~Singleton();
public:
static T* instance();
static void release();
private:
static std::mutex lock_;
static T* instance_;
};
template <typename T>
Singleton<T>::~Singleton()
{
release();
}
template <class T>
std::mutex Singleton<T>::lock_;
template <class T>
T* Singleton<T>::instance_ = nullptr;
template <class T>
T* Singleton<T>::instance()
{
if (instance_ == nullptr)
{
lock_.lock();
if (instance_ == nullptr)
instance_ = new T;
lock_.unlock();
}
return instance_;
}
template <class T>
void Singleton<T>::release()
{
if (instance_ != nullptr)
{
lock_.lock();
if (instance_ != nullptr)
{
delete instance_;
instance_ = nullptr;
}
lock_.unlock();
}
}
} // namespace ustdex