-
Notifications
You must be signed in to change notification settings - Fork 15
/
lqtutils_settings.h
136 lines (121 loc) · 6.33 KB
/
lqtutils_settings.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* MIT License
*
* Copyright (c) 2020 Luca Carlon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
**/
#ifndef LSETTINGS_H
#define LSETTINGS_H
#include <QObject>
#include <QSettings>
#include <QMutex>
#include <QString>
#include <QHash>
// The EXPAND macro here is only needed for MSVC:
// https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly
#define EXPAND( x ) x
#define L_SETTINGS_GET_MACRO(_1, _2, _3, NAME,...) NAME
#define L_DECLARE_SETTINGS(...) \
EXPAND(L_SETTINGS_GET_MACRO(__VA_ARGS__, L_DECLARE_SETTINGS3, L_DECLARE_SETTINGS2, L_DECLARE_SETTINGS1)(__VA_ARGS__))
// Defines a single value inside the settings class.
#define L_DEFINE_VALUE(type, name, def) \
public: \
type name(bool create = false) const { \
const QString key = m_section + QStringLiteral(#name); \
if (create && !m_settings->contains(key)) \
m_settings->setValue(key, def); \
return m_settings->value(key, def).value<type>(); \
} \
public Q_SLOTS: \
void set_##name(type value) { \
if (name() == value) return; \
m_settings->setValue(m_section + QStringLiteral(#name), QVariant::fromValue(value)); \
if (this != ¬ifier()) emit name##Changed(value); \
emit notifier().name##Changed(value); \
} \
Q_SIGNALS: \
void name##Changed(type name); \
private: \
Q_PROPERTY(type name READ name WRITE set_##name NOTIFY name##Changed)
// Declares the settings class.
#define L_DECLARE_SETTINGS2(classname, qsettings) \
L_DECLARE_SETTINGS3(classname, qsettings, "")
#define L_DECLARE_SETTINGS3(classname, qsettings, section) \
class classname : public QObject \
{ \
private: \
Q_OBJECT \
public: \
static classname& notifier() { \
static classname _notifier; \
return _notifier; \
} \
public: \
classname(QObject* parent = nullptr) : QObject(parent) { \
m_settings = qsettings; \
m_section = QStringLiteral(section).isEmpty() ? "" : QString("%1/").arg(section); \
} \
~classname() { delete m_settings; } \
protected: \
QSettings* m_settings; \
QString m_section;
namespace lqt {
template<typename T>
class CacheValue
{
public:
T value(const QString& key, std::function<T()> init);
void reset(const QString& key);
void setValue(const QString& key, const T& v);
bool isSet(const QString& key);
private:
QHash<QString, T> m_cache;
QMutex m_mutex;
};
template<typename T>
T CacheValue<T>::value(const QString& key, std::function<T()> init)
{
QMutexLocker locker(&m_mutex);
if (m_cache.contains(key))
return m_cache.value(key);
T v = init();
m_cache.insert(key, v);
return v;
}
template<typename T>
void CacheValue<T>::reset(const QString& key)
{
QMutexLocker locker(&m_mutex);
m_cache.remove(key);
}
template<typename T>
void CacheValue<T>::setValue(const QString& key, const T& v)
{
QMutexLocker locker(&m_mutex);
m_cache.insert(key, v);
}
template<typename T>
bool CacheValue<T>::isSet(const QString& key)
{
QMutexLocker locker(&m_mutex);
return m_cache.contains(key);
}
} // namespace
#endif