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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
#ifndef CONFIGBASE
#define CONFIGBASE
#include <QString>
#include <QStringList>
#include <QList>
#include <QMap>
#include <QRegularExpression>
#include <exception>
#include <type_traits>
#include <limits>
#include "Util.hpp"
using ConfigException = std::logic_error;
namespace ConfigValueType {
enum Type {
None,
String,
Integer,
Boolean,
List,
Map
};
}
class ConfigValueBase {
public:
virtual void setValue(const QString&) { }
virtual QString value() { return ""; }
virtual ConfigValueType::Type type() { return ConfigValueType::Type::None; }
virtual ~ConfigValueBase() = default;
template<typename Type>
static Type getValue(const QString& value);
template<typename Type>
static QString getStringValue(Type val);
};
using ConfigValues = QMap<QString, ConfigValueBase*>;
template<typename Type, typename = void>
class ConfigValue : public ConfigValueBase {
public:
virtual void setValue(const QString&) { throw ConfigException("Cannot assign"); }
virtual QString value() { throw ConfigException("Cannot get the value"); }
};
using String = QString;
using Integer = int;
using Float = double;
using Port = ushort;
using Boolean = bool;
#define List(Type) QList<Type>
#define Map(KeyType, ValueType) QMap<KeyType, ValueType>
#define ConfigVal(Value) {#Value, mkConfigValue(Value)}
#define ConfigValName(Name, Value) {#Name, mkConfigValue(Value)}
#define ConfigValEx(Value, ...) {#Value, mkConfigValue(Value, __VA_ARGS__)}
template<>
class ConfigValue<String> : public ConfigValueBase {
String& config;
bool trimmedQuotes = false;
public:
ConfigValue(String& config) : config(config) { }
virtual void setValue(const QString& value) { config = Util::trimQuotes(value, trimmedQuotes); }
virtual QString value()
{
static QRegularExpression quoteExp(R"(^(\s.*|.*\s)$)");
if(trimmedQuotes || quoteExp.match(config).hasMatch())
{
return "\"" + config + "\"";
}
else
{
return config;
}
}
virtual ConfigValueType::Type type() { return ConfigValueType::Type::String; }
};
template<typename Type>
class ConfigValue<Type, typename std::enable_if<std::is_integral<Type>::value>::type> : public ConfigValueBase {
Type& config;
public:
ConfigValue(Type& config) : config(config) { }
virtual void setValue(const QString& value)
{
bool ok = true;
long long longLongVal = value.toLongLong(&ok, 0);
if(!ok) throw ConfigException("\"" + value.toStdString() + "\" could not be parsed as an Integer");
if(longLongVal < std::numeric_limits<Type>::min() || longLongVal > std::numeric_limits<Type>::max()) throw ConfigException((QString::number(longLongVal) + " is out of range (" + QString::number(std::numeric_limits<Type>::min()) + " - " + QString::number(std::numeric_limits<Type>::max()) + ")").toStdString());
config = static_cast<Type>(longLongVal);
}
virtual QString value() { return QString::number(config); }
virtual ConfigValueType::Type type() { return ConfigValueType::Type::Integer; }
};
template<>
class ConfigValue<Float> : public ConfigValueBase {
Float& config;
public:
ConfigValue(Float& config) : config(config) { }
virtual void setValue(const QString& value)
{
bool ok = true;
Float temp = value.toDouble(&ok);
if(!ok) throw ConfigException("\"" + value.toStdString() + "\" could not be parsed as a Float");
config = temp;
}
virtual QString value() { return QString::number(config, 'g', 10); }
};
template<>
class ConfigValue<Boolean> : public ConfigValueBase {
Boolean& config;
public:
ConfigValue(Boolean& config) : config(config) { }
virtual void setValue(const QString& value)
{
if(value == "true")
{
config = true;
}
else if(value == "false")
{
config = false;
}
else
{
throw ConfigException("\"" + value.toStdString() + "\" could not be parsed as a Boolean");
}
}
virtual QString value() { return config ? "true" : "false"; }
virtual ConfigValueType::Type type() { return ConfigValueType::Type::Boolean; }
};
class ConfigValueList : public ConfigValueBase {
public:
virtual void append(const QString& entry) = 0;
virtual void remove(const QString& entry) = 0;
virtual QStringList getValues() = 0;
};
class ConfigValueMap : public ConfigValueBase {
public:
virtual void setKeyValue(const QString& key, const QString& value) = 0;
virtual void remove(const QString& key) = 0;
virtual QMap<QString, QString> getValues() = 0;
};
template<typename Type>
class ConfigValue<QList<Type>> : public ConfigValueList {
using ListType = QList<Type>;
ListType& config;
const bool deduplicate;
const QChar splitChar;
public:
ConfigValue(ListType& config, bool deduplicate = true, const QChar splitChar = ';') : config(config), deduplicate(deduplicate), splitChar(splitChar) {}
virtual ListType& list() { return config; }
virtual ConfigValueType::Type type() { return ConfigValueType::Type::List; }
virtual void append(const QString &entry)
{
Type val = ConfigValueBase::getValue<Type>(entry);
if(!deduplicate || !config.contains(val))
{
config.append(val);
}
}
virtual void remove(const QString &entry) { config.removeAll(ConfigValueBase::getValue<Type>(entry)); }
virtual QStringList getValues()
{
QStringList ret;
foreach(Type val, config)
{
ret.append(ConfigValueBase::getStringValue(val));
}
return ret;
}
void setValue(const QString& value)
{
config.clear();
if(value.isEmpty())
{
return;
}
QStringList parts = Util::splitEscaped(value, splitChar);
foreach(const QString& part, parts)
{
config.append(ConfigValueBase::getValue<Type>(part));
}
}
QString value()
{
QStringList parts;
foreach(const Type& part, config)
{
parts.append(ConfigValueBase::getStringValue(part));
}
return Util::joinEscape(parts, splitChar);
}
};
template<typename KeyType, typename ValueType>
class ConfigValue<QMap<KeyType, ValueType>> : public ConfigValueMap {
using MapType = QMap<KeyType, ValueType>;
MapType& config;
const QChar assignChar;
const QChar splitChar;
public:
ConfigValue(MapType& config, const QChar assignChar = ':', const QChar splitChar = ';') : config(config), assignChar(assignChar), splitChar(splitChar) {}
virtual MapType& map() { return config; }
virtual ConfigValueType::Type type() { return ConfigValueType::Type::Map; }
virtual void setKeyValue(const QString& key, const QString& value)
{
config[ConfigValueBase::getValue<KeyType>(key)] = ConfigValueBase::getValue<ValueType>(value);
}
virtual QMap<QString, QString> getValues()
{
QMap<QString, QString> ret;
foreach(KeyType key, config.keys())
{
ret[ConfigValueBase::getStringValue(key)] = ConfigValueBase::getStringValue(config[key]);
}
return ret;
}
virtual void remove(const QString &key)
{
config.remove(ConfigValueBase::getValue<KeyType>(key));
}
QString value()
{
QStringList ret;
foreach(const KeyType& key, config.keys())
{
ret.append(Util::joinEscape({ConfigValueBase::getStringValue(key), ConfigValueBase::getStringValue(config.value(key))}, assignChar));
}
return Util::joinEscape(ret, splitChar);
}
void setValue(const QString& value)
{
config.clear();
if(value.isEmpty())
{
return;
}
QStringList parts = Util::splitEscaped(value, splitChar);
foreach(const QString& part, parts)
{
QStringList parts = Util::splitEscaped(part, assignChar);
if(parts.length() != 2)
{
throw ConfigException("Cannot parse corrupt map key-value-pair");
}
config[ConfigValueBase::getValue<KeyType>(parts.first())] = ConfigValueBase::getValue<ValueType>(parts.at(1));
}
}
};
template<typename Type>
class ConfigValue<Type, typename std::enable_if<std::is_enum<Type>::value>::type> : public ConfigValue<typename std::underlying_type<Type>::type> {
using UndType = typename std::underlying_type<Type>::type;
public:
ConfigValue(Type& config) : ConfigValue<UndType>::ConfigValue((UndType&)config) {}
};
template<typename Type, typename... Types>
ConfigValue<Type>* mkConfigValue(Type& Value, Types... values)
{
return new ConfigValue<Type>(Value, values...);
}
template<typename Type>
Type ConfigValueBase::getValue(const QString& value)
{
Type ret;
ConfigValue<Type> val(ret);
val.setValue(value);
return ret;
}
template<typename Type>
QString ConfigValueBase::getStringValue(Type value)
{
ConfigValue<Type> val(value);
return val.value();
}
class ConfigBase
{
public:
explicit ConfigBase(QMap<QString, ConfigValueBase*> configValues) : configValues(configValues) {}
void setConfigValue(const QString& name, const QString& value);
QString getConfigValueLine(const QString& name);
void addConfigListEntry(const QString& name, const QString& value);
void removeConfigMapListEntry(const QString &name, const QString &value);
void setConfigMapValue(const QString& name, const QString& key, const QString& value);
QString read(const QString& fileName, bool writeDefault = true);
bool write(QString fileName = "");
void setConfigLine(const QString& line);
protected:
void addConfigValue(QString name, ConfigValueBase *value);
ConfigValueBase& getConfigValue(const QString &name);
QMap<QString, ConfigValueBase*> configValues;
private:
QString curFileName = "";
};
template<typename ConfigClass>
class ClearableConfigBase : public ConfigBase {
public:
explicit ClearableConfigBase(QMap<QString, ConfigValueBase*> configValues) : ConfigBase(configValues) {}
void clear()
{
auto configVals = configValues;
*(static_cast<ConfigClass*>(this)) = ConfigClass();
configValues = configVals;
}
};
#endif // CONFIGBASE
|