diff options
author | Michael <7428276+Urmeli0815@users.noreply.github.com> | 2021-03-18 19:01:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-18 11:01:50 -0700 |
commit | 78f0c0d1d96a220163a03174d3240864a6a139da (patch) | |
tree | b77938a288487e20af8e0279e7cdf11d33313d00 /src | |
parent | c992eafb5b50f1aab9d7863864a0a49fe278836e (diff) | |
download | flatbuffers-78f0c0d1d96a220163a03174d3240864a6a139da.tar.gz flatbuffers-78f0c0d1d96a220163a03174d3240864a6a139da.tar.bz2 flatbuffers-78f0c0d1d96a220163a03174d3240864a6a139da.zip |
[C++] #6501 - Problem when mapping a native type multiple times (#6514)
* [C++] #6501 - Problem when mapping a native type multiple times
- idl.h:
added "native_type_pack_name"
- flatbuffers.h:
added CreateVectorOfNativeStructs variants which receive a pointer to the serialization function
- idl_gen_cpp.cpp:
adapted code generation in case "native_type_pack_name" attribute is present
- extended tests & docs; improved surrounding native_type docs a little
* integrated review feedback
Diffstat (limited to 'src')
-rw-r--r-- | src/idl_gen_cpp.cpp | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 5896ad61..f80af045 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2621,9 +2621,14 @@ class CppGenerator : public BaseGenerator { } case BASE_TYPE_STRUCT: { if (IsStruct(type)) { - auto native_type = type.struct_def->attributes.Lookup("native_type"); + const auto &struct_attrs = type.struct_def->attributes; + const auto native_type = struct_attrs.Lookup("native_type"); if (native_type) { - return "flatbuffers::UnPack(*" + val + ")"; + std::string unpack_call = "flatbuffers::UnPack"; + const auto pack_name = struct_attrs.Lookup("native_type_pack_name"); + if (pack_name) { unpack_call += pack_name->constant; } + unpack_call += "(*" + val + ")"; + return unpack_call; } else if (invector || afield.native_inline) { return "*" + val; } else { @@ -2849,15 +2854,24 @@ class CppGenerator : public BaseGenerator { } case BASE_TYPE_STRUCT: { if (IsStruct(vector_type)) { - auto native_type = - field.value.type.struct_def->attributes.Lookup("native_type"); + const auto &struct_attrs = + field.value.type.struct_def->attributes; + const auto native_type = struct_attrs.Lookup("native_type"); if (native_type) { code += "_fbb.CreateVectorOfNativeStructs<"; - code += WrapInNameSpace(*vector_type.struct_def) + ">"; + code += WrapInNameSpace(*vector_type.struct_def) + ", " + + native_type->constant + ">"; + code += "(" + value; + const auto pack_name = + struct_attrs.Lookup("native_type_pack_name"); + if (pack_name) { + code += ", flatbuffers::Pack" + pack_name->constant; + } + code += ")"; } else { code += "_fbb.CreateVectorOfStructs"; + code += "(" + value + ")"; } - code += "(" + value + ")"; } else { code += "_fbb.CreateVector<flatbuffers::Offset<"; code += WrapInNameSpace(*vector_type.struct_def) + ">> "; @@ -2932,10 +2946,14 @@ class CppGenerator : public BaseGenerator { } case BASE_TYPE_STRUCT: { if (IsStruct(field.value.type)) { - auto native_type = - field.value.type.struct_def->attributes.Lookup("native_type"); + const auto &struct_attribs = field.value.type.struct_def->attributes; + const auto native_type = struct_attribs.Lookup("native_type"); if (native_type) { - code += "flatbuffers::Pack(" + value + ")"; + code += "flatbuffers::Pack"; + const auto pack_name = + struct_attribs.Lookup("native_type_pack_name"); + if (pack_name) { code += pack_name->constant; } + code += "(" + value + ")"; } else if (field.native_inline) { code += "&" + value; } else { |