From 7f663b12048d3853c46c43a4d88211ad9900ee79 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Mon, 11 Apr 2022 17:02:19 -0700 Subject: [PATCH] Allow CreateVectorOfStrings() to work with any string-type. (#7238) Any string type that is supported by CreateString(), e.g. const char* or string_view will now also work. Signed-off-by: Henner Zeller --- include/flatbuffers/flatbuffer_builder.h | 9 ++++++--- tests/test.cpp | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h index 999419383..d34b4fc21 100644 --- a/include/flatbuffers/flatbuffer_builder.h +++ b/include/flatbuffers/flatbuffer_builder.h @@ -716,15 +716,18 @@ class FlatBufferBuilder { return CreateVector(elems); } - /// @brief Serialize a `std::vector` into a FlatBuffer `vector`. + /// @brief Serialize a `std::vector` into a FlatBuffer `vector`. + /// whereas StringType is any type that is accepted by the CreateString() + /// overloads. /// This is a convenience function for a common case. /// @param v A const reference to the `std::vector` to serialize into the /// buffer as a `vector`. /// @return Returns a typed `Offset` into the serialized data indicating /// where the vector is stored. - template> + template> Offset>> CreateVectorOfStrings( - const std::vector &v) { + const std::vector &v) { return CreateVectorOfStrings(v.cbegin(), v.cend()); } diff --git a/tests/test.cpp b/tests/test.cpp index c87ca85e3..06d6a08fa 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -136,6 +136,19 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) { names2.push_back("mary"); auto vecofstrings2 = builder.CreateVectorOfStrings(names2); + // Creating vectors from types that are different from std::string + std::vector names3; + names3.push_back("foo"); + names3.push_back("bar"); + builder.CreateVectorOfStrings(names3); // Also an accepted type + +#ifdef FLATBUFFERS_HAS_STRING_VIEW + std::vector names4; + names3.push_back("baz"); + names3.push_back("quux"); + builder.CreateVectorOfStrings(names4); // Also an accepted type +#endif + // Create many vectors of strings std::vector manyNames; for (auto i = 0; i < 100; i++) { manyNames.push_back("john_doe"); }