-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.cpp
292 lines (269 loc) · 8.28 KB
/
profile.cpp
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
#include "profile.hpp"
#include "yaml.hpp"
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <json.hpp>
static void parse_path(const ryml::NodeRef &n, step::ref_t &path) {
path.clear();
auto cs = n.val();
auto v = 0z;
auto prev = 0zu;
auto save = [&](size_t i) {
if (prev == i) {
path.emplace_back();
} else if (v >= 0z) {
path.emplace_back(static_cast<size_t>(v));
} else {
std::string str;
str.resize(i - prev);
std::memcpy(str.data(), &cs.str[prev], i - prev);
path.emplace_back(std::move(str));
}
};
for (auto i = 0zu; i < cs.len; i++) {
auto ch = cs.str[i];
if (ch >= '0' && ch <= '9') {
v *= 10, v += ch - '0';
} else if (ch == '.') {
save(i);
v = 0z, prev = i + 1;
} else {
v = -1z;
}
}
save(cs.len);
}
bool c4::yml::read(const ryml::NodeRef &n0, std::unique_ptr<step::step> *obj) {
auto &n = n0.has_val_tag() ? n0 : n0[0];
auto &&o = n.val_tag();
#define T(ty) \
do { \
if (o == "!" #ty) { \
auto step = new step::ty{}; \
if (!n0.has_val_tag()) \
step->id = n0[0].key(); \
else \
step->id = ""; \
if (n.is_map() && n.has_child("name")) \
n["name"] >> step->name; \
else \
step->name = "\b"; \
step->status = step::step::QUEUED; \
c4::yml::read(n, step); \
*obj = std::unique_ptr<step::step>{ step }; \
return true; \
} \
} while (false)
T(step_group);
T(confirm);
T(user_input);
T(delay);
T(send);
T(recv);
T(recv_str);
T(math);
#undef T
throw std::runtime_error{ "Unknown type " + o };
}
bool c4::yml::read(const NodeRef &n, step::steps_t *obj) {
return c4::yml::read(n, static_cast<std::vector<std::unique_ptr<step::step>> *>(obj));
}
bool c4::yml::read(const ryml::NodeRef &n, step::step_group *obj) {
if (n.has_child("digest"))
parse_path(n["digest"], obj->digest);
else
obj->digest.clear();
if (n.has_child("vertical"))
n["vertical"] >> obj->vertical;
else
obj->vertical = true;
n["steps"] >> obj->steps;
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::confirm *obj) {
if (!n.is_map()) {
obj->prompt = n.val();
return true;
}
if (n.has_child("prompt"))
n["prompt"] >> obj->prompt;
else
obj->prompt = "<Y/N?>";
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::user_input *obj) {
if (n.has_child("prompt"))
n["prompt"] >> obj->prompt;
else
obj->prompt = "<Y/N?>";
if (n.has_child("unit"))
n["unit"] >> obj->unit;
else
obj->unit = "";
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::delay *obj) {
if (n.has_child("seconds"))
n["seconds"] >> obj->seconds;
else
obj->seconds = 0;
if (n.has_child("nanoseconds"))
n["nanoseconds"] >> obj->nanoseconds;
else
obj->nanoseconds = 0;
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::send *obj) {
n["ch"] >> obj->channel;
n["cmd"] >> obj->cmd;
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::recv *obj) {
n["ch"] >> obj->channel;
if (n.has_child("unit"))
n["unit"] >> obj->unit;
else
obj->unit = "";
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::recv_str *obj) {
n["ch"] >> obj->channel;
return true;
}
bool c4::yml::read(const ryml::NodeRef &n, step::math::operand *obj) {
if (!n.is_quoted()) {
obj->kind = step::math::operand::DOUBLE;
n >> obj->value;
} else {
obj->kind = step::math::operand::REFERENCE;
parse_path(n, obj->index);
}
return true;
}
double step::math::operand::operator()(steps_t &steps) {
switch (kind) {
case DOUBLE:
return value;
case REFERENCE:
auto ptr = get(steps, index);
again:
if (auto grp = dynamic_cast<step_group *>(ptr); grp && !grp->digest.empty()) {
ptr = get(grp->steps, grp->digest);
goto again;
}
return dynamic_cast<valued_step *>(ptr)->value;
}
return std::numeric_limits<double>::quiet_NaN();
}
bool c4::yml::read(const ryml::NodeRef &n, step::math *obj) {
if (n.has_child("unit"))
n["unit"] >> obj->unit;
n["operands"] >> obj->operands;
auto &&o = n["op"].val();
if (o == "+") obj->op = step::math::ADD;
else if (o == "-") obj->op = step::math::SUB;
else if (o == "*") obj->op = step::math::MUL;
else if (o == "/") obj->op = step::math::DIV;
else throw std::runtime_error{ "Unknown op " + o };
return true;
}
step::step *step::get(steps_t &steps, const ref_t &cont) {
step *last{};
for (auto ptr = &steps; auto &&ref : cont) {
if (ref.index() == 0) {
ptr = ptr->parent;
continue;
}
if (ref.index() == 1) {
last = ptr->at(std::get<size_t>(ref)).get();
} else {
auto &s = std::get<std::string>(ref);
for (auto &st : *ptr)
if (st->id == s) {
last = st.get();
goto found;
}
throw std::runtime_error{ "Cannot find ref " + s };
}
found:
if (auto grp = dynamic_cast<step_group *>(last))
ptr = &grp->steps;
}
return last;
}
void step::resolve_parent(steps_t &steps) {
for (auto &st : steps)
if (auto grp = dynamic_cast<step_group *>(st.get())) {
grp->steps.parent = &steps;
resolve_parent(grp->steps);
}
}
step::step *profile_t::operator()() {
step::step *last{};
for (auto ptr = &steps; auto id : current) {
last = ptr->at(id).get();
if (auto grp = dynamic_cast<step::step_group *>(last))
ptr = &grp->steps;
}
return last;
}
namespace nlohmann {
template <>
struct adl_serializer<step::steps_t> {
static void from_json(const json &j, step::steps_t &vec);
static void to_json(json &j, const step::steps_t &vec);
};
template <>
struct adl_serializer<std::unique_ptr<step::step>> {
static void from_json(const json &j, std::unique_ptr<step::step> &step) {
step->status = j["status"];
if (auto st = dynamic_cast<step::valued_step *>(step.get()); st)
j["value"].get_to(st->value);
else if (auto st = dynamic_cast<step::recv_str *>(step.get()); st)
j["string"].get_to(st->value);
else if (auto st = dynamic_cast<step::step_group *>(step.get()); st)
j["steps"].get_to(st->steps);
}
static void to_json(json &j, const std::unique_ptr<step::step> &step) {
j["status"] = step->status;
if (auto st = dynamic_cast<step::valued_step *>(step.get()); st)
j["value"] = st->value;
else if (auto st = dynamic_cast<step::recv_str *>(step.get()); st)
j["string"] = st->value;
else if (auto st = dynamic_cast<step::step_group *>(step.get()); st)
j["steps"] = st->steps;
}
};
void adl_serializer<step::steps_t>::from_json(const json &j, step::steps_t &vec) {
for (auto &[k, v] : j.items()) {
for (auto &st : vec)
if (st->id == k) {
v.get_to(st);
break;
}
}
}
void adl_serializer<step::steps_t>::to_json(json &j, const step::steps_t &vec) {
j = {};
for (auto &st : vec)
if (!st->id.empty())
j[st->id] = st;
}
}
std::istream &operator>>(std::istream &is, profile_t &profile) {
nlohmann::json j{};
is >> j;
j["name"].get_to(profile.name);
j["steps"].get_to(profile.steps);
j["current"].get_to(profile.current);
return is;
}
std::ostream &operator<<(std::ostream &os, const profile_t &profile) {
nlohmann::json j{};
j["name"] = profile.name;
j["steps"] = profile.steps;
j["current"] = profile.current;
return os << j.dump(2) << std::endl;
}