Skip to content

Commit 2627c36

Browse files
Merge pull request #144 from martinfantini/group_encoder_error_v2
Group optional encoder_v2
2 parents 7014c9e + 3809d54 commit 2627c36

12 files changed

Lines changed: 290 additions & 122 deletions

src/fast_type_gen/inl_gen.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ std::string get_properties_type(const Instruction *inst) {
2525
using namespace mfast;
2626
struct ext_cref_type_getter : mfast::field_instruction_visitor {
2727
std::stringstream out_;
28+
bool is_group_type_ = false;
2829

2930
public:
3031
ext_cref_type_getter() {}
3132

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

3436
virtual void visit(const int32_field_instruction *inst, void *) override {
3537
out_ << "ext_cref<int32_cref, " << get_operator_tag(inst) << ", "
@@ -83,6 +85,7 @@ struct ext_cref_type_getter : mfast::field_instruction_visitor {
8385
virtual void visit(const group_field_instruction *inst, void *) override {
8486
out_ << "ext_cref< " << codegen_base::cpp_name(inst)
8587
<< "_cref, group_type_tag, " << get_properties_type(inst) << ">";
88+
is_group_type_ = true;
8689
}
8790

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

138+
bool is_group_type(const field_instruction *inst) {
139+
ext_cref_type_getter getter;
140+
inst->accept(getter, nullptr);
141+
return getter.group_type();
142+
}
143+
135144
void ext_cref_type_getter::visit(const sequence_field_instruction *inst,
136145
void *) {
137146
const uint32_field_instruction *length_inst = inst->length_instruction();
@@ -473,9 +482,17 @@ void inl_gen::visit(const mfast::group_field_instruction *inst, void *pIndex) {
473482

474483
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
475484
const field_instruction *subinst = inst->subinstructions()[i];
476-
;
477-
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)["
478-
<< i << "]) );\n";
485+
486+
if (is_group_type(subinst) && subinst->optional())
487+
{
488+
out_ << " {\n"
489+
<< " " << get_ext_cref_type(subinst) << " ext_cref_group((*this)[" << i << "]);\n"
490+
<< " ext_cref_group.set_group_present(this->field_storage(" << i << ")->is_present());\n"
491+
<< " visitor.visit(ext_cref_group);\n"
492+
<< " }\n";
493+
}
494+
else
495+
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)[" << i << "]) );\n";
479496
}
480497

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

509526
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
510527
const field_instruction *subinst = inst->subinstructions()[i];
528+
511529
out_ << " visitor.visit(" << get_ext_mref_type(subinst) << " ((*this)["
512530
<< i << "]) );\n";
513531
}
@@ -662,7 +680,7 @@ void inl_gen::visit(const mfast::sequence_field_instruction *inst,
662680

663681
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
664682
const field_instruction *subinst = inst->subinstructions()[i];
665-
;
683+
666684
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)["
667685
<< i << "]) );\n";
668686
}
@@ -677,7 +695,7 @@ void inl_gen::visit(const mfast::sequence_field_instruction *inst,
677695

678696
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
679697
const field_instruction *subinst = inst->subinstructions()[i];
680-
;
698+
681699
out_ << " visitor.visit(" << get_ext_mref_type(subinst) << " ((*this)["
682700
<< i << "]) );\n";
683701
}
@@ -791,9 +809,17 @@ void inl_gen::visit(const mfast::template_instruction *inst, void *) {
791809

792810
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
793811
const field_instruction *subinst = inst->subinstructions()[i];
794-
;
795-
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)["
796-
<< i << "]) );\n";
812+
813+
if (is_group_type(subinst) && subinst->optional())
814+
{
815+
out_ << " {\n"
816+
<< " " << get_ext_cref_type(subinst) << " ext_cref_group((*this)[" << i << "]);\n"
817+
<< " ext_cref_group.set_group_present(this->field_storage(" << i << ")->is_present());\n"
818+
<< " visitor.visit(ext_cref_group);\n"
819+
<< " }\n";
820+
}
821+
else
822+
out_ << " visitor.visit(" << get_ext_cref_type(subinst) << " ((*this)[" << i << "]) );\n";
797823
}
798824

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

843869
for (std::size_t i = 0; i < inst->subinstructions().size(); ++i) {
844870
const field_instruction *subinst = inst->subinstructions()[i];
845-
;
871+
846872
out_ << " visitor.visit(" << get_ext_mref_type(subinst) << " ((*this)["
847873
<< i << "]) );\n";
848874
}

src/mfast/aggregate_ref.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class aggregate_cref {
5757
this->instruction_ = other.instruction_;
5858
this->storage_array_ = other.storage_array_;
5959
}
60-
bool content() const { return storage_array_->of_group.content_ != nullptr; }
6160

6261
class iterator
6362
: public boost::iterator_facade<iterator, field_cref,

src/mfast/ext_ref.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,13 @@ class ext_cref<BaseCRef, group_type_tag, Properties>
194194
explicit ext_cref(const field_cref &base) : base_(base) {}
195195
explicit ext_cref(const aggregate_cref &base) : base_(base) {}
196196
cref_type get() const { return base_; }
197-
bool present() const { return !this->optional() || base_.content(); }
197+
bool present() const { return !this->optional() || group_present_; }
198+
199+
void set_group_present(bool present) { group_present_ = present; }
198200

199201
private:
200202
cref_type base_;
203+
bool group_present_ = true;
201204
};
202205

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

