FluentUI/src/singleton.h

42 lines
1.0 KiB
C
Raw Normal View History

2023-09-13 15:11:22 +08:00
#ifndef SINGLETON_H
#define SINGLETON_H
#include <QMutex>
template <typename T>
class Singleton {
public:
static T* getInstance();
private:
Q_DISABLE_COPY_MOVE(Singleton)
2023-09-13 15:11:22 +08:00
};
template <typename T>
T* Singleton<T>::getInstance() {
static QMutex mutex;
QMutexLocker locker(&mutex);
static T* instance = nullptr;
2023-09-13 15:11:22 +08:00
if (instance == nullptr) {
instance = new T();
2023-09-13 15:11:22 +08:00
}
return instance;
}
#define SINGLETON(Class) \
2023-09-13 15:11:22 +08:00
private: \
friend class Singleton<Class>; \
\
public: \
static Class* getInstance() { \
return Singleton<Class>::getInstance(); \
}
2023-12-30 20:34:14 +08:00
#define HIDE_CONSTRUCTOR(Class) \
private: \
Class() = default; \
Class(const Class& other) = delete; \
Q_DISABLE_COPY_MOVE(Class);
2023-12-30 20:34:14 +08:00
2023-09-13 15:11:22 +08:00
#endif // SINGLETON_H