Skip to content

Optional group inside sequence #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/fast_type_gen/inl_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,16 @@ void inl_gen::visit(const mfast::sequence_field_instruction *inst,
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
const field_instruction *subinst = inst->subinstructions()[i];

out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)["
<< i << "]) );\n";
if (is_group_type(subinst) && subinst->optional())
{
out_ << " {\n"
<< " " << get_ext_cref_type(subinst) << " ext_cref_group((*this)[" << i << "]);\n"
<< " ext_cref_group.set_group_present(this->field_storage(" << i << ")->is_present());\n"
<< " visitor.visit(ext_cref_group);\n"
<< " }\n";
}
else
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)[" << i << "]) );\n";
}

out_ << "}\n\n";
Expand Down
4 changes: 2 additions & 2 deletions src/mfast/ext_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ class ext_cref<BaseCRef, group_type_tag, Properties>
explicit ext_cref(const field_cref &base) : base_(base) {}
explicit ext_cref(const aggregate_cref &base) : base_(base) {}
cref_type get() const { return base_; }
bool present() const { return !this->optional() || group_present_; }
bool present() const { return group_present_; }

void set_group_present(bool present) { group_present_ = present; }

private:
cref_type base_;
bool group_present_ = true;
bool group_present_ = this->optional()?false:true;
};

template <typename Properties>
Expand Down
6 changes: 2 additions & 4 deletions tests/fast_test_coding_case_v2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class fast_test_coding_case_v2
decoder_v2_(DESC::instance())
{}

bool
encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
bool encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
{
const int buffer_size = 128;
char buffer[buffer_size];
Expand All @@ -32,8 +31,7 @@ class fast_test_coding_case_v2
return false;
}

bool
decoding(const byte_stream& bytes, const mfast::message_cref& result, bool reset=false)
bool decoding(const byte_stream& bytes, const mfast::message_cref& result, bool reset=false)
{
const char* first = bytes.data();
mfast::message_cref msg = decoder_v2_.decode(first, first+bytes.size(), reset);
Expand Down
38 changes: 38 additions & 0 deletions tests/sequence_encoder_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,41 @@ TEST_CASE("group sequence inside sequence encoder/decoder","[group_sequence_insi
REQUIRE(test_case.decoding("\xC0\x86\x81\xD0\xB2\x82\xB2",test_6.cref(),true));
}
}

TEST_CASE("sequence with optional group encoder/decoder","[sequence_optional_group_encoder_decoder]")
{
fast_test_coding_case<simple14::templates_description> test_case;

SECTION("group not present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

auto sequence_7_mref = test_7_mref.set_sequence_7();
sequence_7_mref.resize(1);

auto element_sequence = sequence_7_mref.front();
element_sequence.set_field_7_3().as(50);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xC0\xB2",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xC0\xB2",test_7.cref(),true));
}

SECTION("group present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

auto sequence_7_mref = test_7_mref.set_sequence_7();
sequence_7_mref.resize(1);

auto element_sequence = sequence_7_mref.front();
element_sequence.set_field_7_3().as(50);

auto group_7 = element_sequence.set_group_7();
group_7.set_field_7_4().as(20);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xE0\xB2\x94",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xE0\xB2\x94",test_7.cref(),true));
}
}
38 changes: 38 additions & 0 deletions tests/sequence_encoder_decoder_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,41 @@ TEST_CASE("group sequence inside sequence encoder_V2/decoder_v2","[group_sequenc
REQUIRE(test_case.decoding("\xC0\x86\x81\xD0\xB2\x82\xB2",test_6.cref(),true));
}
}

TEST_CASE("sequence with optional group encoder_V2/decoder_v2","[sequence_optional_group_encoder_v2_decoder_v2]")
{
fast_test_coding_case_v2<simple14::templates_description> test_case;

SECTION("group not present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

auto sequence_7_mref = test_7_mref.set_sequence_7();
sequence_7_mref.resize(1);

auto element_sequence = sequence_7_mref.front();
element_sequence.set_field_7_3().as(50);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xC0\xB2",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xC0\xB2",test_7.cref(),true));
}

SECTION("group present")
{
simple14::Test_7 test_7;
simple14::Test_7_mref test_7_mref = test_7.mref();

auto sequence_7_mref = test_7_mref.set_sequence_7();
sequence_7_mref.resize(1);

auto element_sequence = sequence_7_mref.front();
element_sequence.set_field_7_3().as(50);

auto group_7 = element_sequence.set_group_7();
group_7.set_field_7_4().as(20);

REQUIRE(test_case.encoding(test_7.cref(),"\xC0\x87\x81\xE0\xB2\x94",true));
REQUIRE(test_case.decoding("\xC0\x87\x81\xE0\xB2\x94",test_7.cref(),true));
}
}
9 changes: 9 additions & 0 deletions tests/simple14.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@
</sequence>
</group>
</template>
<template name="Test_7" id="7">
<sequence name="sequence_7">
<length name="field_7_2" id="72"></length>
<uInt32 name="field_7_3" id="73"><copy/></uInt32>
<group name="group_7" presence="optional">
<uInt32 name="field_7_4" id="74"/>
</group>
</sequence>
</template>
</templates>