Skip to content

Commit dd6daa7

Browse files
vglavnyyaardappel
authored andcommitted
Part of #5265, neutral changes (#5281)
1 parent dd85c3b commit dd6daa7

15 files changed

+117
-122
lines changed

docs/source/Schemas.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ Current understood attributes:
324324
Note: currently not guaranteed to have an effect when used with
325325
`--object-api`, since that may allocate objects at alignments less than
326326
what you specify with `force_align`.
327-
- `bit_flags` (on an enum): the values of this field indicate bits,
328-
meaning that any value N specified in the schema will end up
327+
- `bit_flags` (on an unsigned enum): the values of this field indicate bits,
328+
meaning that any unsigned value N specified in the schema will end up
329329
representing 1<<N, or if you don't specify values at all, you'll get
330330
the sequence 1, 2, 4, 8, ...
331331
- `nested_flatbuffer: "table_name"` (on a field): this indicates that the field

include/flatbuffers/idl.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ struct EnumVal {
334334
Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder, const Parser &parser) const;
335335

336336
bool Deserialize(const Parser &parser, const reflection::EnumVal *val);
337+
bool IsZero() const { return 0 == value; }
338+
bool IsNonZero() const { return !IsZero(); }
337339

338340
std::string name;
339341
std::vector<std::string> doc_comment;
@@ -345,9 +347,9 @@ struct EnumDef : public Definition {
345347
EnumDef() : is_union(false), uses_multiple_type_instances(false) {}
346348

347349
EnumVal *ReverseLookup(int64_t enum_idx, bool skip_union_default = true) {
348-
for (auto it = vals.vec.begin() +
350+
for (auto it = Vals().begin() +
349351
static_cast<int>(is_union && skip_union_default);
350-
it != vals.vec.end(); ++it) {
352+
it != Vals().end(); ++it) {
351353
if ((*it)->value == enum_idx) { return *it; }
352354
}
353355
return nullptr;
@@ -357,6 +359,12 @@ struct EnumDef : public Definition {
357359

358360
bool Deserialize(Parser &parser, const reflection::Enum *values);
359361

362+
size_t size() const { return vals.vec.size(); }
363+
364+
const std::vector<EnumVal *> &Vals() const {
365+
return vals.vec;
366+
}
367+
360368
SymbolTable<EnumVal> vals;
361369
bool is_union;
362370
// Type is a union which uses type aliases where at least one type is
@@ -691,17 +699,15 @@ class Parser : public ParserState {
691699
bool ParseFlexBuffer(const char *source, const char *source_filename,
692700
flexbuffers::Builder *builder);
693701

694-
FLATBUFFERS_CHECKED_ERROR InvalidNumber(const char *number,
695-
const std::string &msg);
696-
697702
StructDef *LookupStruct(const std::string &id) const;
698703

699704
std::string UnqualifiedName(std::string fullQualifiedName);
700705

706+
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
707+
701708
private:
702709
void Message(const std::string &msg);
703710
void Warning(const std::string &msg);
704-
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
705711
FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val);
706712
FLATBUFFERS_CHECKED_ERROR Next();
707713
FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark();
@@ -745,7 +751,7 @@ class Parser : public ParserState {
745751
FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef* field);
746752
FLATBUFFERS_CHECKED_ERROR TokenError();
747753
FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string *name, Value &e, bool check_now);
748-
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(Type &type, int64_t *result);
754+
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(const Type &type, std::string *result);
749755
StructDef *LookupCreateStruct(const std::string &name,
750756
bool create_if_new = true,
751757
bool definition = false);

src/idl_gen_cpp.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ class CppGenerator : public BaseGenerator {
785785
struct_def ? (struct_def->fixed ? "ST_STRUCT" : "ST_TABLE")
786786
: (enum_def->is_union ? "ST_UNION" : "ST_ENUM"));
787787
auto num_fields =
788-
struct_def ? struct_def->fields.vec.size() : enum_def->vals.vec.size();
788+
struct_def ? struct_def->fields.vec.size() : enum_def->size();
789789
code_.SetValue("NUM_FIELDS", NumToString(num_fields));
790790
std::vector<std::string> names;
791791
std::vector<Type> types;
@@ -798,13 +798,13 @@ class CppGenerator : public BaseGenerator {
798798
types.push_back(field.value.type);
799799
}
800800
} else {
801-
for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end();
801+
for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
802802
++it) {
803803
const auto &ev = **it;
804804
names.push_back(Name(ev));
805805
types.push_back(enum_def->is_union ? ev.union_type
806806
: Type(enum_def->underlying_type));
807-
if (static_cast<int64_t>(it - enum_def->vals.vec.begin()) != ev.value) {
807+
if (static_cast<int64_t>(it - enum_def->Vals().begin()) != ev.value) {
808808
consecutive_enum_from_zero = false;
809809
}
810810
}
@@ -852,7 +852,7 @@ class CppGenerator : public BaseGenerator {
852852
}
853853
std::string vs;
854854
if (enum_def && !consecutive_enum_from_zero) {
855-
for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end();
855+
for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
856856
++it) {
857857
const auto &ev = **it;
858858
if (!vs.empty()) vs += ", ";
@@ -919,8 +919,7 @@ class CppGenerator : public BaseGenerator {
919919

920920
int64_t anyv = 0;
921921
const EnumVal *minv = nullptr, *maxv = nullptr;
922-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
923-
++it) {
922+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
924923
const auto &ev = **it;
925924

926925
GenComment(ev.doc_comment, " ");
@@ -966,15 +965,14 @@ class CppGenerator : public BaseGenerator {
966965
code_ += "";
967966

968967
// Generate an array of all enumeration values
969-
auto num_fields = NumToString(enum_def.vals.vec.size());
968+
auto num_fields = NumToString(enum_def.size());
970969
code_ += "inline const {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" +
971970
num_fields + "] {";
972971
code_ += " static const {{ENUM_NAME}} values[] = {";
973-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
974-
++it) {
972+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
975973
const auto &ev = **it;
976974
auto value = GetEnumValUse(enum_def, ev);
977-
auto suffix = *it != enum_def.vals.vec.back() ? "," : "";
975+
auto suffix = *it != enum_def.Vals().back() ? "," : "";
978976
code_ += " " + value + suffix;
979977
}
980978
code_ += " };";
@@ -996,8 +994,8 @@ class CppGenerator : public BaseGenerator {
996994
code_ += "inline const char * const *EnumNames{{ENUM_NAME}}() {";
997995
code_ += " static const char * const names[] = {";
998996

999-
auto val = enum_def.vals.vec.front()->value;
1000-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
997+
auto val = enum_def.Vals().front()->value;
998+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
1001999
++it) {
10021000
const auto &ev = **it;
10031001
while (val++ != ev.value) { code_ += " \"\","; }
@@ -1032,7 +1030,7 @@ class CppGenerator : public BaseGenerator {
10321030

10331031
code_ += " switch (e) {";
10341032

1035-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1033+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
10361034
++it) {
10371035
const auto &ev = **it;
10381036
code_ += " case " + GetEnumValUse(enum_def, ev) + ": return \"" +
@@ -1048,11 +1046,11 @@ class CppGenerator : public BaseGenerator {
10481046

10491047
// Generate type traits for unions to map from a type to union enum value.
10501048
if (enum_def.is_union && !enum_def.uses_multiple_type_instances) {
1051-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1049+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
10521050
++it) {
10531051
const auto &ev = **it;
10541052

1055-
if (it == enum_def.vals.vec.begin()) {
1053+
if (it == enum_def.Vals().begin()) {
10561054
code_ += "template<typename T> struct {{ENUM_NAME}}Traits {";
10571055
} else {
10581056
auto name = GetUnionElement(ev, true, true);
@@ -1114,10 +1112,10 @@ class CppGenerator : public BaseGenerator {
11141112
code_ += " " + UnionPackSignature(enum_def, true) + ";";
11151113
code_ += "";
11161114

1117-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1115+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
11181116
++it) {
11191117
const auto &ev = **it;
1120-
if (!ev.value) { continue; }
1118+
if (ev.IsZero()) { continue; }
11211119

11221120
const auto native_type =
11231121
NativeName(GetUnionElement(ev, true, true, true),
@@ -1148,11 +1146,11 @@ class CppGenerator : public BaseGenerator {
11481146
code_ += " if (lhs.type != rhs.type) return false;";
11491147
code_ += " switch (lhs.type) {";
11501148

1151-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1149+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
11521150
++it) {
11531151
const auto &ev = **it;
11541152
code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev));
1155-
if (ev.value) {
1153+
if (ev.IsNonZero()) {
11561154
const auto native_type =
11571155
NativeName(GetUnionElement(ev, true, true, true),
11581156
ev.union_type.struct_def, parser_.opts);
@@ -1204,12 +1202,11 @@ class CppGenerator : public BaseGenerator {
12041202

12051203
code_ += "inline " + UnionVerifySignature(enum_def) + " {";
12061204
code_ += " switch (type) {";
1207-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1208-
++it) {
1205+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
12091206
const auto &ev = **it;
12101207
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
12111208

1212-
if (ev.value) {
1209+
if (ev.IsNonZero()) {
12131210
code_.SetValue("TYPE", GetUnionElement(ev, true, true));
12141211
code_ += " case {{LABEL}}: {";
12151212
auto getptr =
@@ -1257,10 +1254,10 @@ class CppGenerator : public BaseGenerator {
12571254
// Generate union Unpack() and Pack() functions.
12581255
code_ += "inline " + UnionUnPackSignature(enum_def, false) + " {";
12591256
code_ += " switch (type) {";
1260-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1257+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
12611258
++it) {
12621259
const auto &ev = **it;
1263-
if (!ev.value) { continue; }
1260+
if (ev.IsZero()) { continue; }
12641261

12651262
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
12661263
code_.SetValue("TYPE", GetUnionElement(ev, true, true));
@@ -1287,10 +1284,10 @@ class CppGenerator : public BaseGenerator {
12871284

12881285
code_ += "inline " + UnionPackSignature(enum_def, false) + " {";
12891286
code_ += " switch (type) {";
1290-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1287+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
12911288
++it) {
12921289
auto &ev = **it;
1293-
if (!ev.value) { continue; }
1290+
if (ev.IsZero()) { continue; }
12941291

12951292
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
12961293
code_.SetValue("TYPE",
@@ -1324,10 +1321,10 @@ class CppGenerator : public BaseGenerator {
13241321
"{{ENUM_NAME}}Union &u) FLATBUFFERS_NOEXCEPT : type(u.type), "
13251322
"value(nullptr) {";
13261323
code_ += " switch (type) {";
1327-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1324+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
13281325
++it) {
13291326
const auto &ev = **it;
1330-
if (!ev.value) { continue; }
1327+
if (ev.IsZero()) { continue; }
13311328
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
13321329
code_.SetValue("TYPE",
13331330
NativeName(GetUnionElement(ev, true, true, true),
@@ -1370,10 +1367,10 @@ class CppGenerator : public BaseGenerator {
13701367

13711368
code_ += "inline void {{ENUM_NAME}}Union::Reset() {";
13721369
code_ += " switch (type) {";
1373-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1370+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
13741371
++it) {
13751372
const auto &ev = **it;
1376-
if (!ev.value) { continue; }
1373+
if (ev.IsZero()) { continue; }
13771374
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
13781375
code_.SetValue("TYPE",
13791376
NativeName(GetUnionElement(ev, true, true, true),
@@ -1834,8 +1831,7 @@ class CppGenerator : public BaseGenerator {
18341831
" template<typename T> "
18351832
"const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;";
18361833

1837-
for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end();
1838-
++u_it) {
1834+
for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) {
18391835
auto &ev = **u_it;
18401836
if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
18411837
auto full_struct_name = GetUnionElement(ev, true, true);
@@ -1967,7 +1963,7 @@ class CppGenerator : public BaseGenerator {
19671963

19681964
code_.SetValue("FIELD_NAME", Name(field));
19691965

1970-
for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end(); ++u_it) {
1966+
for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) {
19711967
auto &ev = **u_it;
19721968
if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
19731969

src/idl_gen_dart.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,19 @@ class DartGenerator : public BaseGenerator {
251251
" static bool containsValue(int value) =>"
252252
" values.containsKey(value);\n\n";
253253

254-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
255-
++it) {
254+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
256255
auto &ev = **it;
257256

258257
if (!ev.doc_comment.empty()) {
259-
if (it != enum_def.vals.vec.begin()) { code += '\n'; }
258+
if (it != enum_def.Vals().begin()) { code += '\n'; }
260259
GenDocComment(ev.doc_comment, &code, "", " ");
261260
}
262261
code += " static const " + name + " " + ev.name + " = ";
263262
code += "const " + name + "._(" + NumToString(ev.value) + ");\n";
264263
}
265264

266265
code += " static get values => {";
267-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
268-
++it) {
266+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
269267
auto &ev = **it;
270268
code += NumToString(ev.value) + ": " + ev.name + ",";
271269
}
@@ -503,8 +501,9 @@ class DartGenerator : public BaseGenerator {
503501
if (field.value.type.base_type == BASE_TYPE_UNION) {
504502
code += " {\n";
505503
code += " switch (" + field_name + "Type?.value) {\n";
506-
for (auto en_it = field.value.type.enum_def->vals.vec.begin() + 1;
507-
en_it != field.value.type.enum_def->vals.vec.end(); ++en_it) {
504+
auto &enum_def = *field.value.type.enum_def;
505+
for (auto en_it = enum_def.Vals().begin() + 1;
506+
en_it != enum_def.Vals().end(); ++en_it) {
508507
auto &ev = **en_it;
509508

510509
auto enum_name = NamespaceAliasFromUnionType(ev.name);

src/idl_gen_fbs.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
9696
else
9797
schema += "enum " + enum_def.name + " : ";
9898
schema += GenType(enum_def.underlying_type, true) + " {\n";
99-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
100-
++it) {
99+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
101100
auto &ev = **it;
102101
GenComment(ev.doc_comment, &schema, nullptr, " ");
103102
if (enum_def.is_union)

src/idl_gen_general.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,7 @@ class GeneralGenerator : public BaseGenerator {
543543
if (lang_.language == IDLOptions::kJava) {
544544
code += " private " + enum_def.name + "() { }\n";
545545
}
546-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
547-
++it) {
546+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
548547
auto &ev = **it;
549548
GenComment(ev.doc_comment, code_ptr, &lang_.comment_config, " ");
550549
if (lang_.language != IDLOptions::kCSharp) {
@@ -574,8 +573,8 @@ class GeneralGenerator : public BaseGenerator {
574573
code += lang_.const_decl;
575574
code += lang_.string_type;
576575
code += "[] names = { ";
577-
auto val = enum_def.vals.vec.front()->value;
578-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
576+
auto val = enum_def.Vals().front()->value;
577+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
579578
++it) {
580579
while (val++ != (*it)->value) code += "\"\", ";
581580
code += "\"" + (*it)->name + "\", ";

src/idl_gen_go.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class GoGenerator : public BaseGenerator {
153153
}
154154

155155
// A single enum member.
156-
void EnumMember(const EnumDef &enum_def, const EnumVal ev,
156+
void EnumMember(const EnumDef &enum_def, const EnumVal &ev,
157157
std::string *code_ptr) {
158158
std::string &code = *code_ptr;
159159
code += "\t";
@@ -725,17 +725,15 @@ class GoGenerator : public BaseGenerator {
725725
GenComment(enum_def.doc_comment, code_ptr, nullptr);
726726
GenEnumType(enum_def, code_ptr);
727727
BeginEnum(code_ptr);
728-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
729-
++it) {
728+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
730729
auto &ev = **it;
731730
GenComment(ev.doc_comment, code_ptr, nullptr, "\t");
732731
EnumMember(enum_def, ev, code_ptr);
733732
}
734733
EndEnum(code_ptr);
735734

736735
BeginEnumNames(enum_def, code_ptr);
737-
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
738-
++it) {
736+
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
739737
auto &ev = **it;
740738
EnumNameMember(enum_def, ev, code_ptr);
741739
}

0 commit comments

Comments
 (0)