Use more elegant singleton and fix spell bug.

This commit is contained in:
Mentalflow 2024-01-04 00:08:34 +08:00
parent bb19554215
commit 8778ca85a3
No known key found for this signature in database
GPG Key ID: 5AE68D4401A2EE71
13 changed files with 19 additions and 31 deletions

View File

@ -13,7 +13,7 @@ class AppInfo : public QObject
private: private:
explicit AppInfo(QObject *parent = nullptr); explicit AppInfo(QObject *parent = nullptr);
public: public:
SINGLETONG(AppInfo) SINGLETON(AppInfo)
}; };
#endif // APPINFO_H #endif // APPINFO_H

View File

@ -16,7 +16,7 @@ class SettingsHelper : public QObject
private: private:
explicit SettingsHelper(QObject* parent = nullptr); explicit SettingsHelper(QObject* parent = nullptr);
public: public:
SINGLETONG(SettingsHelper) SINGLETON(SettingsHelper)
~SettingsHelper() override; ~SettingsHelper() override;
void init(char *argv[]); void init(char *argv[]);
Q_INVOKABLE void saveDarkMode(int darkModel){save("darkMode",darkModel);} Q_INVOKABLE void saveDarkMode(int darkModel){save("darkMode",darkModel);}

View File

@ -34,7 +34,7 @@ T* Singleton<T>::getInstance() {
return instance; return instance;
} }
#define SINGLETONG(Class) \ #define SINGLETON(Class) \
private: \ private: \
friend class Singleton<Class>; \ friend class Singleton<Class>; \
friend struct QScopedPointerDeleter<Class>; \ friend struct QScopedPointerDeleter<Class>; \

View File

@ -29,7 +29,7 @@ private:
explicit FluApp(QObject *parent = nullptr); explicit FluApp(QObject *parent = nullptr);
~FluApp(); ~FluApp();
public: public:
SINGLETONG(FluApp) SINGLETON(FluApp)
static FluApp *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluApp *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
Q_INVOKABLE void run(); Q_INVOKABLE void run();
Q_INVOKABLE void navigate(const QString& route,const QJsonObject& argument = {},FluRegister* fluRegister = nullptr); Q_INVOKABLE void navigate(const QString& route,const QJsonObject& argument = {},FluRegister* fluRegister = nullptr);

View File

@ -51,7 +51,7 @@ class FluColors : public QObject
private: private:
explicit FluColors(QObject *parent = nullptr); explicit FluColors(QObject *parent = nullptr);
public: public:
SINGLETONG(FluColors) SINGLETON(FluColors)
static FluColors *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluColors *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
}; };

View File

@ -23,7 +23,7 @@ class FluEventBus : public QObject
private: private:
explicit FluEventBus(QObject *parent = nullptr); explicit FluEventBus(QObject *parent = nullptr);
public: public:
SINGLETONG(FluEventBus) SINGLETON(FluEventBus)
static FluEventBus *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluEventBus *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
Q_INVOKABLE void registerEvent(FluEvent* event); Q_INVOKABLE void registerEvent(FluEvent* event);
Q_INVOKABLE void unRegisterEvent(FluEvent* event); Q_INVOKABLE void unRegisterEvent(FluEvent* event);

View File

@ -104,7 +104,7 @@ class FluNetwork : public QObject
private: private:
explicit FluNetwork(QObject *parent = nullptr); explicit FluNetwork(QObject *parent = nullptr);
public: public:
SINGLETONG(FluNetwork) SINGLETON(FluNetwork)
static FluNetwork *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluNetwork *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
Q_INVOKABLE NetworkParams* get(const QString& url); Q_INVOKABLE NetworkParams* get(const QString& url);
Q_INVOKABLE NetworkParams* head(const QString& url); Q_INVOKABLE NetworkParams* head(const QString& url);

View File

