Skip to content

Commit de4176b

Browse files
committed
C++: Always reset optional fields
1 parent 655ccf8 commit de4176b

3 files changed

Lines changed: 56 additions & 36 deletions

File tree

internal/generator/c/templates/binding-cpp.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ var CppBindingTemplate = template.Must(template.New("binding-cpp").Funcs(funcMap
3636
#include <cmath>
3737
{{end -}}
3838
{{range $entity := .Model.EntitiesWithMeta}}
39-
{{- range $property := $entity.Properties}}
39+
{{- range $property := $entity.Properties}}
4040
const
41-
{{- if $property.RelationTarget}} obx::RelationProperty<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$property.Meta.CppNameRelationTarget}}>
42-
{{- else}} obx::Property<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, OBXPropertyType_{{PropTypeName $property.Type}}>
43-
{{- end}} {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$property.Meta.CppName}}({{$property.Id.GetId}});
44-
{{- end}}
45-
{{- range $relation := $entity.Relations}}
41+
{{- if $property.RelationTarget}} obx::RelationProperty<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$property.Meta.CppNameRelationTarget}}>
42+
{{- else}} obx::Property<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, OBXPropertyType_{{PropTypeName $property.Type}}>
43+
{{- end}} {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$property.Meta.CppName}}({{$property.Id.GetId}});
44+
{{- end}}
45+
{{- range $relation := $entity.Relations}}
4646
const obx::RelationStandalone<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$relation.Target.Meta.CppNamespacePrefix}}{{$relation.Target.Meta.CppName}}> {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$relation.Meta.CppName}}({{$relation.Id.GetId}});
47-
{{- end}}
47+
{{- end}}
4848
4949
void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo::toFlatBuffer(flatbuffers::FlatBufferBuilder& fbb, const {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}& object) {
5050
fbb.Clear();
@@ -86,8 +86,8 @@ std::unique_ptr<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}> {{$
8686
void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}& outObject) {
8787
const auto* table = flatbuffers::GetRoot<flatbuffers::Table>(data);
8888
assert(table);
89-
{{range $property := $entity.Properties}}
90-
{{- if eq "std::string" $property.Meta.CppType -}}
89+
{{- range $property := $entity.Properties}}
90+
{{- if eq "std::string" $property.Meta.CppType}}
9191
{
9292
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
9393
if (ptr) {
@@ -110,41 +110,53 @@ void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo:
110110
{{- end}}
111111
}
112112
}
113-
{{else if and ( and $.NaNAsNull $property.Meta.Optional ) $property.Meta.FbIsFloatingPoint }}
114-
if (table->CheckField({{$property.FbvTableOffset}})) {
115-
outObject.{{$property.Meta.CppName}}
116-
{{- template "field-value-assign-pre" $property.Meta -}}
117-
table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, {{$property.Meta.FbDefaultValue}}){{if eq "bool" $property.Meta.CppType}} != 0{{end}}
118-
{{- template "field-value-assign-post" $property.Meta}};
119-
} else {
120-
outObject.{{$property.Meta.CppName}}.reset();
121-
}
122-
{{- else}}
123-
{{- if $property.Meta.Optional}}if (table->CheckField({{$property.FbvTableOffset}})) {{end}}
124-
{{- if eq "std::vector<std::string>" $property.Meta.CppType}}{
113+
{{- else if eq "std::vector<std::string>" $property.Meta.CppType}}
114+
{
125115
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>({{$property.FbvTableOffset}});
126116
if (ptr) {
127117
{{- if $property.Meta.Optional}}
128-
outObject.{{$property.Meta.CppName}}{{if IsOptionalPtr $property.Meta.Optional}}.reset(new {{$property.Meta.CppType}}({{else}} = {{$property.Meta.CppType}}(){{end}}{{template "field-value-assign-post" $property.Meta}};{{end}}
118+
outObject.{{$property.Meta.CppName}}{{if IsOptionalPtr $property.Meta.Optional}}.reset(new {{$property.Meta.CppType}}({{else}} = {{$property.Meta.CppType}}(){{end}}{{template "field-value-assign-post" $property.Meta}};
119+
{{- end}}
129120
outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}reserve(ptr->size());
130121
for (flatbuffers::uoffset_t i = 0; i < ptr->size(); i++) {
131122
auto* itemPtr = ptr->Get(i);
132123
if (itemPtr) outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}emplace_back(itemPtr->c_str());
133124
}
125+
} else {
126+
outObject.{{$property.Meta.CppName}}
127+
{{- if $property.Meta.Optional -}}
128+
.reset();
129+
{{- else -}}
130+
.clear();
131+
{{- end}}
134132
}
135-
}{{else if $property.Meta.FbIsVector}}{
133+
}
134+
{{- else if $property.Meta.FbIsVector}}
135+
{
136136
auto* ptr = table->GetPointer<const {{$property.Meta.FbOffsetType}}*>({{$property.FbvTableOffset}});
137137
if (ptr) outObject.{{$property.Meta.CppName}}
138138
{{- if IsOptionalPtr $property.Meta.Optional}}{{template "field-value-assign-pre" $property.Meta}}ptr->begin(), ptr->end(){{template "field-value-assign-post" $property.Meta}}
139139
{{- else if $property.Meta.Optional}} = {{$property.Meta.CppType}}(ptr->begin(), ptr->end())
140140
{{- else}}.assign(ptr->begin(), ptr->end())
141141
{{- end}};
142-
}{{- else}}outObject.{{$property.Meta.CppName}}
143-
{{- template "field-value-assign-pre" $property.Meta -}}
142+
else {
143+
outObject.{{$property.Meta.CppName}}
144+
{{- if $property.Meta.Optional -}}
145+
.reset();
146+
{{- else -}}
147+
.clear();
148+
{{- end}}
149+
}
150+
}
151+
{{- else }}
152+
{{ if $property.Meta.Optional -}}
153+
if (table->CheckField({{$property.FbvTableOffset}})) {{end -}}
154+
outObject.{{$property.Meta.CppName}}
155+
{{- template "field-value-assign-pre" $property.Meta -}}
144156
table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, {{$property.Meta.FbDefaultValue}}){{if eq "bool" $property.Meta.CppType}} != 0{{end}}
145-
{{- template "field-value-assign-post" $property.Meta}};
146-
{{- end}}
147-
{{end}}
157+
{{- template "field-value-assign-post" $property.Meta}};
158+
{{- if $property.Meta.Optional}} else outObject.{{$property.Meta.CppName}}.reset();{{- end}}
159+
{{- end }}
148160
{{- end}}
149161
}
150162
{{end}}

