-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingletonHolder.h
100 lines (91 loc) · 3.25 KB
/
SingletonHolder.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
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
/*
* File: singleton.h
* Author: apiotro
*
*/
#ifndef SINGLETON_H
#define SINGLETON_H
#include <cstddef>
#include <cstdlib>
#include <stdexcept>
//===================================================================================================
template<class T, template<class> class CreationPolicy, template<class> class LifetimePolicy,
template<class> class ThreadingModel, int V = 0>
class SingletonHolder {
public:
static T& Instance();
private:
SingletonHolder();
static void DestroySingleton();
static T* pInstance;
static bool destroyed;
};
template<class T, template<class> class CreationPolicy, template<class> class LifetimePolicy,
template<class> class ThreadingModel, int V>
T* SingletonHolder<T, CreationPolicy, LifetimePolicy, ThreadingModel, V>::pInstance = NULL;
template<class T, template<class> class CreationPolicy, template<class> class LifetimePolicy,
template<class> class ThreadingModel, int V>
bool SingletonHolder<T, CreationPolicy, LifetimePolicy, ThreadingModel, V>::destroyed = false;
template<class T, template<class> class CreationPolicy, template<class> class LifetimePolicy,
template<class> class ThreadingModel, int V>
void SingletonHolder<T, CreationPolicy, LifetimePolicy, ThreadingModel, V>::DestroySingleton() {
CreationPolicy<T>::Destroy(pInstance);
pInstance = 0;
destroyed = true;
}
template<class T, template<class> class CreationPolicy, template<class> class LifetimePolicy,
template<class> class ThreadingModel, int V>
T& SingletonHolder<T, CreationPolicy, LifetimePolicy, ThreadingModel, V>::Instance() {
if(pInstance == NULL) {
typename ThreadingModel<T>::Lock guard __attribute__((unused));
if(pInstance == NULL) {
if(destroyed) {
LifetimePolicy<T>::OnDeadReference();
destroyed = false;
}
pInstance = CreationPolicy<T>::Create();
LifetimePolicy<T>::ScheduleDestruction(&DestroySingleton);
}
}
return *pInstance;
}
//==============================================================================
template<class T>
class CreateByNew {
public:
static T* Create() { return new T; }
static void Destroy(T* pObj) { delete pObj; }
};
//==============================================================================
template<class T>
class SingleThread {
public:
class Lock {};
};
//==============================================================================
// template <class T>
// class ClassLevelLockable
// {
// public:
// class Lock
// {
// private:
// static pthread_mutex_t mutex;
// public:
// Lock() {pthread_mutex_lock(&mutex);}
// Lock(T& obj) {}
// ~Lock() {pthread_mutex_unlock(&mutex);}
// };
// };
// template <class T>
// pthread_mutex_t ClassLevelLockable<T>::Lock::mutex =
// PTHREAD_MUTEX_INITIALIZER;
//==============================================================================
template<class T>
class LifetimeStandard {
public:
static void ScheduleDestruction(void (*pFun)(void)) { atexit(pFun); }
static void OnDeadReference() { throw std::runtime_error("Internal error: dead reference in singleton class"); }
};
//==============================================================================
#endif /* SINGLETON_H */