Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
svenk177 committed Jun 14, 2019
1 parent f82db64 commit 8d0d5df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
44 changes: 18 additions & 26 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,31 +307,13 @@ class CppGenerator : public BaseGenerator {
}
}

// Check if enum arrays are used without specifying --scoped-enums
if (!parser_.opts.scoped_enums) {
for (auto it_s = parser_.structs_.vec.begin();
it_s != parser_.structs_.vec.end(); ++it_s) {
const auto &struct_def = **it_s;
for (auto it_f = struct_def.fields.vec.begin();
it_f != struct_def.fields.vec.end(); ++it_f) {
const auto &field_def = **it_f;
const auto &field_type = field_def.value.type;

if (IsArray(field_type) && IsEnum(field_type.VectorType())) {
printf("--scoped-enums must be enabled to use enum arrays\n");
return false;
}
}
}
}

// Generate code for all structs, then all tables.
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
const auto &struct_def = **it;
if (struct_def.fixed && !struct_def.generated) {
SetNameSpace(struct_def.defined_namespace);
GenStruct(struct_def);
if (!GenStruct(struct_def)) return false;
}
}
for (auto it = parser_.structs_.vec.begin();
Expand Down Expand Up @@ -2724,7 +2706,7 @@ class CppGenerator : public BaseGenerator {
}

// Generate an accessor struct with constructor for a flatbuffers struct.
void GenStruct(const StructDef &struct_def) {
bool GenStruct(const StructDef &struct_def) {
// Generate an accessor struct, with private variables of the form:
// type name_;
// Generates manual padding and alignment.
Expand All @@ -2743,13 +2725,21 @@ class CppGenerator : public BaseGenerator {
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
const auto &field = **it;
code_.SetValue("FIELD_TYPE",
GenTypeGet(field.value.type, " ", "", " ", false));
const auto &field_type = field.value.type;

// Check if enum arrays are used without specifying --scoped-enums
if (!parser_.opts.scoped_enums && IsArray(field_type) &&
IsEnum(field_type.VectorType())) {
printf("--scoped-enums must be enabled to use enum arrays\n");
return false;
}

code_.SetValue("FIELD_TYPE", GenTypeGet(field_type, " ", "", " ", false));
code_.SetValue("FIELD_NAME", Name(field));
code_.SetValue(
"ARRAY", IsArray(field.value.type)
? "[" + NumToString(field.value.type.fixed_length) + "]"
: "");
code_.SetValue("ARRAY",
IsArray(field_type)
? "[" + NumToString(field_type.fixed_length) + "]"
: "");
code_ += (" {{FIELD_TYPE}}{{FIELD_NAME}}_{{ARRAY}};");

if (field.padding) {
Expand Down Expand Up @@ -2925,6 +2915,8 @@ class CppGenerator : public BaseGenerator {
code_ += "FLATBUFFERS_STRUCT_END({{STRUCT_NAME}}, {{STRUCT_BYTE_SIZE}});";
if (parser_.opts.gen_compare) GenCompareOperator(struct_def, "()");
code_ += "";

return true;
}

// Set up the correct namespace. Only open a namespace if the existing one is
Expand Down
2 changes: 1 addition & 1 deletion src/idl_gen_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ class GeneralGenerator : public BaseGenerator {
// Generate mutators for scalar fields or vectors of scalars.
if (parser_.opts.mutable_buffer) {
auto is_series = (IsSeries(field.value.type));
auto underlying_type =
const auto &underlying_type =
is_series ? field.value.type.VectorType() : field.value.type;
// Boolean parameters have to be explicitly converted to byte
// representation.
Expand Down
5 changes: 3 additions & 2 deletions src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,8 @@ CheckedError Parser::ParseArray(Value &array) {

uoffset_t count = 0;
auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError {
Value val;
vector_emplace_back(&stack, Value());
auto &val = stack.back();
val.type = type;
if (IsStruct(type)) {
ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr));
Expand All @@ -1299,7 +1300,7 @@ CheckedError Parser::ParseArray(Value &array) {

if (length != count) return Error("Fixed-length array size is incorrect.");

for (auto it = stack.rbegin(); it != stack.rend(); it++) {
for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
auto &val = *it;
// clang-format off
switch (val.type.base_type) {
Expand Down

0 comments on commit 8d0d5df

Please sign in to comment.