blob: b170210f916dacfbbb3d98266844bfb79dc8c76b (
plain)
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
|
#pragma once
// qt
#include <QMutex>
#include <QSharedPointer>
#define SINGLETON(type) Singleton<type>::get().data()
template <class T> class Singleton
{
public:
static QSharedPointer<T> get()
{
static QMutex mtx;
QMutexLocker l(&mtx);
if (!m_instance)
{
m_instance.reset(
static_cast<T*>(T::staticMetaObject.newInstance())
// FIXME: deleteLater doesn't work. Crashes without it
,
&T::deleteLater
);
}
return m_instance;
}
static void cleanup()
{
static QMutex mtx;
QMutexLocker l(&mtx);
m_instance.clear();
}
typedef QSharedPointer<T> FactorySharedPointer;
private:
static FactorySharedPointer m_instance;
};
template <class T>
typename Singleton<T>::FactorySharedPointer Singleton<T>::m_instance;
|