summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAustin Schuh <AustinSchuh@users.noreply.github.com>2020-12-07 11:49:24 -0800
committerGitHub <noreply@github.com>2020-12-07 11:49:24 -0800
commitfd4c1b5ff7aca18220ddb0d3b82fd831924533ba (patch)
tree16759484ed0478d8640ebf5b28f5701f6b6347dc /include
parentbc7eb8adeb1ce73b13bacdf7a6904de27a1e5913 (diff)
downloadflatbuffers-fd4c1b5ff7aca18220ddb0d3b82fd831924533ba.tar.gz
flatbuffers-fd4c1b5ff7aca18220ddb0d3b82fd831924533ba.tar.bz2
flatbuffers-fd4c1b5ff7aca18220ddb0d3b82fd831924533ba.zip
Replace std::string and const char* CreateSharedString with string_view (#6315)
It is useful to be able to call CreateSharedString with a string_view. A string_view can be implicitly converted from a std::string or a const char*. This means if string_view is available, we can use it instead of both other functions and get all 3.
Diffstat (limited to 'include')
-rw-r--r--include/flatbuffers/flatbuffers.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h
index 1f25d4eb..12fc64c0 100644
--- a/include/flatbuffers/flatbuffers.h
+++ b/include/flatbuffers/flatbuffers.h
@@ -1611,6 +1611,16 @@ class FlatBufferBuilder {
return off;
}
+#ifdef FLATBUFFERS_HAS_STRING_VIEW
+ /// @brief Store a string in the buffer, which can contain any binary data.
+ /// If a string with this exact contents has already been serialized before,
+ /// instead simply returns the offset of the existing string.
+ /// @param[in] str A const std::string_view to store in the buffer.
+ /// @return Returns the offset in the buffer where the string starts
+ Offset<String> CreateSharedString(const flatbuffers::string_view str) {
+ return CreateSharedString(str.data(), str.size());
+ }
+#else
/// @brief Store a string in the buffer, which null-terminated.
/// If a string with this exact contents has already been serialized before,
/// instead simply returns the offset of the existing string.
@@ -1628,6 +1638,7 @@ class FlatBufferBuilder {
Offset<String> CreateSharedString(const std::string &str) {
return CreateSharedString(str.c_str(), str.length());
}
+#endif
/// @brief Store a string in the buffer, which can contain any binary data.
/// If a string with this exact contents has already been serialized before,