mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
Fixed CreateVectorOfStructs for native_type (2nd try) (#4276)
* Added support for serializing native_type with CreateVectorOfNativeStructs * Added support for serializing native_type with CreateVectorOfSortedNativeStructs * Added C++ code generation for vectors of native_types
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
fb03f78fb4
commit
33932ceea4
@@ -1197,6 +1197,24 @@ FLATBUFFERS_FINAL_CLASS
|
||||
return Offset<Vector<const T *>>(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<typename T, typename S> Offset<Vector<const T *>> CreateVectorOfNativeStructs(
|
||||
const S *v, size_t len) {
|
||||
extern T Pack(const S&);
|
||||
typedef T (*Pack_t)(const S&);
|
||||
std::vector<T> vv(len);
|
||||
std::transform(v, v+len, vv.begin(), *(Pack_t)&Pack);
|
||||
return CreateVectorOfStructs<T>(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<typename T, typename S> Offset<Vector<const T *>> CreateVectorOfNativeStructs(
|
||||
const std::vector<S> &v) {
|
||||
return CreateVectorOfNativeStructs<T, S>(data(v), v.size());
|
||||
}
|
||||
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
template<typename T>
|
||||
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<typename T, typename S> Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs(
|
||||
std::vector<S> *v) {
|
||||
return CreateVectorOfSortedNativeStructs<T, S>(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<typename T, typename S> Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs(
|
||||
S *v, size_t len) {
|
||||
extern T Pack(const S&);
|
||||
typedef T (*Pack_t)(const S&);
|
||||
std::vector<T> vv(len);
|
||||
std::transform(v, v+len, vv.begin(), *(Pack_t)&Pack);
|
||||
return CreateVectorOfSortedStructs<T>(vv, len);
|
||||
}
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
template<typename T>
|
||||
struct TableKeyComparator {
|
||||
|
||||
@@ -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<flatbuffers::Offset<";
|
||||
code += WrapInNameSpace(*vector_type.struct_def) + ">>";
|
||||
|
||||
Reference in New Issue
Block a user