forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_resource.hpp
359 lines (302 loc) · 10.4 KB
/
json_resource.hpp
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
/*##################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
##################################################################################################*/
#pragma once
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <map>
#include "nlohmann/json.hpp"
#include <tcb/span.hpp>
#include <bpstd/string_view.hpp>
#include "input_guard.hpp"
struct JSONResource {
private:
std::set<std::string> vars;
const nlohmann::json *json;
std::stack<const nlohmann::json*> json_parent;
std::unique_ptr<InputGuard> input_guard_ptr;
void warn(const std::exception &exception) {
std::cerr << "WARN: " << exception.what() << std::endl;
// assert(false);
}
protected:
size_t index = 0, named_array_read_count = 0;
JSONResource() {}
JSONResource(const nlohmann::json &json) {
this->set_current_json_ptr(&json);
for (auto &entry : this->json->items()) {
this->vars.insert(entry.key());
}
input_guard_ptr = std::make_unique<InputGuard>(json);
};
void set_current_json_ptr(const nlohmann::json *ptr) {
this->json = ptr;
}
auto dump() const {
return this->json->dump();
}
template <class T>
std::string next_dict_key(T last_key, const T key_cond) const noexcept {
std::string key = "", prev_key = "";
if (last_key == key_cond)
last_key = "";
for (const auto& item : this->json->items()) {
if (this->json->is_array()) {
for (auto &entry : item.value().items()) {
if (prev_key == last_key) {
key = entry.key();
break;
}
else
prev_key = entry.key();
}
} else {
key = item.key();
}
}
input_guard_ptr->update_dict_key(key);
return key;
}
public:
virtual ~JSONResource() {}
auto n_named_array_read_count() noexcept {
return this->named_array_read_count;
}
void zoom_in(const bpstd::string_view &sub) noexcept {
auto it = this->json->is_array()
? this->json->at(this->json->size()-1).find(sub)
: this->json->find(sub);
assert(*it != NULL);
this->json_parent.push(this->json);
if (sub == "dist") // TODO #112: do better than hardcoding "dist"...
this->set_current_json_ptr(&(it->at(this->n_elements(sub) - this->index--)));
else
this->set_current_json_ptr(&(*it));
this->named_array_read_count = 0;
}
void zoom_out() noexcept {
assert(this->json_parent.size() != 0);
this->set_current_json_ptr(this->json_parent.top());
this->json_parent.pop();
}
auto zoom_level() const noexcept {
auto level = this->json_parent.size();
return level;
}
auto begin() noexcept {
return this->json->begin();
}
auto first_field_name() noexcept {
// TODO #112: handle errors
std::string name = "";
assert(this->json->size() > 0);
assert(this->json->begin()->size() > 0);
for (auto &entry : this->json->at(this->named_array_read_count++).items())
{
name = entry.key();
}
if (name == "")
assert(false);
return name;
}
std::size_t n_elements(const bpstd::string_view &name) const noexcept {
std::size_t n_elem = 0;
for (auto i=0u; i<this->json->size(); ++i) {
for (auto &entry : this->json->at(i).items()) {
if (entry.key() == name)
n_elem = entry.value().size();
}
}
return n_elem;
}
auto n_numeric_array_entries() noexcept {
auto count = 0u;
for (auto i=0u; i<this->json->size(); ++i) {
assert(this->json->at(i).is_object());
assert(this->json->at(i).size() == 1);
for (auto &entry : this->json->at(i).items())
if (entry.value().is_array() && (entry.value().size() == 0 || entry.value().at(0).is_number()))
++count;
}
return count;
}
template <typename T>
void read_value(
const bpstd::string_view name,
T *var
) {
*var = this->find(name)->get<T>();
}
void read_str(
const bpstd::string_view &name,
char* var_data,
int* var_size
) noexcept {
auto it = this->find(name);
if (it == this->json->end())
{
assert(false);
// TODO #112
return;
}
auto value = it->is_array()
? name
: it->begin()->get<bpstd::string_view>(); // TODO #112: is this path used anywhere?
if ((int)value.size() > *var_size) {
std::ostringstream oss;
oss << "provided entry \"" << name << "\" has too many characters";
this->warn(std::invalid_argument(oss.str()));
}
for (auto i = 0u; i < value.size(); ++i)
var_data[i] = value[i];
var_size[0] = value.size();
}
template <typename T1, typename T2>
void read_arr(
const T1 &name,
const tcb::span<T2> &values
) noexcept {
for (auto i=0u; i<this->json->size(); ++i) {
for (auto &entry : this->json->at(i).items()) {
if (entry.key() == name) {
for (auto j = 0u; j < values.size(); ++j)
values[j] = entry.value().at(j);
return;
}
}
}
// TODO #112: check size
}
auto varid(const std::string& name) noexcept {
auto it = this->vars.find(name);
if (it == this->vars.end()) {
this->warn(std::logic_error("var not found!"));
}
return std::distance(this->vars.begin(), it);
}
template <typename T>
auto find(const T& entry) noexcept -> decltype(this->json->end()) {
auto it = this->json->find(entry);
if (it == this->json->end()) {
std::ostringstream oss;
oss << "provided data is missing the \"" << entry << "\" entry";
this->warn(std::invalid_argument(oss.str()));
}
return it;
}
InputGuard *get_input_guard_ptr() {
return input_guard_ptr.get();
}
virtual std::string str() const = 0;
virtual bool read_line(std::string &name, std::string &data) = 0;
virtual int read_line_data_size_and_start_enumerating() = 0;
};
struct InputJSONResource: JSONResource {
private:
std::string key_cond, key_name;
std::string last_read_line_key = "";
std::size_t max_zoom_level;
public:
InputJSONResource(
const nlohmann::json &json,
const std::string key_cond = "",
const std::string key_name = "",
const std::size_t max_zoom_level = 3
) : JSONResource(json), key_cond(key_cond), key_name(key_name), max_zoom_level(max_zoom_level)
{
}
std::string str() const {
throw std::logic_error("str() called on InputJSONResource!");
}
bool read_line(std::string &name, std::string &data) {
bool subsequent_record = false;
if (this->zoom_level() == this->max_zoom_level) {
this->zoom_out();
auto key = this->next_dict_key(this->last_read_line_key, this->key_cond);
if (key == "") {
if (this->index == 0)
this->last_read_line_key = "";
else
this->last_read_line_key = this->key_cond;
return true;
}
else
subsequent_record = true;
}
auto key = this->next_dict_key(this->last_read_line_key, this->key_cond);
if (subsequent_record || (this->key_name != "" && (this->key_cond == this->last_read_line_key))) {
name = this->key_name;
this->zoom_in(key);
} else {
name = key;
}
data = key;
this->last_read_line_key = key;
return false;
}
int read_line_data_size_and_start_enumerating() {
if (this->zoom_level() == this->max_zoom_level - 2) {
this->index = this->n_elements(this->key_cond);
return this->index;
}
return 1;
}
static bool unique_keys(const nlohmann::json &json) {
std::set<std::string> keys;
for (auto i=0u; i<json.size(); ++i) {
for (auto &entry : json.at(i).items()) {
if (keys.find(entry.key()) != keys.end())
return false;
keys.insert(entry.key());
}
}
return true;
}
};
struct OutputJSONResource: JSONResource {
std::unique_ptr<nlohmann::json> guard;
OutputJSONResource() : guard(std::make_unique<nlohmann::json>()) {
this->set_current_json_ptr(this->guard.get());
}
std::string str() const {
return this->dump();
}
bool read_line(std::string &, std::string &) {
throw std::logic_error("read_line() called on OutputJSONResource!");
}
int read_line_data_size_and_start_enumerating() {
throw std::logic_error("read_line_data_size_and_start_enumerating() called on OutputJSONResource!");
}
};
std::unique_ptr<JSONResource> &json_resource_ptr();
template <typename T>
struct JSONResourceGuard {
JSONResourceGuard() {
json_resource_ptr() = std::make_unique<T>();
}
JSONResourceGuard(const nlohmann::json & json) {
json_resource_ptr() = std::make_unique<T>(json);
}
JSONResourceGuard(
const nlohmann::json & json,
const std::string key_cond,
const std::string key_name
) {
json_resource_ptr() = std::make_unique<T>(json, key_cond, key_name);
}
JSONResourceGuard(
const nlohmann::json & json,
const std::string key_cond,
const std::string key_name,
const int max_zoom_level
) {
json_resource_ptr() = std::make_unique<T>(json, key_cond, key_name, max_zoom_level);
}
~JSONResourceGuard() {
json_resource_ptr().reset();
}
};