forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast_decoder.cpp
More file actions
327 lines (254 loc) · 9.14 KB
/
fast_decoder.cpp
File metadata and controls
327 lines (254 loc) · 9.14 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
// Copyright (c) 2016, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
// See the file license.txt for licensing information.
#include <boost/container/map.hpp>
#include "../mfast_coder_export.h"
#include "../fast_decoder.h"
#include "../../field_visitor.h"
#include "../../sequence_ref.h"
#include "../../malloc_allocator.h"
#include "../../output.h"
#include "../../composite_type.h"
#include "../common/exceptions.h"
#include "../common/debug_stream.h"
#include "../common/template_repo.h"
#include "../common/codec_helper.h"
#include "decoder_presence_map.h"
#include "decoder_field_operator.h"
#include "fast_istream.h"
#include "mfast/vector_ref.h"
namespace mfast {
struct fast_decoder_impl;
struct fast_decoder_impl {
fast_decoder_impl(mfast::allocator *alloc);
~fast_decoder_impl();
decoder_presence_map ¤t_pmap();
struct pmap_state {
decoder_presence_map pmap_;
decoder_presence_map *prev_pmap_;
pmap_state() : prev_pmap_(nullptr) {}
};
void decode_pmap(pmap_state &state) {
state.prev_pmap_ = this->current_;
this->current_ = &state.pmap_;
this->strm_.decode(state.pmap_);
}
void restore_pmap(pmap_state &state) {
if (state.prev_pmap_)
this->current_ = state.prev_pmap_;
}
void visit(enum_mref &mref) {
this->visit(reinterpret_cast<uint64_mref &>(mref));
}
void visit(set_mref &mref) {
this->visit(reinterpret_cast<uint64_mref &>(mref));
}
template <typename SimpleMRef> void visit(const SimpleMRef &mref);
template <typename IntType> void visit(const int_vector_mref<IntType> &mref);
void visit(const group_mref &mref, int);
void visit(const sequence_mref &mref, int);
void visit(const nested_message_mref &mref, int);
void visit(const sequence_element_mref &mref, int);
message_type *decode_segment(fast_istreambuf &sb);
typedef message_type info_entry;
struct info_entry_converter {
typedef info_entry repo_mapped_type;
info_entry_converter(mfast::allocator *alloc) : alloc_(alloc) {}
template <typename Message>
std::pair<mfast::allocator *, const template_instruction *>
to_repo_entry(const template_instruction *inst, Message *) {
return std::make_pair(alloc_, inst);
}
template_instruction *to_instruction(const repo_mapped_type &entry) {
return const_cast<template_instruction *>(entry.instruction());
}
mfast::allocator *alloc_;
};
template_repo<info_entry_converter> repo_;
allocator *message_alloc_;
fast_istream strm_;
message_type *active_message_;
bool force_reset_;
debug_stream debug_;
decoder_presence_map *current_;
std::ostream *warning_log_;
};
inline fast_decoder_impl::fast_decoder_impl(mfast::allocator *alloc)
: repo_(info_entry_converter(alloc)), message_alloc_(alloc), strm_(nullptr),
warning_log_(nullptr) {}
fast_decoder_impl::~fast_decoder_impl() {}
inline decoder_presence_map &fast_decoder_impl::current_pmap() {
return *current_;
}
template <typename SimpleMRef>
inline void fast_decoder_impl::visit(const SimpleMRef &mref) {
debug_ << " decoding " << mref.name() << ": pmap -> " << current_pmap()
<< "\n"
<< " stream -> " << strm_ << "\n";
const decoder_field_operator *field_operator =
decoder_operators[mref.instruction()->field_operator()];
field_operator->decode(mref, strm_, current_pmap());
if (mref.present())
debug_ << " decoded " << mref.name() << " = " << mref << "\n";
else
debug_ << " decoded " << mref.name() << " is absent\n";
}
template <typename IntType>
void fast_decoder_impl::visit(const int_vector_mref<IntType> &mref) {
debug_ << "decoding int vector " << mref.name();
uint32_t length = 0;
if (!strm_.decode(length, mref.optional())) {
mref.omit();
return;
}
mref.resize(length);
for (uint32_t i = 0; i < length; ++i) {
strm_.decode(mref[i], false);
}
}
inline void fast_decoder_impl::visit(const group_mref &mref, int) {
debug_ << "decoding group " << mref.name();
// If a group field is optional, it will occupy a single bit in the presence
// map.
// The contents of the group may appear in the stream iff the bit is set.
if (mref.optional()) {
debug_ << " : current pmap = " << current_pmap() << "\n";
if (!current_pmap().is_next_bit_set()) {
debug_ << " " << mref.name() << " is absent\n";
mref.omit();
return;
}
} else {
debug_ << " : mandatory\n";
}
pmap_state state;
if (mref.instruction()->segment_pmap_size() > 0) {
decode_pmap(state);
debug_ << " " << mref.name() << " has group pmap -> "
<< current_pmap() << "\n";
}
for (auto &&field : aggregate_mref(mref)) {
apply_mutator(*this, field);
}
restore_pmap(state);
}
inline void fast_decoder_impl::visit(const sequence_mref &mref, int) {
debug_ << "decoding sequence " << mref.name() << " ---\n";
const uint32_field_instruction *length_instruction =
mref.instruction()->length_instruction();
value_storage storage;
debug_ << " decoding sequence length " << mref.name() << " : stream -> "
<< strm_ << "\n";
uint32_mref length_mref(nullptr, &storage, length_instruction);
this->visit(length_mref);
if (length_mref.present()) {
debug_ << " decoded sequence length " << length_mref.value() << "\n";
mref.resize(length_mref.value());
} else {
debug_ << " " << mref.name() << "is absent\n";
mref.omit();
}
if (length_mref.present() && length_mref.value() > 0) {
int i = 0;
for (auto &&elem : mref) {
this->visit(elem, i++);
}
}
}
inline void fast_decoder_impl::visit(const sequence_element_mref &mref,
int index) {
debug_ << "decoding element[" << index << "] : segment pmap size = "
<< mref.instruction()->segment_pmap_size() << "\n";
pmap_state state;
if (mref.instruction()->segment_pmap_size() > 0) {
decode_pmap(state);
debug_ << " decoded pmap -> " << current_pmap() << "\n";
}
for (auto &&field : mref) {
apply_mutator(*this, field);
}
restore_pmap(state);
}
inline void fast_decoder_impl::visit(const nested_message_mref &mref, int) {
pmap_state state;
message_type *saved_active_message = active_message_;
debug_ << "decoding dynamic templateRef ...\n";
decode_pmap(state);
debug_ << " decoded pmap -> " << current_pmap() << "\n";
decoder_presence_map &pmap = current_pmap();
if (pmap.is_next_bit_set()) {
uint32_t template_id;
strm_.decode(template_id, false);
debug_ << " decoded template id -> " << template_id << "\n";
// find the message with corresponding template id
active_message_ = repo_.find(template_id);
if (active_message_ == nullptr) {
using namespace coder;
BOOST_THROW_EXCEPTION(fast_dynamic_error("D9")
<< template_id_info(template_id));
}
mref.set_target_instruction(active_message_->instruction(), false);
}
for (auto &&field : mref.target()) {
apply_mutator(*this, field);
}
restore_pmap(state);
active_message_ = saved_active_message;
}
message_type *fast_decoder_impl::decode_segment(fast_istreambuf &sb) {
strm_.reset(&sb);
decoder_presence_map pmap;
this->current_ = &pmap;
strm_.decode(pmap);
debug_ << "decoding segment : pmap -> " << pmap << "\n"
<< " entity -> " << strm_ << "\n";
uint32_t template_id = 0;
if (pmap.is_next_bit_set()) {
strm_.decode(template_id, false);
debug_ << "decoded template id = " << template_id << "\n";
// find the message with corresponding template id
active_message_ = repo_.find(template_id);
}
if (active_message_ == nullptr) {
BOOST_THROW_EXCEPTION(fast_dynamic_error("D9")
<< coder::template_id_info(template_id));
}
if (force_reset_ || active_message_->instruction()->has_reset_attribute()) {
repo_.reset_dictionary();
}
// we have to keep the active_message_ in a new variable
// because after the accept_mutator(), the active_message_
// may change because of the decoding of dynamic template reference
message_type *message = active_message_;
// message->ensure_valid();
// message->ref().accept_mutator(*this);
for (auto &&field : message->ref()) {
apply_mutator(*this, field);
}
this->current_ = nullptr;
return message;
}
fast_decoder::fast_decoder(allocator *alloc)
: impl_(new fast_decoder_impl(alloc)) {}
fast_decoder::~fast_decoder() { delete impl_; }
void fast_decoder::include(const templates_description *const *descriptions,
std::size_t description_count) {
impl_->repo_.build(descriptions, description_count);
impl_->active_message_ = impl_->repo_.unique_entry();
}
message_cref fast_decoder::decode(const char *&first, const char *last,
bool force_reset) {
assert(first < last);
fast_istreambuf sb(first, last - first);
impl_->force_reset_ = force_reset;
message_cref result = impl_->decode_segment(sb)->cref();
first = sb.gptr();
return result;
}
void fast_decoder::debug_log(std::ostream *log) { impl_->debug_.set(log); }
void fast_decoder::warning_log(std::ostream *os) {
impl_->strm_.warning_log(os);
}
}