mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2024-11-26 05:17:05 +08:00
Use more elegant singleton and fix spell bug.
This commit is contained in:
parent
bb19554215
commit
8778ca85a3
@ -13,7 +13,7 @@ class AppInfo : public QObject
|
||||
private:
|
||||
explicit AppInfo(QObject *parent = nullptr);
|
||||
public:
|
||||
SINGLETONG(AppInfo)
|
||||
SINGLETON(AppInfo)
|
||||
};
|
||||
|
||||
#endif // APPINFO_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);}
|
||||
|
@ -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>; \
|
||||
|
@ -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);
|
||||
|
@ -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();}
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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();}
|
||||
};
|
||||
|
||||
|
@ -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();}
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user