From 6e9f5d98102043dcf4005b2dc3d13a43ba3bd562 Mon Sep 17 00:00:00 2001 From: Mika Raento Date: Mon, 4 May 2020 20:33:11 +0300 Subject: [PATCH] Respect shared attribute in Parser (#5885) Respects the 'shared' field attribute when reading data with Parser. --- include/flatbuffers/idl.h | 2 +- src/idl_parser.cpp | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 9236cf4..d73c92d 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -859,7 +859,7 @@ class Parser : public ParserState { const std::string &name, const Type &type, FieldDef **dest); FLATBUFFERS_CHECKED_ERROR ParseField(StructDef &struct_def); - FLATBUFFERS_CHECKED_ERROR ParseString(Value &val); + FLATBUFFERS_CHECKED_ERROR ParseString(Value &val, bool use_string_pooling); FLATBUFFERS_CHECKED_ERROR ParseComma(); FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field, size_t parent_fieldn, diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 76c56de..10ff4a6 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -878,10 +878,14 @@ CheckedError Parser::ParseField(StructDef &struct_def) { return NoError(); } -CheckedError Parser::ParseString(Value &val) { +CheckedError Parser::ParseString(Value &val, bool use_string_pooling) { auto s = attribute_; EXPECT(kTokenStringConstant); - val.constant = NumToString(builder_.CreateString(s).o); + if (use_string_pooling) { + val.constant = NumToString(builder_.CreateSharedString(s).o); + } else { + val.constant = NumToString(builder_.CreateString(s).o); + } return NoError(); } @@ -974,7 +978,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field, val.constant = NumToString(builder_.GetSize()); } } else if (enum_val->union_type.base_type == BASE_TYPE_STRING) { - ECHECK(ParseString(val)); + ECHECK(ParseString(val, field->shared)); } else { FLATBUFFERS_ASSERT(false); } @@ -984,7 +988,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field, ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr)); break; case BASE_TYPE_STRING: { - ECHECK(ParseString(val)); + ECHECK(ParseString(val, field->shared)); break; } case BASE_TYPE_VECTOR: { @@ -3293,6 +3297,7 @@ bool FieldDef::Deserialize(Parser &parser, const reflection::Field *field) { nested_flatbuffer = parser.LookupStruct(nested_qualified_name); if (!nested_flatbuffer) return false; } + shared = attributes.Lookup("shared") != nullptr; DeserializeDoc(doc_comment, field->documentation()); return true; } -- 2.7.4