diff options
author | Derek Bailey <derekbailey@google.com> | 2022-02-09 22:16:46 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-09 22:16:46 -0800 |
commit | faadbc10ea4b77bc57960696ef0d1d053536c68a (patch) | |
tree | 7f6bd06a801620e5c550e0f5e35e3eb0b159b456 /include | |
parent | ed6ae8d3221be494247cdb7daeeb156707fc9bc0 (diff) | |
download | flatbuffers-faadbc10ea4b77bc57960696ef0d1d053536c68a.tar.gz flatbuffers-faadbc10ea4b77bc57960696ef0d1d053536c68a.tar.bz2 flatbuffers-faadbc10ea4b77bc57960696ef0d1d053536c68a.zip |
Add CreateVector overload to accept array like (#7095)
Diffstat (limited to 'include')
-rw-r--r-- | include/flatbuffers/flatbuffer_builder.h | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h index 3c4c9647..99941938 100644 --- a/include/flatbuffers/flatbuffer_builder.h +++ b/include/flatbuffers/flatbuffer_builder.h @@ -485,7 +485,7 @@ class FlatBufferBuilder { return CreateString(str.c_str(), str.length()); } -// clang-format off + // clang-format off #ifdef FLATBUFFERS_HAS_STRING_VIEW /// @brief Store a string in the buffer, which can contain any binary data. /// @param[in] str A const string_view to copy in to the buffer. @@ -625,7 +625,7 @@ class FlatBufferBuilder { AssertScalarT<T>(); StartVector(len, sizeof(T)); if (len == 0) { return Offset<Vector<T>>(EndVector(len)); } -// clang-format off + // clang-format off #if FLATBUFFERS_LITTLEENDIAN PushBytes(reinterpret_cast<const uint8_t *>(v), len * sizeof(T)); #else @@ -641,6 +641,17 @@ class FlatBufferBuilder { return Offset<Vector<T>>(EndVector(len)); } + /// @brief Serialize an array like object into a FlatBuffer `vector`. + /// @tparam T The data type of the array elements. + /// @tparam C The type of the array. + /// @param[in] array A reference to an array like object of type `T` 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, class C> Offset<Vector<T>> CreateVector(const C &array) { + return CreateVector(array.data(), array.size()); + } + template<typename T> Offset<Vector<Offset<T>>> CreateVector(const Offset<T> *v, size_t len) { StartVector(len, sizeof(Offset<T>)); |