-
Notifications
You must be signed in to change notification settings - Fork 1
/
Singleton.h
55 lines (42 loc) · 1.33 KB
/
Singleton.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
#ifndef HISIGNCORE_SINGLETON_H
#define HISIGNCORE_SINGLETON_H
#include <QMutex>
#include <QScopedPointer>
template <typename T>
class Singleton
{
public:
static T* getInstance();
private:
Singleton(const Singleton&);
Singleton<T>& operator = (const Singleton&);
static QMutex s_mutex;
static QScopedPointer<T> s_instance;
};
template <typename T> QMutex Singleton<T>::s_mutex;
template <typename T> QScopedPointer<T> Singleton<T>::s_instance;
template <typename T>
T* Singleton<T>::getInstance()
{
if(s_instance.isNull()) {
s_mutex.lock();
if(s_instance.isNull()) {
s_instance.reset(new T());
}
s_mutex.unlock();
}
return s_instance.data();
}
#define SINGLETON(Class) \
public: \
static Class* getInstance() { \
return Singleton<Class>::getInstance(); \
} \
private: \
Class(); \
~Class(); \
Class(const Class &other); \
Class& operator=(const Class &other); \
friend class Singleton<Class>; \
friend struct QScopedPointerDeleter<Class>;
#endif // HISIGNCORE_SINGLETON_H