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:
explicit AppInfo(QObject *parent = nullptr);
public:
SINGLETONG(AppInfo)
SINGLETON(AppInfo)
};
#endif // APPINFO_H

View File

@ -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);}

View File

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

View File

@ -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);

View File

@ -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();}
};

View File

@ -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);

View File

@ -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);

View File

@ -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();}
};

View File

@ -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();}

View File

@ -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();

View File

@ -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);

View File

@ -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);

View File

@ -2,42 +2,30 @@
#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;
Q_DISABLE_COPY_MOVE(Singleton)
};
template <typename T>
std::mutex Singleton<T>::mutex;
template <typename T>
T* Singleton<T>::instance;
template <typename T>
T* Singleton<T>::getInstance() {
static QMutex mutex;
QMutexLocker locker(&mutex);
static T* instance = nullptr;
if (instance == nullptr) {
std::lock_guard<std::mutex> locker(mutex);
if (instance == nullptr) {
instance = new T();
}
instance = new T();
}
return instance;
}
#define SINGLETONG(Class) \
#define SINGLETON(Class) \
private: \
friend class Singleton<Class>; \
friend struct QScopedPointerDeleter<Class>; \
\
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