Skip to content
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

Group optional encoder_v2 #144

Merged
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
44 changes: 35 additions & 9 deletions src/fast_type_gen/inl_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ std::string get_properties_type(const Instruction *inst) {
using namespace mfast;
struct ext_cref_type_getter : mfast::field_instruction_visitor {
std::stringstream out_;
bool is_group_type_ = false;

public:
ext_cref_type_getter() {}

std::string get() { return out_.str(); }
bool group_type() { return is_group_type_; }

virtual void visit(const int32_field_instruction *inst, void *) override {
out_ << "ext_cref<int32_cref, " << get_operator_tag(inst) << ", "
Expand Down Expand Up @@ -83,6 +85,7 @@ struct ext_cref_type_getter : mfast::field_instruction_visitor {
virtual void visit(const group_field_instruction *inst, void *) override {
out_ << "ext_cref< " << codegen_base::cpp_name(inst)
<< "_cref, group_type_tag, " << get_properties_type(inst) << ">";
is_group_type_ = true;
}

virtual void visit(const sequence_field_instruction *inst, void *) override;
Expand Down Expand Up @@ -132,6 +135,12 @@ std::string get_ext_cref_type(const field_instruction *inst) {
return getter.get();
}

bool is_group_type(const field_instruction *inst) {
ext_cref_type_getter getter;
inst->accept(getter, nullptr);
return getter.group_type();
}

void ext_cref_type_getter::visit(const sequence_field_instruction *inst,
void *) {
const uint32_field_instruction *length_inst = inst->length_instruction();
Expand Down Expand Up @@ -473,9 +482,17 @@ void inl_gen::visit(const mfast::group_field_instruction *inst, void *pIndex) {

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"
Copy link
Contributor Author

@martinfantini martinfantini Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the set of the group presence, at the encode time.

<< " visitor.visit(ext_cref_group);\n"
<< " }\n";
}
else
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)[" << i << "]) );\n";
}

out_ << "}\n\n";
Expand Down Expand Up @@ -508,6 +525,7 @@ void inl_gen::visit(const mfast::group_field_instruction *inst, void *pIndex) {

for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
const field_instruction *subinst = inst->subinstructions()[i];

out_ << " visitor.visit(" << get_ext_mref_type(subinst) << " ((*this)["
<< i << "]) );\n";
}
Expand Down Expand Up @@ -662,7 +680,7 @@ 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";
}
Expand All @@ -677,7 +695,7 @@ 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_mref_type(subinst) << " ((*this)["
<< i << "]) );\n";
}
Expand Down Expand Up @@ -791,9 +809,17 @@ void inl_gen::visit(const mfast::template_instruction *inst, void *) {

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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the set of the group presence, at the encode time.

<< " visitor.visit(ext_cref_group);\n"
<< " }\n";
}
else
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)[" << i << "]) );\n";
}

out_ << "}\n\n";
Expand Down Expand Up @@ -842,7 +868,7 @@ void inl_gen::visit(const mfast::template_instruction *inst, void *) {

for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
const field_instruction *subinst = inst->subinstructions()[i];
;

out_ << " visitor.visit(" << get_ext_mref_type(subinst) << " ((*this)["
<< i << "]) );\n";
}
Expand Down
1 change: 0 additions & 1 deletion src/mfast/aggregate_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class aggregate_cref {
this->instruction_ = other.instruction_;
this->storage_array_ = other.storage_array_;
}
bool content() const { return storage_array_->of_group.content_ != nullptr; }

class iterator
: public boost::iterator_facade<iterator, field_cref,
Expand Down
8 changes: 7 additions & 1 deletion src/mfast/ext_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,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() || base_.content(); }
bool present() const { return !this->optional() || group_present_; }

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

private:
cref_type base_;
bool group_present_ = true;
};

template <typename Properties>
Expand All @@ -212,8 +215,11 @@ class ext_cref<nested_message_cref, group_type_tag, Properties>
cref_type get() const { return cref_type(aggregate_cref(base_)[0]); }
bool present() const { return !this->optional() || base_.present(); }

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

private:
field_cref base_;
bool group_present_ = true;
};