@ -23,7 +23,7 @@ public:
private: private:
explicit FluTextStyle(QObject *parent = nullptr); explicit FluTextStyle(QObject *parent = nullptr);
public: public:
SINGLETONG(FluTextStyle) SINGLETON(FluTextStyle)
static FluTextStyle *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluTextStyle *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
}; };

View File

@ -40,7 +40,7 @@ private:
bool systemDark(); bool systemDark();
void refreshColors(); void refreshColors();
public: public:
SINGLETONG(FluTheme) SINGLETON(FluTheme)
Q_INVOKABLE QJsonArray awesomeList(const QString& keyword = ""); Q_INVOKABLE QJsonArray awesomeList(const QString& keyword = "");
Q_SIGNAL void darkChanged(); Q_SIGNAL void darkChanged();
static FluTheme *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluTheme *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}

View File

@ -19,7 +19,7 @@ class FluTools : public QObject
private: private:
explicit FluTools(QObject *parent = nullptr); explicit FluTools(QObject *parent = nullptr);
public: public:
SINGLETONG(FluTools) SINGLETON(FluTools)
static FluTools *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();} static FluTools *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
Q_INVOKABLE int qtMajor(); Q_INVOKABLE int qtMajor();
Q_INVOKABLE int qtMinor(); Q_INVOKABLE int qtMinor();

View File

@ -53,7 +53,7 @@ class ViewModelManager:public QObject{
private: private:
explicit ViewModelManager(QObject *parent = nullptr); explicit ViewModelManager(QObject *parent = nullptr);
public: public:
SINGLETONG(ViewModelManager) SINGLETON(ViewModelManager)
bool exist(const QString& key); bool exist(const QString& key);
void insert(const QString& key,QObject* value); void insert(const QString& key,QObject* value);
QObject* getModel(const QString& key); QObject* getModel(const QString& key);

View File

@ -9,7 +9,7 @@ class FluentUI : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
SINGLETONG(FluentUI) SINGLETON(FluentUI)
Q_DECL_EXPORT void registerTypes(QQmlEngine *engine); Q_DECL_EXPORT void registerTypes(QQmlEngine *engine);
void registerTypes(const char *uri); void registerTypes(const char *uri);
void initializeEngine(QQmlEngine *engine, const char *uri); void initializeEngine(QQmlEngine *engine, const char *uri);

View File

@ -2,42 +2,30 @@
#define SINGLETON_H #define SINGLETON_H
#include <QMutex> #include <QMutex>
#include <QScopedPointer>
#include <memory>
#include <mutex>
template <typename T> template <typename T>
class Singleton { class Singleton {
public: public:
static T* getInstance(); static T* getInstance();
Singleton(const Singleton& other) = delete;
Singleton<T>& operator=(const Singleton& other) = delete;
private: private:
static std::mutex mutex; Q_DISABLE_COPY_MOVE(Singleton)
static T* instance;
}; };
template <typename T>
std::mutex Singleton<T>::mutex;
template <typename T>
T* Singleton<T>::instance;
template <typename T> template <typename T>
T* Singleton<T>::getInstance() { T* Singleton<T>::getInstance() {
if (instance == nullptr) { static QMutex mutex;
std::lock_guard<std::mutex> locker(mutex); QMutexLocker locker(&mutex);
static T* instance = nullptr;
if (instance == nullptr) { if (instance == nullptr) {
instance = new T(); instance = new T();
} }
}
return instance; return instance;
} }
#define SINGLETONG(Class) \ #define SINGLETON(Class) \
private: \ private: \
friend class Singleton<Class>; \ friend class Singleton<Class>; \
friend struct QScopedPointerDeleter<Class>; \
\ \
public: \ public: \
static Class* getInstance() { \ static Class* getInstance() { \
@ -48,6 +36,6 @@ private: \
private: \ private: \
Class() = default; \ Class() = default; \
Class(const Class& other) = delete; \ Class(const Class& other) = delete; \
Class& operator=(const Class& other) = delete; Q_DISABLE_COPY_MOVE(Class);
#endif // SINGLETON_H #endif // SINGLETON_H