diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index aa9cf4fca..e38e55280 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1197,6 +1197,24 @@ FLATBUFFERS_FINAL_CLASS return Offset>(EndVector(len)); } + /// @brief Serialize an array of native structs into a FlatBuffer `vector`. + /// @tparam T The data type of the struct array elements. + /// @tparam S The data type of the native struct array elements. + /// @param[in] v A pointer to the array of type `S` to serialize into the + /// buffer as a `vector`. + /// @param[in] len The number of elements to serialize. + /// @return Returns a typed `Offset` into the serialized data indicating + /// where the vector is stored. + template Offset> CreateVectorOfNativeStructs( + const S *v, size_t len) { + extern T Pack(const S&); + typedef T (*Pack_t)(const S&); + std::vector vv(len); + std::transform(v, v+len, vv.begin(), *(Pack_t)&Pack); + return CreateVectorOfStructs(vv.data(), vv.size()); + } + + #ifndef FLATBUFFERS_CPP98_STL /// @brief Serialize an array of structs into a FlatBuffer `vector`. /// @tparam T The data type of the struct array elements. @@ -1229,6 +1247,19 @@ FLATBUFFERS_FINAL_CLASS return CreateVectorOfStructs(data(v), v.size()); } + /// @brief Serialize a `std::vector` of native structs into a FlatBuffer `vector`. + /// @tparam T The data type of the `std::vector` struct elements. + /// @tparam S The data type of the `std::vector` native struct elements. + /// @param[in]] v A const reference to the `std::vector` of structs to + /// serialize into the buffer as a `vector`. + /// @return Returns a typed `Offset` into the serialized data indicating + /// where the vector is stored. + template Offset> CreateVectorOfNativeStructs( + const std::vector &v) { + return CreateVectorOfNativeStructs(data(v), v.size()); + } + + /// @cond FLATBUFFERS_INTERNAL template struct StructKeyComparator { @@ -1253,6 +1284,19 @@ FLATBUFFERS_FINAL_CLASS return CreateVectorOfSortedStructs(data(*v), v->size()); } + /// @brief Serialize a `std::vector` of native structs into a FlatBuffer `vector` + /// in sorted order. + /// @tparam T The data type of the `std::vector` struct elements. + /// @tparam S The data type of the `std::vector` native struct elements. + /// @param[in]] v A const reference to the `std::vector` of structs to + /// serialize into the buffer as a `vector`. + /// @return Returns a typed `Offset` into the serialized data indicating + /// where the vector is stored. + template Offset> CreateVectorOfSortedNativeStructs( + std::vector *v) { + return CreateVectorOfSortedNativeStructs(data(*v), v->size()); + } + /// @brief Serialize an array of structs into a FlatBuffer `vector` in sorted /// order. /// @tparam T The data type of the struct array elements. @@ -1267,6 +1311,24 @@ FLATBUFFERS_FINAL_CLASS return CreateVectorOfStructs(v, len); } + /// @brief Serialize an array of native structs into a FlatBuffer `vector` in sorted + /// order. + /// @tparam T The data type of the struct array elements. + /// @tparam S The data type of the native struct array elements. + /// @param[in] v A pointer to the array of type `S` to serialize into the + /// buffer as a `vector`. + /// @param[in] len The number of elements to serialize. + /// @return Returns a typed `Offset` into the serialized data indicating + /// where the vector is stored. + template Offset> CreateVectorOfSortedNativeStructs( + S *v, size_t len) { + extern T Pack(const S&); + typedef T (*Pack_t)(const S&); + std::vector vv(len); + std::transform(v, v+len, vv.begin(), *(Pack_t)&Pack); + return CreateVectorOfSortedStructs(vv, len); + } + /// @cond FLATBUFFERS_INTERNAL template struct TableKeyComparator { diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 48751f405..6192212c9 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1753,7 +1753,15 @@ class CppGenerator : public BaseGenerator { } case BASE_TYPE_STRUCT: { if (IsStruct(vector_type)) { - code += "_fbb.CreateVectorOfStructs(" + value + ")"; + auto native_type = + field.value.type.struct_def->attributes.Lookup("native_type"); + if (native_type) { + code += "_fbb.CreateVectorOfNativeStructs<"; + code += WrapInNameSpace(*vector_type.struct_def) + ">"; + } else { + code += "_fbb.CreateVectorOfStructs"; + } + code += "(" + value + ")"; } else { code += "_fbb.CreateVector>";