2023-09-17 20:36:33 +08:00
|
|
|
#ifndef SINGLETON_H
|
|
|
|
#define SINGLETON_H
|
|
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Singleton {
|
|
|
|
public:
|
|
|
|
static T* getInstance();
|
|
|
|
|
|
|
|
Singleton(const Singleton& other) = delete;
|
|
|
|
Singleton<T>& operator=(const Singleton& other) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static std::mutex mutex;
|
|
|
|
static T* instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::mutex Singleton<T>::mutex;
|
|
|
|
template <typename T>
|
|
|
|
T* Singleton<T>::instance;
|
|
|
|
template <typename T>
|
|
|
|
T* Singleton<T>::getInstance() {
|
|
|
|
if (instance == nullptr) {
|
|
|
|
std::lock_guard<std::mutex> locker(mutex);
|
|
|
|
if (instance == nullptr) {
|
|
|
|
instance = new T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2024-01-04 00:08:34 +08:00
|
|
|
#define SINGLETON(Class) \
|
2023-09-17 20:36:33 +08:00
|
|
|
private: \
|
|
|
|
friend class Singleton<Class>; \
|
|
|
|
friend struct QScopedPointerDeleter<Class>; \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
static Class* getInstance() { \
|
|
|
|
return Singleton<Class>::getInstance(); \
|
|
|
|
}
|
|
|
|
|
2023-12-30 20:33:33 +08:00
|
|
|
#define HIDE_CONSTRUCTOR(Class) \
|
|
|
|
private: \
|
|
|
|
Class() = default; \
|
|
|
|
Class(const Class& other) = delete; \
|
|
|
|
Class& operator=(const Class& other) = delete;
|
|
|
|
|
2023-09-17 20:36:33 +08:00
|
|
|
#endif // SINGLETON_H
|