mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 07:38:18 +00:00
Add utility method to build a vector of struct in-place. (#4153)
Change-Id: I6df195cbae621cf2bf6b4f3b56f68be80dc23152
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
6cc2307c71
commit
2dd6ba57d1
@@ -1128,6 +1128,27 @@ FLATBUFFERS_FINAL_CLASS
|
|||||||
return Offset<Vector<const T *>>(EndVector(len));
|
return Offset<Vector<const T *>>(EndVector(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef FLATBUFFERS_CPP98_STL
|
||||||
|
/// @brief Serialize an array of structs into a FlatBuffer `vector`.
|
||||||
|
/// @tparam T The data type of the struct array elements.
|
||||||
|
/// @param[in] f A function that takes the current iteration 0..vector_size-1
|
||||||
|
/// and a pointer to the struct that must be filled.
|
||||||
|
/// @return Returns a typed `Offset` into the serialized data indicating
|
||||||
|
/// where the vector is stored.
|
||||||
|
/// This is mostly useful when flatbuffers are generated with mutation
|
||||||
|
/// accessors.
|
||||||
|
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
|
||||||
|
size_t vector_size, const std::function<void(size_t i, T *)> &filler) {
|
||||||
|
StartVector(vector_size * sizeof(T) / AlignOf<T>(), AlignOf<T>());
|
||||||
|
T *structs = reinterpret_cast<T *>(buf_.make_space(vector_size * sizeof(T)));
|
||||||
|
for (size_t i = 0; i < vector_size; i++) {
|
||||||
|
filler(i, structs);
|
||||||
|
structs++;
|
||||||
|
}
|
||||||
|
return Offset<Vector<const T *>>(EndVector(vector_size));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`.
|
/// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`.
|
||||||
/// @tparam T The data type of the `std::vector` struct elements.
|
/// @tparam T The data type of the `std::vector` struct elements.
|
||||||
/// @param[in]] v A const reference to the `std::vector` of structs to
|
/// @param[in]] v A const reference to the `std::vector` of structs to
|
||||||
|
|||||||
Reference in New Issue
Block a user