///////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/mfast/value_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ union MFAST_EXPORT value_storage {
void defined(bool v) { of_array.defined_bit_ = v; }
bool is_empty() const { return of_array.len_ == 0; }
void present(bool p) { of_array.len_ = p; }
bool is_present() const { return of_group.present_ == 1; }
uint32_t array_length() const {
return of_array.len_ == 0 ? 0 : of_array.len_ - 1;
};
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ FASTTYPEGEN_TARGET(simple_types9 simple9.xml)
FASTTYPEGEN_TARGET(simple_types10 simple10.xml)
FASTTYPEGEN_TARGET(simple_types11 simple11.xml)
FASTTYPEGEN_TARGET(simple_types12 simple12.xml)
FASTTYPEGEN_TARGET(simple_types13 simple13.xml)


FASTTYPEGEN_TARGET(test_types1 test1.xml test2.xml)
Expand All @@ -41,6 +42,8 @@ add_executable (mfast_test
encoder_decoder_test.cpp
encoder_decoder_test_v2.cpp
field_comparator_test.cpp
group_encoder_decoder_v2.cpp
group_encoder_decoder.cpp
coder_test.cpp
value_storage_test.cpp
${FASTTYPEGEN_test_types1_OUTPUTS}
Expand All @@ -60,6 +63,7 @@ add_executable (mfast_test
${FASTTYPEGEN_simple_types10_OUTPUTS}
${FASTTYPEGEN_simple_types11_OUTPUTS}
${FASTTYPEGEN_simple_types12_OUTPUTS}
${FASTTYPEGEN_simple_types13_OUTPUTS}
fast_type_gen_test.cpp
dictionary_builder_test.cpp
json_test.cpp
Expand Down
68 changes: 13 additions & 55 deletions tests/encoder_decoder_test.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,16 @@
#include "catch.hpp"
#include <mfast.h>
#include <mfast/coder/fast_encoder.h>
#include <mfast/coder/fast_decoder.h>

#include "fast_test_coding_case.hpp"
#include "byte_stream.h"

#include "simple12.h"

namespace
{
class fast_test_coding_case
{
public:
fast_test_coding_case()
{
encoder_.include({simple12::description()});
decoder_.include({simple12::description()});
}

bool encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
{
const int buffer_size = 128;
char buffer[buffer_size];

std::size_t encoded_size = encoder_.encode(msg_ref,
buffer,
buffer_size,
reset);

if (result == byte_stream(buffer, encoded_size))
return true;

std::cout << byte_stream(buffer, encoded_size);

INFO( "Got \"" << byte_stream(buffer, encoded_size) << "\" instead." );
return 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_.decode(first, first+bytes.size(), reset);

return (msg == result);
}

private:
mfast::fast_encoder encoder_;
mfast::fast_decoder decoder_;
};
}
using namespace test::coding;

TEST_CASE("simple field and sequence optional encoder","[field_sequence_optional_sequence_encoder]")
TEST_CASE("simple field and sequence optional encoder/decoder","[field_sequence_optional_encoder_decoder]")
{
fast_test_coding_case test_case;
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode field")
{
Expand Down Expand Up @@ -110,9 +68,9 @@ TEST_CASE("simple field and sequence optional encoder","[field_sequence_optional
}
}

TEST_CASE("simple field encoder","[field_optional_encoder]")
TEST_CASE("simple field encoder/decoder","[field_optional_encoder_decoder]")
{
fast_test_coding_case test_case;
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode fields")
{
Expand All @@ -130,9 +88,9 @@ TEST_CASE("simple field encoder","[field_optional_encoder]")
}
}

TEST_CASE("simple field and sequence encoder","[field_sequence_encoder]")
TEST_CASE("simple field and sequence encoder/decoder","[field_sequence_encoder_decoder]")
{
fast_test_coding_case test_case;
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode fields")
{
Expand All @@ -156,9 +114,9 @@ TEST_CASE("simple field and sequence encoder","[field_sequence_encoder]")
}
}

TEST_CASE("simple field optional encoder","[field_optional_encoder]")
TEST_CASE("simple field optional encoder/decoder","[field_optional_encoder_decoder]")
{
fast_test_coding_case test_case;
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode fields")
{
Expand Down Expand Up @@ -204,9 +162,9 @@ TEST_CASE("simple field optional encoder","[field_optional_encoder]")
}
}

TEST_CASE("simple field group optional encoder","[field_group_optional_encoder]")
TEST_CASE("simple field group optional encoder/decoder","[field_group_optional_encoder_decoder]")
{
fast_test_coding_case test_case;
fast_test_coding_case<simple12::templates_description> test_case;

SECTION("encode field")
{
Expand Down
Loading