Skip to content

Commit 2979409

Browse files
Merge pull request #148 from martinfantini/sequence_encode
Sequence encode
2 parents a74af23 + 6171f6f commit 2979409

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-8
lines changed

src/mfast/coder/encoder/fast_encoder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ inline void fast_encoder_impl::visit(group_cref cref, int) {
124124

125125
inline void fast_encoder_impl::visit(sequence_cref cref, int) {
126126

127+
if (cref.instruction()->optional() && !cref.present()) {
128+
if (cref.instruction()->length_instruction()->pmap_size() > 0)
129+
this->current_->set_next_bit(false);
130+
if (cref.instruction()->length_instruction()->field_operator() != operator_constant &&
131+
cref.instruction()->length_instruction()->field_operator() != operator_default &&
132+
cref.instruction()->length_instruction()->field_operator() != operator_copy)
133+
strm_.encode_null();
134+
return;
135+
}
136+
127137
value_storage storage;
128138

129139
uint32_mref length_mref(nullptr, &storage,

src/mfast/coder/encoder_v2/fast_encoder_core.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ inline void fast_encoder_core::encode_field(const T &ext_ref, group_type_tag) {
259259
template <typename T>
260260
inline void fast_encoder_core::encode_field(const T &ext_ref,
261261
sequence_type_tag) {
262+
263+
if (ext_ref.get().instruction()->optional() && !ext_ref.present()) {
264+
if (T::length_type::has_pmap_type::value)
265+
this->current_->set_next_bit(false);
266+
if(!std::is_same<typename T::length_type::operator_category, constant_operator_tag>::value &&
267+
!std::is_same<typename T::length_type::operator_category, copy_operator_tag>::value &&
268+
!std::is_same<typename T::length_type::operator_category, default_operator_tag>::value )
269+
strm_.encode_null();
270+
return;
271+
}
272+
262273
value_storage storage;
263274

264275
typename T::length_type length = ext_ref.get_length(storage);

src/mfast/ext_ref.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class ext_cref<sequence_cref, LengthExtRef, ElementExtRef> {
167167
}
168168
std::size_t size() const { return base_.size(); }
169169

170+
bool present() const { return base_.present(); }
171+
170172
private:
171173
sequence_cref base_;
172174
};

tests/encoder_decoder_test.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,38 @@ TEST_CASE("simple optional field with default value encoder/decoder","[optional_
245245
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
246246
}
247247
}
248+
249+
TEST_CASE("sequence optional with constant length encoder/decoder","[optional_sequence_with_constant_length_encoder_decoder]")
250+
{
251+
fast_test_coding_case<simple12::templates_description> test_case;
252+
253+
SECTION("encode fields without sequence")
254+
{
255+
simple12::Test_7 test_7;
256+
simple12::Test_7_mref test_7_mref = test_7.mref();
257+
test_7_mref.set_field_7_1().as(1);
258+
REQUIRE(test_case.encoding(test_7.cref(),"\xE0\x87\x82",true));
259+
REQUIRE(test_case.decoding("\xE0\x87\x82",test_7.cref(),true));
260+
}
261+
262+
SECTION("encode fields with sequence")
263+
{
264+
simple12::Test_7 test_7;
265+
simple12::Test_7_mref test_7_mref = test_7.mref();
266+
test_7_mref.set_field_7_1().as(1);
267+
268+
{
269+
auto sequence_7_mref = test_7_mref.set_sequence_7();
270+
sequence_7_mref.resize(1);
271+
272+
{
273+
auto element_sequence = sequence_7_mref.front();
274+
element_sequence.set_field_7_4().as(1);
275+
element_sequence.set_field_7_5().as(1);
276+
}
277+
}
278+
279+
REQUIRE(test_case.encoding(test_7.cref(),"\xF0\x87\x82\xa0\x82\x82",true));
280+
REQUIRE(test_case.decoding("\xF0\x87\x82\xa0\x82\x82",test_7.cref(),true));
281+
}
282+
}

tests/encoder_decoder_test_v2.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TEST_CASE("simple field and sequence optional encoder_v2/decoder_v2","[field_seq
2222
// 1 : Set Template Id.
2323
// 1 : Set Field field_1_1
2424
// 0 : XXX
25-
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\x81",true));
25+
REQUIRE(test_case.encoding(test_1.cref(),"\xE0\x81\x82\x80",true));
2626
REQUIRE(test_case.decoding("\xE0\x81\x82\x80",test_1.cref(),true));
2727
}
2828

@@ -246,3 +246,38 @@ TEST_CASE("simple optional field with default value encoder_v2/decoder_v2","[opt
246246
REQUIRE(test_case.decoding("\xD0\x86\x82",test_6.cref(),true));
247247
}
248248
}
249+
250+
TEST_CASE("sequence optional with constant length encoder_v2/decoder_v2","[optional_sequence_with_constant_length_encoder_v2/decoder_v2]")
251+
{
252+
fast_test_coding_case_v2<simple12::templates_description> test_case;
253+
254+
SECTION("encode fields without sequence")
255+
{
256+
simple12::Test_7 test_7;
257+
simple12::Test_7_mref test_7_mref = test_7.mref();
258+
test_7_mref.set_field_7_1().as(1);
259+
REQUIRE(test_case.encoding(test_7.cref(),"\xE0\x87\x82",true));
260+
REQUIRE(test_case.decoding("\xE0\x87\x82",test_7.cref(),true));
261+
}
262+
263+
SECTION("encode fields with sequence")
264+
{
265+
simple12::Test_7 test_7;
266+
simple12::Test_7_mref test_7_mref = test_7.mref();
267+
test_7_mref.set_field_7_1().as(1);
268+
269+
{
270+
auto sequence_7_mref = test_7_mref.set_sequence_7();
271+
sequence_7_mref.resize(1);
272+
273+
{
274+
auto element_sequence = sequence_7_mref.front();
275+
element_sequence.set_field_7_4().as(1);
276+
element_sequence.set_field_7_5().as(1);
277+
}
278+
}
279+
280+
REQUIRE(test_case.encoding(test_7.cref(),"\xF0\x87\x82\xa0\x82\x82",true));
281+
REQUIRE(test_case.decoding("\xF0\x87\x82\xa0\x82\x82",test_7.cref(),true));
282+
}
283+
}

tests/simple12.xml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
<template name="Test_1" id="1">
44
<uInt32 name="field_1_1" id="11" presence="optional"><copy/></uInt32>
55
<sequence name="sequence_1" presence="optional">
6-
<uInt32 name="field_1_2" id="12" presence="optional"></uInt32>
7-
<uInt32 name="field_1_3" id="13" presence="optional"></uInt32>
6+
<length name="num_elements" id="12"/>
7+
<uInt32 name="field_1_2" id="13" presence="optional"></uInt32>
8+
<uInt32 name="field_1_3" id="14" presence="optional"></uInt32>
89
</sequence>
910
</template>
1011
<template name="Test_2" id="2">
@@ -14,8 +15,9 @@
1415
<template name="Test_3" id="3">
1516
<uInt32 name="field_3_1" id="31"><copy/></uInt32>
1617
<sequence name="sequence_3">
17-
<uInt32 name="field_3_2" id="32"></uInt32>
18-
<uInt32 name="field_3_3" id="33"></uInt32>
18+
<length name="num_elements" id="32"/>
19+
<uInt32 name="field_3_2" id="33"></uInt32>
20+
<uInt32 name="field_3_3" id="34"></uInt32>
1921
</sequence>
2022
</template>
2123
<template name="Test_4" id="4">
@@ -26,10 +28,11 @@
2628
<uInt32 name="field_5_1" id="51" presence="optional"><copy/></uInt32>
2729
<group name="test_5_group" presence="optional">
2830
<sequence name="sequence_5">
29-
<uInt32 name="field_5_2" id="52"></uInt32>
30-
<uInt32 name="field_5_3" id="53"></uInt32>
31+
<length name="num_elements" id="52"/>
32+
<uInt32 name="field_5_2" id="53"></uInt32>
33+
<uInt32 name="field_5_3" id="54"></uInt32>
3134
</sequence>
32-
<uInt32 name="field_5_5" id="54" presence="optional"><copy/></uInt32>
35+
<uInt32 name="field_5_5" id="55" presence="optional"><copy/></uInt32>
3336
</group>
3437
</template>
3538
<template name="Test_6" id="6">
@@ -43,4 +46,23 @@
4346
<default value="1"/>
4447
</uInt32>
4548
</template>
49+
<template name="Test_7" id="7">
50+
<uInt32 name="field_7_1" id="71" presence="optional">
51+
<copy/>
52+
</uInt32>
53+
<sequence name="sequence_7" presence="optional">
54+
<length name="num_elements" id="72">
55+
<constant value="1"/>
56+
</length>
57+
<uInt32 name="field_7_3" id="73">
58+
<default value="0"/>
59+
</uInt32>
60+
<uInt32 name="field_7_4" id="74" presence="optional">
61+
<delta/>
62+
</uInt32>
63+
<uInt32 name="field_7_5" id="75" presence="optional">
64+
<copy/>
65+
</uInt32>
66+
</sequence>
67+
</template>
4668
</templates>

0 commit comments

Comments
 (0)