Skip to content

Commit a9e9c4b

Browse files
committed
C++: Reset string if value ptr is null for value and optional + tests for Empty/NaN as null
1 parent 503210c commit a9e9c4b

6 files changed

Lines changed: 296 additions & 188 deletions

File tree

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,39 @@ void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo:
8787
const auto* table = flatbuffers::GetRoot<flatbuffers::Table>(data);
8888
assert(table);
8989
{{range $property := $entity.Properties}}
90+
{{- if eq "std::string" $property.Meta.CppType -}}
91+
{
92+
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
93+
if (ptr) {
94+
outObject.{{$property.Meta.CppName}}
95+
{{- if $property.Meta.Optional}}
96+
{{- if IsOptionalPtr $.Optional -}}
97+
.reset(new std::string(ptr->c_str(), ptr->size()));
98+
{{- else -}}
99+
.emplace(ptr->c_str(), ptr->size());
100+
{{- end}}
101+
{{- else -}}
102+
.assign(ptr->c_str(), ptr->size());
103+
{{- end}}
104+
} else {
105+
outObject.{{$property.Meta.CppName}}
106+
{{- if $property.Meta.Optional -}}
107+
.reset();
108+
{{- else -}}
109+
.clear();
110+
{{- end}}
111+
}
112+
}
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}}
90123
{{- if $property.Meta.Optional}}if (table->CheckField({{$property.FbvTableOffset}})) {{end}}
91124
{{- if eq "std::vector<std::string>" $property.Meta.CppType}}{
92125
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>({{$property.FbvTableOffset}});
@@ -99,12 +132,6 @@ void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo:
99132
if (itemPtr) outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}emplace_back(itemPtr->c_str());
100133
}
101134
}
102-
}{{else if eq "std::string" $property.Meta.CppType}}{
103-
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
104-
if (ptr) outObject.{{$property.Meta.CppName}}
105-
{{- if $property.Meta.Optional}}{{template "field-value-assign-pre" $property.Meta}}ptr->c_str(){{template "field-value-assign-post" $property.Meta}}
106-
{{- else}}.assign(ptr->c_str())
107-
{{- end}};
108135
}{{else if $property.Meta.FbIsVector}}{
109136
auto* ptr = table->GetPointer<const {{$property.Meta.FbOffsetType}}*>({{$property.FbvTableOffset}});
110137
if (ptr) outObject.{{$property.Meta.CppName}}
@@ -118,6 +145,7 @@ void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo:
118145
{{- template "field-value-assign-post" $property.Meta}};
119146
{{- end}}
120147
{{end}}
148+
{{- end}}
121149
}
122150
{{end}}
123151
`))

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
9090
outObject.bool_ = table->GetField<uint8_t>(26, 0) != 0;
9191
{
9292
auto* ptr = table->GetPointer<const flatbuffers::String*>(28);
93-
if (ptr) outObject.string.assign(ptr->c_str());
93+
if (ptr) {
94+
outObject.string.assign(ptr->c_str(), ptr->size());
95+
} else {
96+
outObject.string.clear();
97+
}
9498
}
9599
{
96100
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>(30);
@@ -172,25 +176,45 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
172176
outObject.identifier = table->GetField<obx_id>(4, 0);
173177
{
174178
auto* ptr = table->GetPointer<const flatbuffers::String*>(6);
175-
if (ptr) outObject.fullName.assign(ptr->c_str());
179+
if (ptr) {
180+
outObject.fullName.assign(ptr->c_str(), ptr->size());
181+
} else {
182+
outObject.fullName.clear();
183+
}
176184
}
177185
outObject.time = table->GetField<int64_t>(8, 0);
178186
outObject.relId = table->GetField<obx_id>(10, 0);
179187
{
180188
auto* ptr = table->GetPointer<const flatbuffers::String*>(12);
181-
if (ptr) outObject.unique.assign(ptr->c_str());
189+
if (ptr) {
190+
outObject.unique.assign(ptr->c_str(), ptr->size());
191+
} else {
192+
outObject.unique.clear();
193+
}
182194
}
183195
{
184196
auto* ptr = table->GetPointer<const flatbuffers::String*>(14);
185-
if (ptr) outObject.uniqueValue.assign(ptr->c_str());
197+
if (ptr) {
198+
outObject.uniqueValue.assign(ptr->c_str(), ptr->size());
199+
} else {
200+
outObject.uniqueValue.clear();
201+
}
186202
}
187203
{
188204
auto* ptr = table->GetPointer<const flatbuffers::String*>(16);
189-
if (ptr) outObject.uniqueHash.assign(ptr->c_str());
205+
if (ptr) {
206+
outObject.uniqueHash.assign(ptr->c_str(), ptr->size());
207+
} else {
208+
outObject.uniqueHash.clear();
209+
}
190210
}
191211
{
192212
auto* ptr = table->GetPointer<const flatbuffers::String*>(18);
193-
if (ptr) outObject.uniqueHash64.assign(ptr->c_str());
213+
if (ptr) {
214+
outObject.uniqueHash64.assign(ptr->c_str(), ptr->size());
215+
} else {
216+
outObject.uniqueHash64.clear();
217+
}
194218
}
195219
outObject.uid = table->GetField<int32_t>(20, 0);
196220

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
9090
outObject.bool_ = table->GetField<uint8_t>(26, 0) != 0;
9191
{
9292
auto* ptr = table->GetPointer<const flatbuffers::String*>(28);
93-
if (ptr) outObject.string.assign(ptr->c_str());
93+
if (ptr) {
94+
outObject.string.assign(ptr->c_str(), ptr->size());
95+
} else {
96+
outObject.string.clear();
97+
}
9498
}
9599
{
96100
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>(30);
@@ -172,25 +176,45 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
172176
outObject.identifier = table->GetField<obx_id>(4, 0);
173177
{
174178
auto* ptr = table->GetPointer<const flatbuffers::String*>(6);
175-
if (ptr) outObject.fullName.assign(ptr->c_str());
179+
if (ptr) {
180+
outObject.fullName.assign(ptr->c_str(), ptr->size());
181+
} else {
182+
outObject.fullName.clear();
183+
}
176184
}
177185
outObject.time = table->GetField<int64_t>(8, 0);
178186
outObject.relId = table->GetField<obx_id>(10, 0);
179187
{
180188
auto* ptr = table->GetPointer<const flatbuffers::String*>(12);
181-
if (ptr) outObject.unique.assign(ptr->c_str());
189+
if (ptr) {
190+
outObject.unique.assign(ptr->c_str(), ptr->size());
191+
} else {
192+
outObject.unique.clear();
193+
}
182194
}
183195
{
184196
auto* ptr = table->GetPointer<const flatbuffers::String*>(14);
185-
if (ptr) outObject.uniqueValue.assign(ptr->c_str());
197+
if (ptr) {
198+
outObject.uniqueValue.assign(ptr->c_str(), ptr->size());
199+
} else {
200+
outObject.uniqueValue.clear();
201+
}
186202
}
187203
{
188204
auto* ptr = table->GetPointer<const flatbuffers::String*>(16);
189-
if (ptr) outObject.uniqueHash.assign(ptr->c_str());
205+
if (ptr) {
206+
outObject.uniqueHash.assign(ptr->c_str(), ptr->size());
207+
} else {
208+
outObject.uniqueHash.clear();
209+
}
190210
}
191211
{
192212
auto* ptr = table->GetPointer<const flatbuffers::String*>(18);
193-
if (ptr) outObject.uniqueHash64.assign(ptr->c_str());
213+
if (ptr) {
214+
outObject.uniqueHash64.assign(ptr->c_str(), ptr->size());
215+
} else {
216+
outObject.uniqueHash64.clear();
217+
}
194218
}
195219
outObject.uid = table->GetField<int32_t>(20, 0);
196220

0 commit comments

Comments
 (0)