test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,31 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
104104
auto* itemPtr = ptr->Get(i);
105105
if (itemPtr) outObject.stringvector.emplace_back(itemPtr->c_str());
106106
}
107+
} else {
108+
outObject.stringvector.clear();
107109
}
108110
}
109111
outObject.byte = table->GetField<int8_t>(32, 0);
110112
outObject.ubyte = table->GetField<uint8_t>(34, 0);
111113
{
112114
auto* ptr = table->GetPointer<const flatbuffers::Vector<int8_t>*>(36);
113115
if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end());
116+
else {
117+
outObject.bytevector.clear();
118+
}
114119
}
115120
{
116121
auto* ptr = table->GetPointer<const flatbuffers::Vector<uint8_t>*>(38);
117122
if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end());
123+
else {
124+
outObject.ubytevector.clear();
125+
}
118126
}
119127
outObject.float32 = table->GetField<float>(40, 0.0f);
120128
outObject.float64 = table->GetField<double>(42, 0.0);
121129
outObject.float_ = table->GetField<float>(44, 0.0f);
122130
outObject.double_ = table->GetField<double>(46, 0.0);
123131
outObject.relId = table->GetField<obx_id>(48, 0);
124-
125132
}
126133

127134
const obx::Property<ns::Annotated, OBXPropertyType_Long> ns::Annotated_::identifier(1);
@@ -217,7 +224,6 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
217224
}
218225
}
219226
outObject.uid = table->GetField<int32_t>(20, 0);
220-
221227
}
222228

223229
const obx::Property<ns::TSDate, OBXPropertyType_Long> ns::TSDate_::id(1);
@@ -250,7 +256,6 @@ void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSD
250256
assert(table);
251257
outObject.id = table->GetField<obx_id>(4, 0);
252258
outObject.timestamp = table->GetField<int64_t>(6, 0);
253-
254259
}
255260

256261
const obx::Property<ns::TSDateNano, OBXPropertyType_Long> ns::TSDateNano_::id(1);
@@ -283,6 +288,5 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:
283288
assert(table);
284289
outObject.id = table->GetField<obx_id>(4, 0);
285290
outObject.timestamp = table->GetField<int64_t>(6, 0);
286-
287291
}
288292

test/comparison/testdata/fbs/typeful/cpp11/schema.obx.cpp.expected

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,31 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
104104
auto* itemPtr = ptr->Get(i);
105105
if (itemPtr) outObject.stringvector.emplace_back(itemPtr->c_str());
106106
}
107+
} else {
108+
outObject.stringvector.clear();
107109
}
108110
}
109111
outObject.byte = table->GetField<int8_t>(32, 0);
110112
outObject.ubyte = table->GetField<uint8_t>(34, 0);
111113
{
112114
auto* ptr = table->GetPointer<const flatbuffers::Vector<int8_t>*>(36);
113115
if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end());
116+
else {
117+
outObject.bytevector.clear();
118+
}
114119
}
115120
{
116121
auto* ptr = table->GetPointer<const flatbuffers::Vector<uint8_t>*>(38);
117122
if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end());
123+
else {
124+
outObject.ubytevector.clear();
125+
}
118126
}
119127
outObject.float32 = table->GetField<float>(40, 0.0f);
120128
outObject.float64 = table->GetField<double>(42, 0.0);
121129
outObject.float_ = table->GetField<float>(44, 0.0f);
122130
outObject.double_ = table->GetField<double>(46, 0.0);
123131
outObject.relId = table->GetField<obx_id>(48, 0);
124-
125132
}
126133

127134
const obx::Property<ns::Annotated, OBXPropertyType_Long> ns::Annotated_::identifier(1);
@@ -217,7 +224,6 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
217224
}
218225
}
219226
outObject.uid = table->GetField<int32_t>(20, 0);
220-
221227
}
222228

223229
const obx::Property<ns::TSDate, OBXPropertyType_Long> ns::TSDate_::id(1);
@@ -250,7 +256,6 @@ void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSD
250256
assert(table);
251257
outObject.id = table->GetField<obx_id>(4, 0);
252258
outObject.timestamp = table->GetField<int64_t>(6, 0);
253-
254259
}
255260

256261
const obx::Property<ns::TSDateNano, OBXPropertyType_Long> ns::TSDateNano_::id(1);
@@ -283,6 +288,5 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:
283288
assert(table);
284289
outObject.id = table->GetField<obx_id>(4, 0);
285290
outObject.timestamp = table->GetField<int64_t>(6, 0);
286-
287291
}
288292

0 commit comments

Comments
 (0)