7979func cpp_decorate_const_ref(field)
8080 $result = field | @cpp_type
8181
82- if field.is_nullable || field.type == 'string' || field. is_custom_class
82+ if field.is_nullable || field.is_custom_class
8383 $result = 'const ' + $result + '&'
84+ else if field.type == 'string'
85+ $result = 'std::string_view'
8486 end
8587
8688 ret $result
@@ -181,13 +183,22 @@ func cpp_default_value(field)
181183
182184 ret $result
183185end
186+
187+ func cpp_member_initializer(field)
188+ if field.type == 'string' && !field.is_nullable
189+ ret 'std::string(' + field.name + ')'
190+ else
191+ ret field.name
192+ end
193+ end
184194~}}
185195// clang-format off
186196
187197#ifndef __FLATBUFFER_PROTOCOL_H__
188198#define __FLATBUFFER_PROTOCOL_H__
189199
190200#include <string>
201+ #include <string_view>
191202#include <optional>
192203#include <mutex>
193204#include <memory>
@@ -218,7 +229,7 @@ namespace flatbuffers {
218229class option
219230{
220231public:
221- using encoding_func_type = std::function<std::string(const std::string& )>;
232+ using encoding_func_type = std::function<std::string(std::string_view )>;
222233
223234private:
224235 encoding_func_type encoding_func;
@@ -258,7 +269,7 @@ public:
258269 }
259270
260271public:
261- static std::string encode(const std::string& value)
272+ static std::string encode(std::string_view value)
262273 {
263274 auto& ist = get();
264275 if (ist.encoding_func)
@@ -267,12 +278,12 @@ public:
267278 }
268279 else
269280 {
270- return value;
281+ return std::string( value) ;
271282 }
272283 }
273284
274285public:
275- static std::string decode(const std::string& value)
286+ static std::string decode(std::string_view value)
276287 {
277288 auto& ist = get();
278289 if (ist.decoding_func)
@@ -281,7 +292,7 @@ public:
281292 }
282293 else
283294 {
284- return value;
295+ return std::string( value) ;
285296 }
286297 }
287298};
@@ -319,7 +330,7 @@ template <typename T> struct FlatBufferOffset<std::vector<T>> { typedef flatbuff
319330template <typename T> inline static
320331typename FlatBufferOffset<T>::type build(FlatBufferBuilder& builder, const T& value);
321332template <>
322- flatbuffers::Offset<flatbuffers::String> build<std::string>(FlatBufferBuilder& builder, const std::string& value);
333+ inline flatbuffers::Offset<flatbuffers::String> build<std::string>(FlatBufferBuilder& builder, const std::string& value);
323334{{~ for scope in context.scopes ~}}
324335{{~ for table in scope.tables ~}}
325336template <>
@@ -385,7 +396,7 @@ public:
385396
386397{{~ if (table.fields | array.size) > 0 ~}}
387398 {{ table.name }}({{ for field in table.fields }}{{ field | @cpp_decorate_const_ref }} {{ field.name }}{{ if !for.last }}, {{end }}{{ end }})
388- : {{ for field in table.fields }}{{ field.name }}({{ field.name }}){{ if !for.last }}, {{ end }}{{ end }}
399+ : {{ for field in table.fields }}{{ field.name }}({{ field | @cpp_member_initializer }}){{ if !for.last }}, {{ end }}{{ end }}
389400 { }
390401{{~ end ~}}
391402
@@ -465,9 +476,9 @@ typename FlatBufferOffset<T>::type build(FlatBufferBuilder& builder, const T& va
465476}
466477
467478template <>
468- flatbuffers::Offset<flatbuffers::String> build<std::string>(FlatBufferBuilder& builder, const std::string& value)
479+ inline flatbuffers::Offset<flatbuffers::String> build<std::string>(FlatBufferBuilder& builder, const std::string& value)
469480{
470- return builder.CreateString(flatbuffers::option::encode(value));
481+ return builder.CreateString(flatbuffers::option::encode(std::string_view( value) ));
471482}
472483
473484{{~ for scope in context.scopes ~}}
0 commit comments