forked from Project-LemonLime/Project_LemonLime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLemonUtils.hpp
More file actions
162 lines (155 loc) · 4.35 KB
/
LemonUtils.hpp
File metadata and controls
162 lines (155 loc) · 4.35 KB
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
/*
* SPDX-FileCopyrightText: 2020-2022 Project LemonLime
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#pragma once
#include <LemonType.hpp>
#include <QDir>
#include <QJsonArray>
#include <QJsonObject>
#include <type_traits>
/*
* Use a lot of SFAINE tricks to support varied data and container types
*/
namespace Lemon::detail {
inline int jsonReadHelper(QString &val, const QJsonValue &jval) {
if (jval.isString()) {
val = jval.toString();
return 0;
} else
return -1;
}
inline int jsonReadHelper(int &val, const QJsonValue &jval) {
if (jval.isDouble()) {
val = jval.toInt();
return 0;
} else
return -1;
}
inline qint64 jsonReadHelper(qint64 &val, const QJsonValue &jval) {
if (jval.isDouble()) {
val = jval.toInteger();
return 0;
} else
return -1;
}
inline int jsonReadHelper(bool &val, const QJsonValue &jval) {
if (jval.isBool()) {
val = jval.toBool();
return 0;
} else
return -1;
}
inline int jsonReadHelper(double &val, const QJsonValue &jval) {
if (jval.isDouble()) {
val = jval.toDouble();
return 0;
} else
return -1;
}
inline int jsonReadHelper(QJsonObject &val, const QJsonValue &jval) {
if (jval.isObject()) {
val = jval.toObject();
return 0;
} else
return -1;
}
inline int jsonReadHelper(QJsonArray &val, const QJsonValue &jval) {
if (jval.isArray()) {
val = jval.toArray();
QJsonArray arr;
return 0;
} else
return -1;
}
inline int jsonReadHelper(ResultState &val, const QJsonValue &jval) {
int x;
if (jsonReadHelper(x, jval) == -1)
return -1;
val = static_cast<ResultState>(x);
return 0;
}
inline int jsonReadHelper(CompileState &val, const QJsonValue &jval) {
int x;
if (jsonReadHelper(x, jval) == -1)
return -1;
val = static_cast<CompileState>(x);
return 0;
}
template <typename T> int jsonReadHelper(QList<T> &val, const QJsonArray &jval);
template <typename T> int jsonReadHelper(QList<T> &val, const QJsonValue &jval) {
QJsonArray arr;
return jsonReadHelper(arr, jval) == -1 || jsonReadHelper(val, arr) == -1 ? -1 : 0;
}
inline int jsonReadHelper(QStringList &val, const QJsonArray &jval) {
QList<QString> s;
if (jsonReadHelper<QString>(s, jval) == -1)
return -1;
val = s;
return 0;
}
template <typename T> int jsonReadHelper(QList<T> &val, const QJsonArray &jval) {
val.clear();
for (auto i : jval) {
T x;
if (jsonReadHelper(x, i) == -1)
return -1;
val.append(x);
}
return 0;
}
template <typename T> int jsonReadHelper(T &val, const QString &name, const QJsonObject &json) {
if (json.contains(name))
return jsonReadHelper(val, json[name]);
else
return -1;
}
template <typename T>
std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T> ||
std::is_same_v<std::remove_cv_t<T>, QString> ||
std::is_same_v<std::remove_cv_t<T>, QJsonArray> ||
std::is_same_v<std::remove_cv_t<T>, QJsonValue> ||
std::is_same_v<std::remove_cv_t<T>, QJsonObject>,
void>
jsonWriteHelper(const T &val, QJsonValue &jval) {
jval = val;
}
template <typename T>
std::enable_if_t<std::is_enum_v<T>, void> jsonWriteHelper(const T &val, QJsonValue &jval) {
jval = static_cast<int>(val);
}
template <typename T> void jsonWriteHelper(const QList<T> &val, QJsonValue &jval) {
QJsonArray arr;
for (const T &i : val) {
QJsonValue x;
jsonWriteHelper(i, x);
arr.append(x);
}
jval = arr;
}
inline void jsonWriteHelper(const QStringList &val, QJsonValue &jval) {
QList<QString> s = val;
jsonWriteHelper<QString>(s, jval);
}
template <typename T> void jsonWriteHelper(const T &val, const QString &name, QJsonObject &json) {
QJsonValue jval;
jsonWriteHelper(val, jval);
json[name] = jval;
}
} // namespace Lemon::detail
namespace Lemon {
template <typename T> int readJson(T &x, const QString &name, const QJsonObject &json) {
return detail::jsonReadHelper(x, name, json);
}
template <typename T> void writeJson(const T &x, const QString &name, QJsonObject &json) {
detail::jsonWriteHelper(x, name, json);
}
} // namespace Lemon
#define READ_JSON(json, x) Lemon::readJson(x, #x, json)
#define WRITE_JSON(json, x) Lemon::writeJson(x, #x, json)
namespace Lemon::common {
QStringList GetFileList(const QDir &dir);
bool FileExistsIn(const QDir &dir, const QString &fileName);
} // namespace Lemon::common