Skip to content

Commit

Permalink
Part of #5265, neutral changes (#5281)
Browse files Browse the repository at this point in the history
  • Loading branch information
vglavnyy authored and aardappel committed Apr 8, 2019
1 parent dd85c3b commit dd6daa7
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 122 deletions.
4 changes: 2 additions & 2 deletions docs/source/Schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ Current understood attributes:
Note: currently not guaranteed to have an effect when used with
`--object-api`, since that may allocate objects at alignments less than
what you specify with `force_align`.
- `bit_flags` (on an enum): the values of this field indicate bits,
meaning that any value N specified in the schema will end up
- `bit_flags` (on an unsigned enum): the values of this field indicate bits,
meaning that any unsigned value N specified in the schema will end up
representing 1<<N, or if you don't specify values at all, you'll get
the sequence 1, 2, 4, 8, ...
- `nested_flatbuffer: "table_name"` (on a field): this indicates that the field
Expand Down
20 changes: 13 additions & 7 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ struct EnumVal {
Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder, const Parser &parser) const;

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

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

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

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

size_t size() const { return vals.vec.size(); }

const std::vector<EnumVal *> &Vals() const {
return vals.vec;
}

SymbolTable<EnumVal> vals;
bool is_union;
// Type is a union which uses type aliases where at least one type is
Expand Down Expand Up @@ -691,17 +699,15 @@ class Parser : public ParserState {
bool ParseFlexBuffer(const char *source, const char *source_filename,
flexbuffers::Builder *builder);

FLATBUFFERS_CHECKED_ERROR InvalidNumber(const char *number,
const std::string &msg);

StructDef *LookupStruct(const std::string &id) const;

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

FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);

