Skip to content

Commit 3809d54

Browse files
committed
Add code changes to support the optional group for the encoder V2
1 parent 673fa54 commit 3809d54

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/fast_type_gen/inl_gen.cpp

+35-9
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/ext_ref.h

+7-1
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_.present(); }
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

+1
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
};

0 commit comments

Comments
 (0)