forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_field_operator.cpp
More file actions
631 lines (531 loc) · 22.8 KB
/
decoder_field_operator.cpp
File metadata and controls
631 lines (531 loc) · 22.8 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// 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 "../common/codec_helper.h"
#include "fast_istream.h"
#include "decoder_presence_map.h"
#include "decoder_field_operator.h"
#include "fast_istream_extractor.h"
#include "check_overflow.h"
namespace mfast {
void decoder_field_operator::decode(const int32_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const uint32_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const int64_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const uint64_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const ascii_string_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const unicode_string_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const byte_vector_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
void decoder_field_operator::decode(const decimal_mref & /* mref */,
fast_istream & /* stream */,
decoder_presence_map & /* pmap */) const {}
namespace decoder_detail {
template <typename Operator> struct decimal_decoder {
void decode_decimal(const decimal_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const {
const Operator *derived = static_cast<const Operator *>(this);
if (!mref.has_individual_operators())
derived->decode_impl(mref, stream, pmap);
else {
derived->decode_impl(mref.set_exponent(), stream, pmap);
if (mref.present()) {
int64_mref mantissa_mref = mref.set_mantissa();
const decoder_field_operator *mantissa_operator =
decoder_operators[mantissa_mref.instruction()->field_operator()];
mantissa_operator->decode(mantissa_mref, stream, pmap);
}
}
}
};
class no_operator : public decoder_field_operator,
public mfast::detail::codec_helper,
public decimal_decoder<no_operator> {
public:
no_operator() {}
template <typename T>
void decode_impl(const T &mref, fast_istream &stream,
decoder_presence_map & /* pmap */) const {
stream >> mref;
// Fast Specification 1.1, page 22
//
// If a field is mandatory and has no field operator, it will not occupy any
// bit in the presence map and its value must always appear in the stream.
//
// If a field is optional and has no field operator, it is encoded with a
// nullable representation and the NULL is used to represent absence of a
// value. It will not occupy any bits in the presence map.
save_previous_value(mref);
}
virtual void decode(const int32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const int64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const ascii_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const unicode_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const byte_vector_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const decimal_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_decimal(mref, stream, pmap);
}
};
class constant_operator : public decoder_field_operator,
public mfast::detail::codec_helper,
public decimal_decoder<constant_operator> {
public:
constant_operator() {}
template <typename T>
void decode_impl(const T &mref, fast_istream & /* stream */,
decoder_presence_map &pmap) const {
// A field will not occupy any bit in the presence map if it is mandatory
// and has the constant operator.
// An optional field with the constant operator will occupy a single bit. If
// the bit is set, the value
// is the initial value in the instruction context. If the bit is not set,
// the value is considered absent.
if (!mref.optional()) {
mref.to_initial_value();
} else {
if (pmap.is_next_bit_set()) {
mref.to_initial_value();
} else {
mref.omit();
}
}
save_previous_value(mref);
}
virtual void decode(const int32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const int64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const ascii_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const unicode_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const byte_vector_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const decimal_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_decimal(mref, stream, pmap);
}
};
template <typename Operation>
class copy_or_increment_operator_impl : public mfast::detail::codec_helper {
public:
template <typename T>
void decode_impl(const T &mref, fast_istream &stream,
decoder_presence_map &pmap) const {
if (pmap.is_next_bit_set()) {
stream >> mref;
// A NULL indicates that the value is absent and the state of the previous
// value is set to empty
save_previous_value(mref);
} else {
value_storage &previous = previous_value_of(mref);
if (!previous.is_defined()) {
// if the previous value is undefined – the value of the field is the
// initial value
// that also becomes the new previous value.
// If the field has optional presence and no initial value, the field is
// considered
// absent and the state of the previous value is changed to empty.
mref.to_initial_value();
save_previous_value(mref);
if (mref.instruction()->mandatory_without_initial_value()) {
// Unless the field has optional presence, it is a dynamic error [ERR
// D5]
// if the instruction context has no initial value.
BOOST_THROW_EXCEPTION(fast_dynamic_error("D5"));
}
} else if (previous.is_empty()) {
// It is a dynamic error [ERR D6] if the field is mandatory.
if (!mref.optional()) {
BOOST_THROW_EXCEPTION(fast_dynamic_error("D6"));
}
// if the previous value is empty – the value of the field is empty.
// If the field is optional the value is considered absent.
mref.omit();
} else {
Operation()(mref, previous);
// if the previous value is assigned – the value of the field is the
// previous value.
load_previous_value(mref);
}
}
}
};
struct null_operation {
template <typename T> void operator()(const T &, value_storage &) const {}
};
class copy_operator : public decoder_field_operator,
public copy_or_increment_operator_impl<null_operation>,
public decimal_decoder<copy_operator> {
public:
copy_operator() {}
virtual void decode(const int32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const int64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const ascii_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const unicode_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const byte_vector_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const decimal_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_decimal(mref, stream, pmap);
}
};
struct increment_operation {
template <typename T> void operator()(T &, value_storage &previous) const {
previous.set<typename T::value_type>(
previous.get<typename T::value_type>() + 1);
}
};
class increment_operator
: public decoder_field_operator,
copy_or_increment_operator_impl<increment_operation> {
public:
increment_operator() {}
virtual void decode(const int32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const int64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
};
class default_operator : public decoder_field_operator,
public mfast::detail::codec_helper,
public decimal_decoder<default_operator> {
public:
default_operator() {}
template <typename T>
void decode_impl(const T &mref, fast_istream &stream,
decoder_presence_map &pmap) const {
// Mandatory integer, decimal, string and byte vector fields – one bit. If
// set, the value appears in the stream.
// Optional integer, decimal, string and byte vector fields – one bit. If
// set, the value appears in the stream in a nullable representation.
if (pmap.is_next_bit_set()) {
stream >> mref;
// A NULL indicates that the value is absent and the state of the
// previous value is left unchanged.
if (mref.absent())
return;
} else {
// If the field has optional presence and no initial value, the field is
// considered absent
// when there is no value in the stream.
// The default operator specifies that the value of a field is either
// present in the stream
// or it will be the initial value.
mref.to_initial_value();
}
save_previous_value(mref);
}
virtual void decode(const int32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint32_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const int64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const uint64_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const ascii_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const unicode_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const byte_vector_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const decimal_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_decimal(mref, stream, pmap);
}
};
class delta_operator : public decoder_field_operator,
public mfast::detail::codec_helper {
template <typename T>
void decode_integer(const T &mref, fast_istream &stream) const {
int64_t d;
if (stream.decode(d, mref.instruction()->is_nullable())) {
value_storage bv = delta_base_value_of(mref);
T tmp(nullptr, &bv, nullptr);
check_overflow(tmp.value(), d, mref.instruction(), stream);
mref.as(static_cast<typename T::value_type>(tmp.value() + d));
save_previous_value(mref);
} else {
// If the field has optional presence, the delta value can be NULL. In
// that case the value of the field is considered absent.
mref.omit();
}
}
template <typename T>
void decode_string(const T &mref, fast_istream &stream,
decoder_presence_map & /* pmap */) const {
// The delta value is represented as a Signed Integer subtraction length
// followed by an ASCII String.
// If the delta is nullable, the subtraction length is nullable. A NULL
// delta is represented as a
// NULL subtraction length. The string part is present in the stream iff the
// subtraction length is not NULL.
int32_t substraction_length;
if (stream.decode(substraction_length, mref.instruction()->is_nullable())) {
// It is a dynamic error [ERR D7] if the subtraction length is larger than
// the
// number of characters in the base value, or if it does not fall in the
// value range of an int32.
int32_t sub_len =
substraction_length >= 0 ? substraction_length : ~substraction_length;
const value_storage &base_value = delta_base_value_of(mref);
if (sub_len > static_cast<int32_t>(base_value.array_length()))
BOOST_THROW_EXCEPTION(fast_dynamic_error("D7"));
uint32_t delta_len;
const typename T::value_type *delta_str = nullptr;
stream.decode(delta_str, delta_len, mref.instruction(), false);
this->apply_string_delta(mref, base_value, substraction_length, delta_str,
delta_len);
save_previous_value(mref);
} else {
mref.omit();
}
}
public:
delta_operator() {}
virtual void decode(const int32_mref &mref, fast_istream &stream,
decoder_presence_map & /* pmap */) const override {
this->decode_integer(mref, stream);
}
virtual void decode(const uint32_mref &mref, fast_istream &stream,
decoder_presence_map & /* pmap */) const override {
decode_integer(mref, stream);
}
virtual void decode(const int64_mref &mref, fast_istream &stream,
decoder_presence_map & /* pmap */) const override {
decode_integer(mref, stream);
}
virtual void decode(const uint64_mref &mref, fast_istream &stream,
decoder_presence_map & /* pmap */) const override {
decode_integer(mref, stream);
}
virtual void decode(const decimal_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
if (!mref.has_individual_operators()) {
stream >> mref;
if (mref.present()) {
value_storage bv = delta_base_value_of(mref);
check_overflow(bv.of_decimal.mantissa_, mref.mantissa(),
mref.instruction(), stream);
check_overflow(bv.of_decimal.exponent_, mref.exponent(),
mref.instruction(), stream);
mref.set_mantissa(bv.of_decimal.mantissa_ + mref.mantissa());
mref.set_exponent(bv.of_decimal.exponent_ + mref.exponent());
// if (mref.exponent() > 63 || mref.exponent() < -63 )
// BOOST_THROW_EXCEPTION(fast_reportable_error("R1"));
//
save_previous_value(mref);
} else {
mref.omit();
}
} else {
decode_integer(mref.set_exponent(), stream);
if (mref.present()) {
int64_mref mantissa_mref = mref.set_mantissa();
const decoder_field_operator *mantissa_operator =
decoder_operators[mantissa_mref.instruction()->field_operator()];
mantissa_operator->decode(mantissa_mref, stream, pmap);
}
}
}
virtual void decode(const ascii_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_string(mref, stream, pmap);
}
virtual void decode(const unicode_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_string(mref, stream, pmap);
}
virtual void decode(const byte_vector_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_string(mref, stream, pmap);
}
};
class tail_operator : public decoder_field_operator,
public mfast::detail::codec_helper {
private:
template <typename T>
void decode_impl(const T &mref, fast_istream &stream,
decoder_presence_map &pmap) const {
if (pmap.is_next_bit_set()) {
uint32_t len;
const typename T::value_type *str;
if (stream.decode(str, len, mref.instruction(),
mref.instruction()->is_nullable())) {
const value_storage &base_value(tail_base_value_of(mref));
this->apply_string_delta(mref, base_value,
std::min<int>(len, base_value.array_length()),
str, len);
} else {
// If the field has optional presence, the tail value can be NULL.
// In that case the value of the field is considered absent.
mref.omit();
}
} else {
// If the tail value is not present in the stream, the value of the field
// depends
// on the state of the previous value in the following way:
value_storage &prev = previous_value_of(mref);
if (!prev.is_defined()) {
// * undefined – the value of the field is the initial value that also
// becomes the new previous value.
// If the field has optional presence and no initial value, the field is
// considered absent and the state of the previous value is changed to
// empty.
mref.to_initial_value();
if (mref.instruction()->mandatory_without_initial_value()) {
// Unless the field has optional presence, it is a dynamic error [ERR
// D6] if the instruction context has no initial value.
BOOST_THROW_EXCEPTION(fast_dynamic_error("D6"));
}
} else if (prev.is_empty()) {
// * empty – the value of the field is empty. If the field is optional
// the value is considered absent.
// It is a dynamic error [ERR D7] if the field is mandatory.
if (!mref.optional())
BOOST_THROW_EXCEPTION(fast_dynamic_error("D7"));
mref.omit();
} else {
// * assigned – the value of the field is the previous value.
load_previous_value(mref);
return;
}
}
save_previous_value(mref);
}
public:
tail_operator() {}
virtual void decode(const ascii_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const unicode_string_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
virtual void decode(const byte_vector_mref &mref, fast_istream &stream,
decoder_presence_map &pmap) const override {
decode_impl(mref, stream, pmap);
}
};
static const no_operator no_operator_instance;
static const constant_operator constant_operator_instance;
static const copy_operator copy_operator_instance;
static const default_operator default_operator_instance;
static const delta_operator delta_operator_instance;
static const increment_operator increment_operator_instance;
static const tail_operator tail_operator_instance;
}
const decoder_field_operator *const decoder_operators[] = {
&decoder_detail::no_operator_instance,
&decoder_detail::constant_operator_instance,
&decoder_detail::delta_operator_instance,
&decoder_detail::default_operator_instance,
&decoder_detail::copy_operator_instance,
&decoder_detail::increment_operator_instance,
&decoder_detail::tail_operator_instance};
}