diff --git a/example/src/AppInfo.h b/example/src/AppInfo.h index dcc59543..ff2a0ffc 100644 --- a/example/src/AppInfo.h +++ b/example/src/AppInfo.h @@ -13,7 +13,7 @@ class AppInfo : public QObject private: explicit AppInfo(QObject *parent = nullptr); public: - SINGLETONG(AppInfo) + SINGLETON(AppInfo) }; #endif // APPINFO_H diff --git a/example/src/helper/SettingsHelper.h b/example/src/helper/SettingsHelper.h index e1f7954d..18dab549 100644 --- a/example/src/helper/SettingsHelper.h +++ b/example/src/helper/SettingsHelper.h @@ -16,7 +16,7 @@ class SettingsHelper : public QObject private: explicit SettingsHelper(QObject* parent = nullptr); public: - SINGLETONG(SettingsHelper) + SINGLETON(SettingsHelper) ~SettingsHelper() override; void init(char *argv[]); Q_INVOKABLE void saveDarkMode(int darkModel){save("darkMode",darkModel);} diff --git a/example/src/singleton.h b/example/src/singleton.h index f6f25892..c1a71ce1 100644 --- a/example/src/singleton.h +++ b/example/src/singleton.h @@ -34,7 +34,7 @@ T* Singleton::getInstance() { return instance; } -#define SINGLETONG(Class) \ +#define SINGLETON(Class) \ private: \ friend class Singleton; \ friend struct QScopedPointerDeleter; \ diff --git a/src/FluApp.h b/src/FluApp.h index 439606c2..3a7f3da7 100644 --- a/src/FluApp.h +++ b/src/FluApp.h @@ -29,7 +29,7 @@ private: explicit FluApp(QObject *parent = nullptr); ~FluApp(); public: - SINGLETONG(FluApp) + SINGLETON(FluApp) static FluApp *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE void run(); Q_INVOKABLE void navigate(const QString& route,const QJsonObject& argument = {},FluRegister* fluRegister = nullptr); diff --git a/src/FluColors.h b/src/FluColors.h index 052cabea..9b935fc7 100644 --- a/src/FluColors.h +++ b/src/FluColors.h @@ -51,7 +51,7 @@ class FluColors : public QObject private: explicit FluColors(QObject *parent = nullptr); public: - SINGLETONG(FluColors) + SINGLETON(FluColors) static FluColors *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} }; diff --git a/src/FluEventBus.h b/src/FluEventBus.h index 8d98799c..0622c5f5 100644 --- a/src/FluEventBus.h +++ b/src/FluEventBus.h @@ -23,7 +23,7 @@ class FluEventBus : public QObject private: explicit FluEventBus(QObject *parent = nullptr); public: - SINGLETONG(FluEventBus) + SINGLETON(FluEventBus) static FluEventBus *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE void registerEvent(FluEvent* event); Q_INVOKABLE void unRegisterEvent(FluEvent* event); diff --git a/src/FluNetwork.h b/src/FluNetwork.h index 4a8ed8e3..410dc8e5 100644 --- a/src/FluNetwork.h +++ b/src/FluNetwork.h @@ -104,7 +104,7 @@ class FluNetwork : public QObject private: explicit FluNetwork(QObject *parent = nullptr); public: - SINGLETONG(FluNetwork) + SINGLETON(FluNetwork) static FluNetwork *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE NetworkParams* get(const QString& url); Q_INVOKABLE NetworkParams* head(const QString& url); diff --git a/src/FluTextStyle.h b/src/FluTextStyle.h index c266a786..6dafc45f 100644 --- a/src/FluTextStyle.h +++ b/src/FluTextStyle.h @@ -23,7 +23,7 @@ public: private: explicit FluTextStyle(QObject *parent = nullptr); public: - SINGLETONG(FluTextStyle) + SINGLETON(FluTextStyle) static FluTextStyle *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} }; diff --git a/src/FluTheme.h b/src/FluTheme.h index 3d0e00d0..e0da349d 100644 --- a/src/FluTheme.h +++ b/src/FluTheme.h @@ -40,7 +40,7 @@ private: bool systemDark(); void refreshColors(); public: - SINGLETONG(FluTheme) + SINGLETON(FluTheme) Q_INVOKABLE QJsonArray awesomeList(const QString& keyword = ""); Q_SIGNAL void darkChanged(); static FluTheme *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} diff --git a/src/FluTools.h b/src/FluTools.h index c4f384c1..dbf99aca 100644 --- a/src/FluTools.h +++ b/src/FluTools.h @@ -19,7 +19,7 @@ class FluTools : public QObject private: explicit FluTools(QObject *parent = nullptr); public: - SINGLETONG(FluTools) + SINGLETON(FluTools) static FluTools *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} Q_INVOKABLE int qtMajor(); Q_INVOKABLE int qtMinor(); diff --git a/src/FluViewModel.h b/src/FluViewModel.h index 93bb9bf3..c82c1661 100644 --- a/src/FluViewModel.h +++ b/src/FluViewModel.h @@ -53,7 +53,7 @@ class ViewModelManager:public QObject{ private: explicit ViewModelManager(QObject *parent = nullptr); public: - SINGLETONG(ViewModelManager) + SINGLETON(ViewModelManager) bool exist(const QString& key); void insert(const QString& key,QObject* value); QObject* getModel(const QString& key); diff --git a/src/FluentUI.h b/src/FluentUI.h index a8fa2b25..b9c4977c 100644 --- a/src/FluentUI.h +++ b/src/FluentUI.h @@ -9,7 +9,7 @@ class FluentUI : public QObject { Q_OBJECT public: - SINGLETONG(FluentUI) + SINGLETON(FluentUI) Q_DECL_EXPORT void registerTypes(QQmlEngine *engine); void registerTypes(const char *uri); void initializeEngine(QQmlEngine *engine, const char *uri); diff --git a/src/singleton.h b/src/singleton.h index f6f25892..4053d6f4 100644 --- a/src/singleton.h +++ b/src/singleton.h @@ -2,42 +2,30 @@ #define SINGLETON_H #include -#include -#include -#include template class Singleton { public: static T* getInstance(); - Singleton(const Singleton& other) = delete; - Singleton& operator=(const Singleton& other) = delete; - private: - static std::mutex mutex; - static T* instance; + Q_DISABLE_COPY_MOVE(Singleton) }; -template -std::mutex Singleton::mutex; -template -T* Singleton::instance; template T* Singleton::getInstance() { + static QMutex mutex; + QMutexLocker locker(&mutex); + static T* instance = nullptr; if (instance == nullptr) { - std::lock_guard locker(mutex); - if (instance == nullptr) { - instance = new T(); - } + instance = new T(); } return instance; } -#define SINGLETONG(Class) \ +#define SINGLETON(Class) \ private: \ friend class Singleton; \ - friend struct QScopedPointerDeleter; \ \ public: \ static Class* getInstance() { \ @@ -48,6 +36,6 @@ private: \ private: \ Class() = default; \ Class(const Class& other) = delete; \ - Class& operator=(const Class& other) = delete; + Q_DISABLE_COPY_MOVE(Class); #endif // SINGLETON_H