private:
void Message(const std::string &msg);
void Warning(const std::string &msg);
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val);
FLATBUFFERS_CHECKED_ERROR Next();
FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark();
Expand Down Expand Up @@ -745,7 +751,7 @@ class Parser : public ParserState {
FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef* field);
FLATBUFFERS_CHECKED_ERROR TokenError();
FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string *name, Value &e, bool check_now);
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(Type &type, int64_t *result);
FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(const Type &type, std::string *result);
StructDef *LookupCreateStruct(const std::string &name,
bool create_if_new = true,
bool definition = false);
Expand Down
62 changes: 29 additions & 33 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ class CppGenerator : public BaseGenerator {
struct_def ? (struct_def->fixed ? "ST_STRUCT" : "ST_TABLE")
: (enum_def->is_union ? "ST_UNION" : "ST_ENUM"));
auto num_fields =
struct_def ? struct_def->fields.vec.size() : enum_def->vals.vec.size();
struct_def ? struct_def->fields.vec.size() : enum_def->size();
code_.SetValue("NUM_FIELDS", NumToString(num_fields));
std::vector<std::string> names;
std::vector<Type> types;
Expand All @@ -798,13 +798,13 @@ class CppGenerator : public BaseGenerator {
types.push_back(field.value.type);
}
} else {
for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end();
for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
++it) {
const auto &ev = **it;
names.push_back(Name(ev));
types.push_back(enum_def->is_union ? ev.union_type
: Type(enum_def->underlying_type));
if (static_cast<int64_t>(it - enum_def->vals.vec.begin()) != ev.value) {
if (static_cast<int64_t>(it - enum_def->Vals().begin()) != ev.value) {
consecutive_enum_from_zero = false;
}
}
Expand Down Expand Up @@ -852,7 +852,7 @@ class CppGenerator : public BaseGenerator {
}
std::string vs;
if (enum_def && !consecutive_enum_from_zero) {
for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end();
for (auto it = enum_def->Vals().begin(); it != enum_def->Vals().end();
++it) {
const auto &ev = **it;
if (!vs.empty()) vs += ", ";
Expand Down Expand Up @@ -919,8 +919,7 @@ class CppGenerator : public BaseGenerator {

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

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

// Generate an array of all enumeration values
auto num_fields = NumToString(enum_def.vals.vec.size());
auto num_fields = NumToString(enum_def.size());
code_ += "inline const {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" +
num_fields + "] {";
code_ += " static const {{ENUM_NAME}} values[] = {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
const auto &ev = **it;
auto value = GetEnumValUse(enum_def, ev);
auto suffix = *it != enum_def.vals.vec.back() ? "," : "";
auto suffix = *it != enum_def.Vals().back() ? "," : "";
code_ += " " + value + suffix;
}
code_ += " };";
Expand All @@ -996,8 +994,8 @@ class CppGenerator : public BaseGenerator {
code_ += "inline const char * const *EnumNames{{ENUM_NAME}}() {";
code_ += " static const char * const names[] = {";

auto val = enum_def.vals.vec.front()->value;
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
auto val = enum_def.Vals().front()->value;
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
const auto &ev = **it;
while (val++ != ev.value) { code_ += " \"\","; }
Expand Down Expand Up @@ -1032,7 +1030,7 @@ class CppGenerator : public BaseGenerator {

code_ += " switch (e) {";

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

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

if (it == enum_def.vals.vec.begin()) {
if (it == enum_def.Vals().begin()) {
code_ += "template<typename T> struct {{ENUM_NAME}}Traits {";
} else {
auto name = GetUnionElement(ev, true, true);
Expand Down Expand Up @@ -1114,10 +1112,10 @@ class CppGenerator : public BaseGenerator {
code_ += " " + UnionPackSignature(enum_def, true) + ";";
code_ += "";

for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
const auto &ev = **it;
if (!ev.value) { continue; }
if (ev.IsZero()) { continue; }

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

for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
const auto &ev = **it;
code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev));
if (ev.value) {
if (ev.IsNonZero()) {
const auto native_type =
NativeName(GetUnionElement(ev, true, true, true),
ev.union_type.struct_def, parser_.opts);
Expand Down Expand Up @@ -1204,12 +1202,11 @@ class CppGenerator : public BaseGenerator {

code_ += "inline " + UnionVerifySignature(enum_def) + " {";
code_ += " switch (type) {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
const auto &ev = **it;
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));

if (ev.value) {
if (ev.IsNonZero()) {
code_.SetValue("TYPE", GetUnionElement(ev, true, true));
code_ += " case {{LABEL}}: {";
auto getptr =
Expand Down Expand Up @@ -1257,10 +1254,10 @@ class CppGenerator : public BaseGenerator {
// Generate union Unpack() and Pack() functions.
code_ += "inline " + UnionUnPackSignature(enum_def, false) + " {";
code_ += " switch (type) {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
const auto &ev = **it;
if (!ev.value) { continue; }
if (ev.IsZero()) { continue; }

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

code_ += "inline " + UnionPackSignature(enum_def, false) + " {";
code_ += " switch (type) {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
auto &ev = **it;
if (!ev.value) { continue; }
if (ev.IsZero()) { continue; }

code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
code_.SetValue("TYPE",
Expand Down Expand Up @@ -1324,10 +1321,10 @@ class CppGenerator : public BaseGenerator {
"{{ENUM_NAME}}Union &u) FLATBUFFERS_NOEXCEPT : type(u.type), "
"value(nullptr) {";
code_ += " switch (type) {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
const auto &ev = **it;
if (!ev.value) { continue; }
if (ev.IsZero()) { continue; }
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
code_.SetValue("TYPE",
NativeName(GetUnionElement(ev, true, true, true),
Expand Down Expand Up @@ -1370,10 +1367,10 @@ class CppGenerator : public BaseGenerator {

code_ += "inline void {{ENUM_NAME}}Union::Reset() {";
code_ += " switch (type) {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
const auto &ev = **it;
if (!ev.value) { continue; }
if (ev.IsZero()) { continue; }
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
code_.SetValue("TYPE",
NativeName(GetUnionElement(ev, true, true, true),
Expand Down Expand Up @@ -1834,8 +1831,7 @@ class CppGenerator : public BaseGenerator {
" template<typename T> "
"const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;";

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

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

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

Expand Down
13 changes: 6 additions & 7 deletions src/idl_gen_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,19 @@ class DartGenerator : public BaseGenerator {
" static bool containsValue(int value) =>"
" values.containsKey(value);\n\n";

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

if (!ev.doc_comment.empty()) {
if (it != enum_def.vals.vec.begin()) { code += '\n'; }
if (it != enum_def.Vals().begin()) { code += '\n'; }
GenDocComment(ev.doc_comment, &code, "", " ");
}
code += " static const " + name + " " + ev.name + " = ";
code += "const " + name + "._(" + NumToString(ev.value) + ");\n";
}

code += " static get values => {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
code += NumToString(ev.value) + ": " + ev.name + ",";
}
Expand Down Expand Up @@ -503,8 +501,9 @@ class DartGenerator : public BaseGenerator {
if (field.value.type.base_type == BASE_TYPE_UNION) {
code += " {\n";
code += " switch (" + field_name + "Type?.value) {\n";
for (auto en_it = field.value.type.enum_def->vals.vec.begin() + 1;
en_it != field.value.type.enum_def->vals.vec.end(); ++en_it) {
auto &enum_def = *field.value.type.enum_def;
for (auto en_it = enum_def.Vals().begin() + 1;
en_it != enum_def.Vals().end(); ++en_it) {
auto &ev = **en_it;

auto enum_name = NamespaceAliasFromUnionType(ev.name);
Expand Down
3 changes: 1 addition & 2 deletions src/idl_gen_fbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
else
schema += "enum " + enum_def.name + " : ";
schema += GenType(enum_def.underlying_type, true) + " {\n";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
GenComment(ev.doc_comment, &schema, nullptr, " ");
if (enum_def.is_union)
Expand Down
7 changes: 3 additions & 4 deletions src/idl_gen_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,7 @@ class GeneralGenerator : public BaseGenerator {
if (lang_.language == IDLOptions::kJava) {
code += " private " + enum_def.name + "() { }\n";
}
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, &lang_.comment_config, " ");
if (lang_.language != IDLOptions::kCSharp) {
Expand Down Expand Up @@ -574,8 +573,8 @@ class GeneralGenerator : public BaseGenerator {
code += lang_.const_decl;
code += lang_.string_type;
code += "[] names = { ";
auto val = enum_def.vals.vec.front()->value;
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
auto val = enum_def.Vals().front()->value;
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
while (val++ != (*it)->value) code += "\"\", ";
code += "\"" + (*it)->name + "\", ";
Expand Down
8 changes: 3 additions & 5 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class GoGenerator : public BaseGenerator {
}

// A single enum member.
void EnumMember(const EnumDef &enum_def, const EnumVal ev,
void EnumMember(const EnumDef &enum_def, const EnumVal &ev,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += "\t";
Expand Down Expand Up @@ -725,17 +725,15 @@ class GoGenerator : public BaseGenerator {
GenComment(enum_def.doc_comment, code_ptr, nullptr);
GenEnumType(enum_def, code_ptr);
BeginEnum(code_ptr);
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr, "\t");
EnumMember(enum_def, ev, code_ptr);
}
EndEnum(code_ptr);

BeginEnumNames(enum_def, code_ptr);
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
EnumNameMember(enum_def, ev, code_ptr);
}
Expand Down
Loading

0 comments on commit dd6daa7

Please sign in to comment.