-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigStore.hpp
More file actions
53 lines (34 loc) · 981 Bytes
/
Copy pathConfigStore.hpp
File metadata and controls
53 lines (34 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef CONFIGSTORE_HPP
#define CONFIGSTORE_HPP
#include <QHash>
#include <QJsonObject>
#include <QMutex>
#include <QString>
class ConfigStore {
public:
static ConfigStore &get();
// 禁止拷贝和移动
ConfigStore(const ConfigStore &) = delete;
ConfigStore &operator=(const ConfigStore &) = delete;
// ====== 单个键值操作 ======
void save(const QString &key, const QJsonObject &value);
QJsonObject load(const QString &key) const;
void remove(const QString &key);
bool contains(const QString &key) const;
// ====== 批量操作 ======
QHash<QString, QJsonObject> loadAll() const;
QStringList keys() const;
void clear();
/// 在 Application 析构前主动刷新数据到磁盘
void flush();
private:
ConfigStore();
~ConfigStore();
QString filePath() const;
void readFromFile();
void writeToFile();
QHash<QString, QJsonObject> _data;
mutable QMutex _mutex;
bool _flushed = false;
};
#endif // CONFIGSTORE_HPP