-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.cpp
More file actions
427 lines (351 loc) · 13.9 KB
/
elements.cpp
File metadata and controls
427 lines (351 loc) · 13.9 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "elements.hpp"
#include "crtutils.hpp"
#include "err.hpp"
#include <fstream>
// Definitions in rgb
rgb::rgb() noexcept : r(0), g(0), b(0) {}
rgb::rgb(uint8_t _r, uint8_t _g, uint8_t _b) noexcept : r(_r), g(_g), b(_b) {}
void rgb::fromJson(const json &j) {
if (!j.is_array())
throw CrtExcept(
0x0008, tr("from rgb::fromJson(); the RGB color in the json object isn't an array"));
if (j.size() != 3)
throw CrtExcept(0x0008, tr("from rgb::fromJson(); incorrect number of color channels"));
r = j[0].get<uint8_t>();
g = j[1].get<uint8_t>();
b = j[2].get<uint8_t>();
}
json rgb::toJson() const { return json{r, g, b}; }
// Definitions in Ucc
Ucc::Ucc(const char32_t _c) noexcept : c(_c), hasB(false), hasF(false) {}
Ucc::Ucc(const char32_t _c, const rgb _b, const rgb _f) noexcept
: c(_c), hasB(true), b(_b), hasF(true), f(_f) {}
Ucc::Ucc(const char32_t _c, Mode cm, const rgb color) noexcept : c(_c) {
switch (cm) {
case Mode::b: hasB = true; b = color;
case Mode::f: hasF = true; f = color;
}
}
Ucc::Ucc(const is_json auto &j) { fromJson(j); }
void Ucc::fromJson(const json &j) {
if (!j.is_object())
throw CrtExcept(
0x0006,
tr("from Ucc::fromJson(); the unicode colored character's json isn't an object"));
QString _c = j.at("c").get<QString>();
if (_c.size() > sizeof(char32_t))
throw CrtExcept(
0x0003,
tr("from Ucc::fromJson(); the string to parse is {} and it has too many characters"),
_c);
c = _c.toStdU32String()[0];
if (j.find("b") != j.end()) {
hasB = true;
b.fromJson(j["b"]);
}
if (j.find("f") != j.end()) {
hasF = true;
f.fromJson(j["f"]);
}
}
json Ucc::toJson() const {
json j;
j["c"] = char32ToQString(c);
if (hasB)
j["b"] = b.toJson();
if (hasF)
j["f"] = f.toJson();
return j;
}
QString Ucc::operator()() const { return char32ToQString(c); }
// Definitions in BasicCrtClass
const QString &BasicCrtClass::getID() const noexcept { return id; }
void BasicCrtClass::setID(const QString &_id) {
if (isInvalidID(_id))
throw CrtExcept(0x0002, tr("from BasicCrtClass::setID()"));
else if (_id.isEmpty())
throw CrtExcept(0x0001, tr("from BasicCrtClass::setID()"));
else
id = _id;
}
const QString &BasicCrtClass::getName() const noexcept { return name; }
void BasicCrtClass::setName(const QString &_name) noexcept { name = _name; }
bool BasicCrtClass::operator==(const BasicCrtClass &other) const noexcept {
return id == other.getID();
}
// Definitions in Block
Block::Block(const Ucc &_blk) noexcept { setBlk(_blk); }
Block::Block(const Ucc &_blk, const QString &_id, const QString &_name) {
setBlk(_blk);
setID(_id);
setName(_name);
}
Block::Block(const is_json auto &j) { fromJson(j); }
const Ucc &Block::getBlk() const noexcept { return blk; }
void Block::setBlk(const Ucc &_blk) noexcept { blk = _blk; }
void Block::fromJson(const json &j) {
if (!j.is_object())
throw CrtExcept(0x0006, tr("from Block::fromJson(); the block's json isn't an object"));
setBlk(Ucc{j.at("blk")});
if (j.find("id") != j.end())
setID(j.at("id").get<QString>());
else
throw CrtExcept(0x0001, tr("from Block::fromJson()"));
setName(j.value("name", id));
}
json Block::toJson() const {
json j{{"blk", getBlk().toJson()}, {"id", getID()}, {"name", getName()}};
return j;
}
// Definitions in LBlock
LBlock::LBlock(const size_t _w, const size_t _h) {
lblk.resize(_h);
w.resize(_h);
for (auto &i : lblk) i.resize(_w);
}
LBlock::LBlock(const size_t _w, const size_t _h, const QString &_id, const QString &_name) {
lblk.resize(_h);
w.resize(_h);
for (auto &i : lblk) i.resize(_w);
setID(_id);
setName(_name);
}
LBlock::LBlock(const UccL2 &_lblk, const QString &_id, const QString &_name) {
setLblk(_lblk);
setID(_id);
setName(_name);
}
LBlock::LBlock(const is_json auto &j) { fromJson(j); }
const UccL2 &LBlock::getLblk() const noexcept { return lblk; }
void LBlock::setLblk(const UccL2 &_lblk) {
lblk = _lblk;
w.resize(lblk.size());
for (size_t i{}; i < w.size(); i++) w[i] = lblk[i].size();
}
const Ucc &LBlock::getPos(const size_t r, const size_t c) const {
if (r >= lblk.size() || c >= (lblk.empty() ? 0 : lblk[r].size()))
throw CrtExcept(
0x0005,
tr("from LBlock::getPos(); the required position is ({},{}), but it's out of range"),
r,
c);
return lblk[r][c];
}
void LBlock::setPos(const size_t r, const size_t c, const Ucc &blk) {
if (r >= lblk.size() || c >= (lblk.empty() ? 0 : lblk[r].size()))
throw CrtExcept(
0x0005,
tr("from LBlock::setPos(); the required position is ({},{}), but it's out of range"),
r,
c);
lblk[r][c] = blk;
}
size_t LBlock::getW(const size_t r) const {
if (r < w.size())
return w[r];
else if (r < lblk.size())
return lblk[r].size();
else
throw CrtExcept(0x0005,
tr("from LBlock::getW(); the required row number is {}, but there's only "
"%n row(s) in this large-block",
nullptr,
lblk.size()),
r,
lblk.size());
}
auto LBlock::getFullW() const noexcept -> const QList<size_t> & { return w; }
void LBlock::setW(const size_t r, const size_t _w) {
if (r < w.size())
w[r] = _w;
else
throw CrtExcept(0x0005, tr("from LBlock::setW()"));
}
void LBlock::fromJson(const json &j) {
if (!j.is_object())
throw CrtExcept(0x0006,
tr("from LBlock::fromJson(); the large-block's json isn't an object"));
if (!j.at("lblk").is_array())
throw CrtExcept(0x0006, tr("from LBlock::fromJson(); the \"lblk\" isn't an array"));
lblk.resize(j.at("lblk").size());
w.resize(j.at("lblk").size());
for (size_t r{}; r < j.at("lblk").size(); r++) {
if (!j.at("lblk")[r].is_array())
throw CrtExcept(0x0006, tr("from LBlock::fromJson(); Row {} isn't an array"), r + 1);
for (size_t c{}; c < j.at("lblk")[0].size(); c++) {
lblk[r].resize(j.at("lblk")[r].size());
setPos(r, c, Ucc{j.at("lblk")[r][c]});
}
}
if (!j.at("w").is_array())
throw CrtExcept(0x0006, tr("from LBlock::fromJson(); the \"w\" isn't an array"));
for (size_t i{}; i < j.at("w").size(); i++) w[i] = j.at("w")[i].get<size_t>();
if (j.find("id") != j.end())
setID(j.at("id").get<QString>());
else
throw CrtExcept(0x0001, tr("from LBlock::fromJson()"));
setName(j.value("name", id));
}
json LBlock::toJson() const {
std::vector<std::vector<json>> lblk_json;
lblk_json.resize(lblk.size());
for (size_t r{}; r < lblk.size(); r++) {
lblk_json[r].resize(lblk.empty() ? 0 : lblk[r].size());
for (size_t c{}; c < (lblk.empty() ? 0 : lblk[r].size()); c++) {
lblk_json[r][c] = lblk[r][c].toJson();
}
}
return json{{"lblk", lblk_json}, {"w", getFullW()}, {"id", getID()}, {"name", getName()}};
}
// Definitions in BasicProduct
const QString &BasicProduct::getAuthor() const noexcept { return author; }
void BasicProduct::setAuthor(const QString &_author) {
if (isInvalidEmail(_author))
throw CrtExcept(
0x0007,
tr("from BasicProduct::setAuthor(); the string is \"{}\" and it isn't a valid "
"email address"),
_author);
else
author = _author;
}
const QString &BasicProduct::getDes() const noexcept { return des; }
void BasicProduct::setDes(const QString &_des) noexcept { des = _des; }
uint32_t BasicProduct::getPrice() const noexcept { return price; }
void BasicProduct::setPrice(uint32_t _price) noexcept { price = _price; }
// Definitions in Kit
Kit::Kit(const QString &path) { fromFile(path); }
const BlockL &Kit::getBlks() const noexcept { return blks; }
void Kit::clearBlks() noexcept { blks.clear(); }
const LBlockL &Kit::getLblks() const noexcept { return lblks; }
void Kit::clearLblks() noexcept { lblks.clear(); }
void Kit::operator+=(const Block &blk) { blks.emplaceBack(blk); }
void Kit::operator+=(const LBlock &lblk) { lblks.emplaceBack(lblk); }
void Kit::operator-=(const QString &_id) {
auto erased_blks = erase_if(blks, [&_id](const Block &blk) { return blk.getID() == _id; });
auto erased_lblks = erase_if(lblks, [&_id](const LBlock &lblk) { return lblk.getID() == _id; });
if (erased_blks == 0 && erased_lblks == 0)
throw CrtExcept(
0x0004,
tr("from Kit::operator-=; no one's id is \"{}\" in both blocks and large-blocks"),
_id);
}
auto Kit::operator[](const QString &_id) -> const std::variant<Block, LBlock> {
for (const auto &i : blks)
if (i.getID() == _id)
return i;
for (const auto &i : lblks)
if (i.getID() == _id)
return i;
throw CrtExcept(
0x0004,
tr("from Kit::operator[]; no one's id is \"{}\" in both blocks and large-blocks"),
_id);
}
void Kit::fromJson(const json &j) {
if (!j.is_object())
throw CrtExcept(0x0006, tr("from Kit::fromJson(); the kit's json isn't an object"));
if (j.find("author") != j.end())
setID(j.at("author").get<QString>());
else
throw CrtExcept(0x0009, tr("from Kit::fromJson()"));
if (j.find("id") != j.end())
setID(j.at("id").get<QString>());
else
throw CrtExcept(0x0001, tr("from Kit::fromJson()"));
setName(j.value("name", id));
setDes(j.value("des", QString{tr("Empty")}));
if (j.find("blks") != j.end())
for (const json &blk : j["blks"]) *this += Block{blk};
if (j.find("lblks") != j.end())
for (const json &lblk : j["lblks"]) *this += LBlock{lblk};
}
json Kit::toJson() const {
json j{{"author", getAuthor()}, {"id", getID()}, {"name", getName()}, {"des", getDes()}};
if (!blks.empty()) {
std::vector<json> blks_json;
blks_json.reserve(blks.size());
for (const auto &i : blks) blks_json.emplace_back(i.toJson());
j["blks"] = blks_json;
}
if (!lblks.empty()) {
std::vector<json> lblks_json;
lblks_json.reserve(lblks.size());
for (const auto &i : lblks) lblks_json.emplace_back(i.toJson());
j["lblks"] = lblks_json;
}
return j;
}
void Kit::fromFile(const QString &path) {
std::ifstream ifs(path.toStdString());
if (!ifs)
throw CrtExcept(tr("from Kit::fromFile(); failed to open the kit file at {} ({})"),
path,
strerror(errno));
json j;
ifs >> j;
fromJson(j);
}
void Kit::toFile(const QString &path, const uint8_t tabsize) {
std::ofstream ofs(path.toStdString());
ofs << std::setw(tabsize) << toJson() << std::endl;
}
// Definitions in Map
Map::Map(const QString &path) { fromFile(path); }
const MapDataType &Map::getData() const noexcept { return data; }
void Map::setData(const MapDataType &_data) noexcept { data = _data; }
const Ucc Map::operator[](const size_t r, const size_t c) {
if (r > data.size() || c > data.empty() ? 0 : data[r].size())
throw CrtExcept(
0x0009,
tr("from Map::operator[]; the required position is ({},{}), but it's out of range"),
r,
c);
if (data[r][c].index()) { // is QSharedPointer<LBlock>
if (std::get<1>(data[r][c]) != nullptr) // is at the upper left corner
return std::get<1>(data[r][c])->getPos(0, 0); // getPos directly
else {
for (size_t _r{}; _r < r; _r++)
for (size_t _c{}; _c < c; _c++)
if (data[_r][_c].index() && // (_r, _c) is QSharedPointer<LBlock>
std::get<1>(data[_r][_c]) !=
nullptr && // (_r, _c) is at the upper left corner
std::get<1>(data[_r][_c])->getLblk().size() + _r > r &&
std::get<1>(data[_r][_c])->getW(r - _r) + _c >
c) // (r,c) is filled by (_r, _c)
return std::get<1>(data[_r][_c])
->getPos(r - _r, c - _c); // getPos with the relative position
return Ucc{}; // (r, c) is null but QSharedPointer<LBlock> (UNEXCEPTED)
}
} else
return std::get<0>(data[r][c]) == nullptr ? Ucc{} : std::get<0>(data[r][c])->getBlk();
}
template <> auto Map::get<0>(const size_t r, const size_t c) {
if (r > data.size() || c > data.empty() ? 0 : data[r].size())
throw CrtExcept(
0x0009,
tr("from Map::get<0>(); the required position is ({},{}), but it's out of range"),
r,
c);
return std::get<0>(data[r][c]) == nullptr ? Block{} : *std::get<0>(data[r][c]);
}
template <> auto Map::get<1>(const size_t r, const size_t c) {
if (r > data.size() || c > data.empty() ? 0 : data[r].size())
throw CrtExcept(
0x0009,
tr("from Map::get<1>(); the required position is ({},{}), but it's out of range"),
r,
c);
if (std::get<1>(data[r][c]) != nullptr) // is at the upper left corner
return *std::get<1>(data[r][c]);
else {
for (size_t _r{}; _r < r; _r++)
for (size_t _c{}; _c < c; _c++)
if (data[_r][_c].index() && // (_r, _c) is QSharedPointer<LBlock>
std::get<1>(data[_r][_c]) != nullptr && // (_r, _c) is at the upper left corner
std::get<1>(data[_r][_c])->getLblk().size() + _r > r &&
std::get<1>(data[_r][_c])->getW(r - _r) + _c > c) // (r,c) is filled by (_r, _c)
return *std::get<1>(data[_r][_c]);
return LBlock{}; // (r, c) is null but QSharedPointer<LBlock> (UNEXCEPTED)
}
}