forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_info_builder.cpp
More file actions
300 lines (254 loc) · 9.57 KB
/
view_info_builder.cpp
File metadata and controls
300 lines (254 loc) · 9.57 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
#include <cstring>
#include <string.h>
#include "view_info_builder.h"
#include "xml_util.h"
#include "../exceptions.h"
#include <algorithm>
namespace mfast {
namespace xml_parser {
void view_info_builder::visit_basic(const field_instruction *inst,
void *pIndex) {
std::size_t current_context_size = current_context_.size();
if (current_context_size)
current_context_ += ".";
current_context_ += inst->name();
current_indeces_.push_back(
static_cast<int>(*static_cast<std::size_t *>(pIndex)));
infos_[current_context_] = current_indeces_;
current_indeces_.pop_back();
current_context_.resize(current_context_size);
}
void view_info_builder::visit(const group_field_instruction *inst,
void *pIndex) {
std::size_t current_context_size = current_context_.size();
if (pIndex) {
current_indeces_.push_back(
static_cast<int>(*static_cast<std::size_t *>(pIndex)));
if (current_context_size)
current_context_ += ".";
current_context_ += inst->name();
infos_[current_context_] = current_indeces_;
}
std::size_t i = 0;
for (auto subinst : inst->subinstructions()) {
subinst->accept(*this, &i);
++i;
}
if (pIndex) {
current_indeces_.pop_back();
current_context_.resize(current_context_size);
}
}
void view_info_builder::visit(const sequence_field_instruction *inst,
void *pIndex) {
std::size_t current_context_size = current_context_.size();
if (current_context_size)
current_context_ += ".";
current_context_ += inst->name();
current_indeces_.push_back(
pIndex ? static_cast<int>(*static_cast<std::size_t *>(pIndex)) : 0);
infos_[current_context_] = current_indeces_;
// std::cout << "inserting " << current_context_ << "\n";
current_context_ += "[]";
current_indeces_.push_back(-2); // place holder for sequence element index
infos_[current_context_] = current_indeces_;
// std::cout << "inserting " << current_context_ << "\n";
std::size_t i = 0;
for (auto subinst : inst->subinstructions()) {
subinst->accept(*this, &i);
++i;
}
current_indeces_.pop_back();
current_indeces_.pop_back();
current_context_.resize(current_context_size);
}
void view_info_builder::visit(const int32_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const uint32_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const int64_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const uint64_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const decimal_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const ascii_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const unicode_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const byte_vector_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const template_instruction *inst, void *pIndex) {
visit(static_cast<const group_field_instruction *>(inst), pIndex);
}
void view_info_builder::visit(const templateref_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const int32_vector_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const uint32_vector_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const int64_vector_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const uint64_vector_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const enum_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
void view_info_builder::visit(const set_field_instruction *inst,
void *pIndex) {
visit_basic(inst, pIndex);
}
struct tag_reference_name;
typedef boost::error_info<tag_reference_name, std::string> reference_name_info;
void view_info_builder::build_field_view(const tinyxml2::XMLElement &element,
unsigned &max_depth,
std::deque<field_view_info> &fields) {
std::string ref_name = get_optional_attr(element, "name", "");
if (ref_name.size()) {
field_view_info result = {field_view_info::CONTINUE_BIT, nullptr};
std::vector<int> sequence_indeces;
std::string ref_name_no_seq_index;
// remove anything in [] and save the numbers in [] into a queue
std::size_t pos = 0, new_pos;
do {
// find the first occurence of '['
new_pos = ref_name.find_first_of('[', pos);
if (new_pos != std::string::npos) {
++new_pos;
ref_name_no_seq_index += ref_name.substr(pos, (new_pos - pos));
pos = new_pos;
new_pos = ref_name.find_first_of(']', pos);
if (new_pos != std::string::npos) {
try {
sequence_indeces.push_back(boost::lexical_cast<unsigned>(
ref_name.substr(pos, new_pos - pos)));
pos = new_pos;
} catch (boost::bad_lexical_cast &) {
BOOST_THROW_EXCEPTION(
fast_static_error("Invalid reference specification, the token "
"inside [] must be a non-negative integer")
<< reference_name_info(ref_name));
}
}
} else {
ref_name_no_seq_index += ref_name.substr(pos);
}
} while (new_pos != std::string::npos);
// find the field index
// std::cout << "finding " << ref_name_no_seq_index << "\n";
auto it = infos_.find(ref_name_no_seq_index);
if (it == infos_.end()) {
BOOST_THROW_EXCEPTION(
fast_static_error(
"Invalid reference specification, no such reference name exists")
<< reference_name_info(ref_name));
}
const indeces_t &field_indeces = it->second;
// replace every instance -2 with the indeces in []
typedef field_view_info::nest_index_t nest_index_t;
nest_index_t *nest_indices = static_cast<nest_index_t *>(
alloc_.allocate(sizeof(nest_index_t) * field_indeces.size() + 1));
unsigned i = 0, j = 0;
for (i = 0; i < field_indeces.size(); ++i) {
if (field_indeces[i] != -2)
nest_indices[i] = field_indeces[i];
else {
// -2 is used for specifying undefined sequence index
if (j < sequence_indeces.size())
nest_indices[i] = sequence_indeces[j++];
else {
BOOST_THROW_EXCEPTION(
fast_static_error("Invalid reference specification, no such "
"reference name exists")
<< reference_name_info(ref_name));
}
}
}
// the nest_indeces must terminiated with a -1
nest_indices[i] = -1;
result.nest_indices = nest_indices;
max_depth = std::max<unsigned>(max_depth,
static_cast<unsigned>(field_indeces.size()));
result.prop += 1;
if (fields.size()) {
result.prop += std::mismatch(nest_indices, nest_indices + i,
fields.back().nest_indices)
.first -
nest_indices;
}
fields.push_back(result);
}
}
aggregate_view_info
view_info_builder::build(const tinyxml2::XMLElement &element,
const mfast::group_field_instruction *inst) {
const char *name = get_optional_attr(element, "name", nullptr);
if (name == nullptr)
BOOST_THROW_EXCEPTION(
fast_static_error("A view must has a name attribute"));
this->visit(inst, nullptr);
aggregate_view_info result;
result.max_depth_ = 0;
std::size_t sz = std::strlen(name) + 1;
result.name_ = reinterpret_cast<const char *>(
std::memcpy(new (alloc_) char[sz], name, sz));
// result.name_ = std::strcpy(new (alloc_) char[std::strlen(name) + 1],
// name);
std::deque<field_view_info> fields;
const tinyxml2::XMLElement *child = element.FirstChildElement();
while (child != nullptr) {
if (std::strcmp(child->Name(), "field") == 0) {
const tinyxml2::XMLElement *grandchild = child->FirstChildElement();
while (grandchild != nullptr) {
build_field_view(*grandchild, result.max_depth_, fields);
grandchild = grandchild->NextSiblingElement();
}
fields.back().prop &= ~field_view_info::CONTINUE_BIT;
}
child = child->NextSiblingElement();
}
field_view_info terminator = {0, nullptr};
fields.push_back(terminator);
auto data = new (alloc_) field_view_info[fields.size()];
std::copy(fields.begin(), fields.end(), data);
result.data_ = mfast::array_view<const field_view_info>(data, fields.size());
result.instruction_ = inst;
return result;
}
void view_info_builder::print() {
for (auto v : infos_) {
std::cout << v.first << "=>";
for (auto &elem : v.second)
std::cout << elem << ", ";
std::cout << "\n";
}
}
} /* xml_parser */
} /* mfast */