@@ -987,7 +987,7 @@ class CppGenerator : public BaseGenerator {
987
987
988
988
std::string UnionVectorVerifySignature (const EnumDef &enum_def) {
989
989
const std::string name = Name (enum_def);
990
- const std::string & type = opts_.scoped_enums ? name : " uint8_t" ;
990
+ const std::string &type = opts_.scoped_enums ? name : " uint8_t" ;
991
991
return " bool Verify" + name + " Vector" +
992
992
" (::flatbuffers::Verifier &verifier, " +
993
993
" const ::flatbuffers::Vector<::flatbuffers::Offset<void>> "
@@ -1807,7 +1807,7 @@ class CppGenerator : public BaseGenerator {
1807
1807
field.value .type .element != BASE_TYPE_UTYPE)) {
1808
1808
auto type = GenTypeNative (field.value .type , false , field);
1809
1809
auto cpp_type = field.attributes .Lookup (" cpp_type" );
1810
- const std::string & full_type =
1810
+ const std::string &full_type =
1811
1811
(cpp_type
1812
1812
? (IsVector (field.value .type )
1813
1813
? " std::vector<" +
@@ -1954,9 +1954,10 @@ class CppGenerator : public BaseGenerator {
1954
1954
if (!initializer_list.empty ()) { initializer_list += " ,\n " ; }
1955
1955
const auto cpp_type = field->attributes .Lookup (" cpp_type" );
1956
1956
const auto cpp_ptr_type = field->attributes .Lookup (" cpp_ptr_type" );
1957
- const std::string & type_name = (cpp_type) ? cpp_type->constant
1958
- : GenTypeNative (type, /* invector*/ false ,
1959
- *field, /* forcopy*/ true );
1957
+ const std::string &type_name =
1958
+ (cpp_type) ? cpp_type->constant
1959
+ : GenTypeNative (type, /* invector*/ false , *field,
1960
+ /* forcopy*/ true );
1960
1961
const bool is_ptr = !(IsStruct (type) && field->native_inline ) ||
1961
1962
(cpp_type && cpp_ptr_type->constant != " naked" );
1962
1963
CodeWriter cw;
@@ -1976,10 +1977,10 @@ class CppGenerator : public BaseGenerator {
1976
1977
if (vec_type.base_type == BASE_TYPE_UTYPE) continue ;
1977
1978
const auto cpp_type = field->attributes .Lookup (" cpp_type" );
1978
1979
const auto cpp_ptr_type = field->attributes .Lookup (" cpp_ptr_type" );
1979
- const std::string & type_name = (cpp_type)
1980
- ? cpp_type->constant
1981
- : GenTypeNative (vec_type, /* invector*/ true ,
1982
- *field, /* forcopy*/ true );
1980
+ const std::string &type_name =
1981
+ (cpp_type) ? cpp_type->constant
1982
+ : GenTypeNative (vec_type, /* invector*/ true , *field ,
1983
+ /* forcopy*/ true );
1983
1984
const bool is_ptr = IsVectorOfPointers (*field) ||
1984
1985
(cpp_type && cpp_ptr_type->constant != " naked" );
1985
1986
CodeWriter cw (" " );
@@ -2733,9 +2734,10 @@ class CppGenerator : public BaseGenerator {
2733
2734
if (!nfn.empty ()) {
2734
2735
code_.SetValue (" CPP_NAME" , nfn);
2735
2736
code_ += " const {{CPP_NAME}} *{{FIELD_NAME}}_nested_root() const {" ;
2737
+ code_ += " const auto _f = {{FIELD_NAME}}();" ;
2736
2738
code_ +=
2737
- " return "
2738
- " ::flatbuffers::GetRoot<{{CPP_NAME}}>({{FIELD_NAME}}()->Data()) ;" ;
2739
+ " return _f ? ::flatbuffers::GetRoot<{{CPP_NAME}}>(_f->Data()) " ;
2740
+ code_ += " : nullptr ;" ;
2739
2741
code_ += " }" ;
2740
2742
}
2741
2743
@@ -2745,9 +2747,9 @@ class CppGenerator : public BaseGenerator {
2745
2747
" const {" ;
2746
2748
// Both Data() and size() are const-methods, therefore call order
2747
2749
// doesn't matter.
2748
- code_ +=
2749
- " return flexbuffers::GetRoot({{FIELD_NAME}}() ->Data(), "
2750
- " {{FIELD_NAME}}()->size() );" ;
2750
+ code_ += " const auto _f = {{FIELD_NAME}}(); " ;
2751
+ code_ += " return _f ? flexbuffers::GetRoot(_f ->Data(), _f->size()) " ;
2752
+ code_ += " : flexbuffers::Reference( );" ;
2751
2753
code_ += " }" ;
2752
2754
}
2753
2755
@@ -2835,8 +2837,9 @@ class CppGenerator : public BaseGenerator {
2835
2837
// Generate code to do force_align for the vector.
2836
2838
if (align > 1 ) {
2837
2839
const auto vtype = field.value .type .VectorType ();
2838
- const std::string & type = IsStruct (vtype) ? WrapInNameSpace (*vtype.struct_def )
2839
- : GenTypeWire (vtype, " " , false );
2840
+ const std::string &type = IsStruct (vtype)
2841
+ ? WrapInNameSpace (*vtype.struct_def )
2842
+ : GenTypeWire (vtype, " " , false );
2840
2843
return " _fbb.ForceVectorAlignment(" + field_size + " , sizeof(" + type +
2841
2844
" ), " + std::to_string (static_cast <long long >(align)) + " );" ;
2842
2845
}
@@ -3357,8 +3360,9 @@ class CppGenerator : public BaseGenerator {
3357
3360
}
3358
3361
case BASE_TYPE_UTYPE: {
3359
3362
value = StripUnionType (value);
3360
- const std::string & type = opts_.scoped_enums ? Name (*field.value .type .enum_def )
3361
- : " uint8_t" ;
3363
+ const std::string &type = opts_.scoped_enums
3364
+ ? Name (*field.value .type .enum_def )
3365
+ : " uint8_t" ;
3362
3366
auto enum_value = " __va->_" + value + " [i].type" ;
3363
3367
if (!opts_.scoped_enums )
3364
3368
enum_value = " static_cast<uint8_t>(" + enum_value + " )" ;
@@ -3424,7 +3428,7 @@ class CppGenerator : public BaseGenerator {
3424
3428
}
3425
3429
} else {
3426
3430
// _o->field ? CreateT(_fbb, _o->field.get(), _rehasher);
3427
- const std::string & type = field.value .type .struct_def ->name ;
3431
+ const std::string &type = field.value .type .struct_def ->name ;
3428
3432
code += value + " ? Create" + type;
3429
3433
code += " (_fbb, " + value;
3430
3434
if (!field.native_inline ) code += GenPtrGet (field);
@@ -3810,7 +3814,7 @@ class CppGenerator : public BaseGenerator {
3810
3814
const auto field_type = GenTypeGet (type, " " , is_array ? " " : " const " ,
3811
3815
is_array ? " " : " &" , true );
3812
3816
auto member = Name (*field) + " _" ;
3813
- const std::string & value =
3817
+ const std::string &value =
3814
3818
is_scalar ? " ::flatbuffers::EndianScalar(" + member + " )" : member;
3815
3819
3816
3820
code_.SetValue (" FIELD_NAME" , Name (*field));
0 commit comments