218+
void set_group_present(bool present) { group_present_ = present; }
219+
215220
private:
216221
field_cref base_;
222+
bool group_present_ = true;
217223
};
218224

219225
///////////////////////////////////////////////////////////////

src/mfast/value_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ union MFAST_EXPORT value_storage {
107107
void defined(bool v) { of_array.defined_bit_ = v; }
108108
bool is_empty() const { return of_array.len_ == 0; }
109109
void present(bool p) { of_array.len_ = p; }
110+
bool is_present() const { return of_group.present_ == 1; }
110111
uint32_t array_length() const {
111112
return of_array.len_ == 0 ? 0 : of_array.len_ - 1;
112113
};

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ FASTTYPEGEN_TARGET(simple_types9 simple9.xml)
2121
FASTTYPEGEN_TARGET(simple_types10 simple10.xml)
2222
FASTTYPEGEN_TARGET(simple_types11 simple11.xml)
2323
FASTTYPEGEN_TARGET(simple_types12 simple12.xml)
24+
FASTTYPEGEN_TARGET(simple_types13 simple13.xml)
2425

2526

2627
FASTTYPEGEN_TARGET(test_types1 test1.xml test2.xml)
@@ -41,6 +42,8 @@ add_executable (mfast_test
4142
encoder_decoder_test.cpp
4243
encoder_decoder_test_v2.cpp
4344
field_comparator_test.cpp
45+
group_encoder_decoder_v2.cpp
46+
group_encoder_decoder.cpp
4447
coder_test.cpp
4548
value_storage_test.cpp
4649
${FASTTYPEGEN_test_types1_OUTPUTS}
@@ -60,6 +63,7 @@ add_executable (mfast_test
6063
${FASTTYPEGEN_simple_types10_OUTPUTS}
6164
${FASTTYPEGEN_simple_types11_OUTPUTS}
6265
${FASTTYPEGEN_simple_types12_OUTPUTS}
66+
${FASTTYPEGEN_simple_types13_OUTPUTS}
6367
fast_type_gen_test.cpp
6468
dictionary_builder_test.cpp
6569
json_test.cpp

tests/encoder_decoder_test.cpp

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,16 @@
11
#include "catch.hpp"
22
#include <mfast.h>
3-
#include <mfast/coder/fast_encoder.h>
4-
#include <mfast/coder/fast_decoder.h>
3+
4+
#include "fast_test_coding_case.hpp"
55
#include "byte_stream.h"
66

77
#include "simple12.h"
88

9-
namespace
10-
{
11-
class fast_test_coding_case
12-
{
13-
public:
14-
fast_test_coding_case()
15-
{
16-
encoder_.include({simple12::description()});
17-
decoder_.include({simple12::description()});
18-
}
19-
20-
bool encoding(const mfast::message_cref& msg_ref, const byte_stream& result, bool reset=false)
21-
{
22-
const int buffer_size = 128;
23-
char buffer[buffer_size];
24-
25-
std::size_t encoded_size = encoder_.encode(msg_ref,
26-
buffer,
27-
buffer_size,
28-
reset);
29-
30-
if (result == byte_stream(buffer, encoded_size))
31-
return true;
32-
33-
std::cout << byte_stream(buffer, encoded_size);
34-
35-
INFO( "Got \"" << byte_stream(buffer, encoded_size) << "\" instead." );
36-
return false;
37-
}
38-
39-
bool decoding(const byte_stream& bytes, const mfast::message_cref& result, bool reset=false)
40-
{
41-
const char* first = bytes.data();
42-
mfast::message_cref msg = decoder_.decode(first, first+bytes.size(), reset);
43-
44-
return (msg == result);
45-
}
46-
47-
private:
48-
mfast::fast_encoder encoder_;
49-
mfast::fast_decoder decoder_;
50-
};
51-
}
9+
using namespace test::coding;
5210

53-
TEST_CASE("simple field and sequence optional encoder","[field_sequence_optional_sequence_encoder]")
11+
TEST_CASE("simple field and sequence optional encoder/decoder","[field_sequence_optional_encoder_decoder]")
5412
{
55-
fast_test_coding_case test_case;
13+
fast_test_coding_case<simple12::templates_description> test_case;
5614

5715
SECTION("encode field")
5816
{
@@ -110,9 +68,9 @@ TEST_CASE("simple field and sequence optional encoder","[field_sequence_optional
11068
}
11169
}
11270

113-
TEST_CASE("simple field encoder","[field_optional_encoder]")
71+
TEST_CASE("simple field encoder/decoder","[field_optional_encoder_decoder]")
11472
{
115-
fast_test_coding_case test_case;
73+
fast_test_coding_case<simple12::templates_description> test_case;
11674

11775
SECTION("encode fields")
11876
{
@@ -130,9 +88,9 @@ TEST_CASE("simple field encoder","[field_optional_encoder]")
13088
}
13189
}
13290

133-
TEST_CASE("simple field and sequence encoder","[field_sequence_encoder]")
91+
TEST_CASE("simple field and sequence encoder/decoder","[field_sequence_encoder_decoder]")
13492
{
135-
fast_test_coding_case test_case;
93+
fast_test_coding_case<simple12::templates_description> test_case;
13694

13795
SECTION("encode fields")
13896
{
@@ -156,9 +114,9 @@ TEST_CASE("simple field and sequence encoder","[field_sequence_encoder]")
156114
}
157115
}
158116

159-
TEST_CASE("simple field optional encoder","[field_optional_encoder]")
117+
TEST_CASE("simple field optional encoder/decoder","[field_optional_encoder_decoder]")
160118
{
161-
fast_test_coding_case test_case;
119+
fast_test_coding_case<simple12::templates_description> test_case;
162120

163121
SECTION("encode fields")
164122
{
@@ -204,9 +162,9 @@ TEST_CASE("simple field optional encoder","[field_optional_encoder]")
204162
}
205163
}
206164

207-
TEST_CASE("simple field group optional encoder","[field_group_optional_encoder]")
165+
TEST_CASE("simple field group optional encoder/decoder","[field_group_optional_encoder_decoder]")
208166
{
209-
fast_test_coding_case test_case;
167+
fast_test_coding_case<simple12::templates_description> test_case;
210168

211169
SECTION("encode field")
212170
{

0 commit comments

Comments
 (0)