Skip to content

Commit

Permalink
[C++] Using calculated fields_number for field_names size (google#6769)
Browse files Browse the repository at this point in the history
* using already defined "fields_number" in "field_names" size calculation

* tests

* forgotten test
  • Loading branch information
bakinovsky-m authored Aug 5, 2021
1 parent 909ce97 commit 5d77820
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
12 changes: 3 additions & 9 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2156,14 +2156,8 @@ class CppGenerator : public BaseGenerator {
// };
//
void GenFieldNames(const StructDef &struct_def) {
auto non_deprecated_field_count = std::count_if(
struct_def.fields.vec.begin(), struct_def.fields.vec.end(),
[](const FieldDef *field) { return !field->deprecated; });
code_ += " static constexpr std::array<\\";
code_.SetValue(
"FIELD_COUNT",
std::to_string(static_cast<long long>(non_deprecated_field_count)));
code_ += "const char *, {{FIELD_COUNT}}> field_names = {\\";
code_ += "const char *, fields_number> field_names = {\\";
if (struct_def.fields.vec.empty()) {
code_ += "};";
return;
Expand All @@ -2185,7 +2179,7 @@ class CppGenerator : public BaseGenerator {
}

void GenFieldsNumber(const StructDef &struct_def) {
auto non_deprecated_field_count = std::count_if(
const auto non_deprecated_field_count = std::count_if(
struct_def.fields.vec.begin(), struct_def.fields.vec.end(),
[](const FieldDef *field) { return !field->deprecated; });
code_.SetValue(
Expand All @@ -2209,9 +2203,9 @@ class CppGenerator : public BaseGenerator {
code_ +=
" static constexpr auto fully_qualified_name = "
"\"{{FULLY_QUALIFIED_NAME}}\";";
GenFieldsNumber(struct_def);
GenFieldNames(struct_def);
GenFieldTypeHelper(struct_def);
GenFieldsNumber(struct_def);
}
code_ += "};";
code_ += "";
Expand Down
40 changes: 20 additions & 20 deletions tests/cpp17/generated_cpp17/monster_test_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,13 @@ struct Test::Traits {
using type = Test;
static constexpr auto name = "Test";
static constexpr auto fully_qualified_name = "MyGame.Example.Test";
static constexpr std::array<const char *, 2> field_names = {
static constexpr size_t fields_number = 2;
static constexpr std::array<const char *, fields_number> field_names = {
"a",
"b"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 2;
};

FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
Expand Down Expand Up @@ -626,7 +626,8 @@ struct Vec3::Traits {
using type = Vec3;
static constexpr auto name = "Vec3";
static constexpr auto fully_qualified_name = "MyGame.Example.Vec3";
static constexpr std::array<const char *, 6> field_names = {
static constexpr size_t fields_number = 6;
static constexpr std::array<const char *, fields_number> field_names = {
"x",
"y",
"z",
Expand All @@ -636,7 +637,6 @@ struct Vec3::Traits {
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 6;
};

FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
Expand Down Expand Up @@ -688,13 +688,13 @@ struct Ability::Traits {
using type = Ability;
static constexpr auto name = "Ability";
static constexpr auto fully_qualified_name = "MyGame.Example.Ability";
static constexpr std::array<const char *, 2> field_names = {
static constexpr size_t fields_number = 2;
static constexpr std::array<const char *, fields_number> field_names = {
"id",
"distance"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 2;
};

FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructOfStructs FLATBUFFERS_FINAL_CLASS {
Expand Down Expand Up @@ -750,14 +750,14 @@ struct StructOfStructs::Traits {
using type = StructOfStructs;
static constexpr auto name = "StructOfStructs";
static constexpr auto fully_qualified_name = "MyGame.Example.StructOfStructs";
static constexpr std::array<const char *, 3> field_names = {
static constexpr size_t fields_number = 3;
static constexpr std::array<const char *, fields_number> field_names = {
"a",
"b",
"c"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 3;
};

} // namespace Example
Expand Down Expand Up @@ -808,8 +808,8 @@ struct InParentNamespace::Traits {
static auto constexpr Create = CreateInParentNamespace;
static constexpr auto name = "InParentNamespace";
static constexpr auto fully_qualified_name = "MyGame.InParentNamespace";
static constexpr std::array<const char *, 0> field_names = {};
static constexpr size_t fields_number = 0;
static constexpr std::array<const char *, fields_number> field_names = {};
};

flatbuffers::Offset<InParentNamespace> CreateInParentNamespace(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
Expand Down Expand Up @@ -862,8 +862,8 @@ struct Monster::Traits {
static auto constexpr Create = CreateMonster;
static constexpr auto name = "Monster";
static constexpr auto fully_qualified_name = "MyGame.Example2.Monster";
static constexpr std::array<const char *, 0> field_names = {};
static constexpr size_t fields_number = 0;
static constexpr std::array<const char *, fields_number> field_names = {};
};

flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
Expand Down Expand Up @@ -939,12 +939,12 @@ struct TestSimpleTableWithEnum::Traits {
static auto constexpr Create = CreateTestSimpleTableWithEnum;
static constexpr auto name = "TestSimpleTableWithEnum";
static constexpr auto fully_qualified_name = "MyGame.Example.TestSimpleTableWithEnum";
static constexpr std::array<const char *, 1> field_names = {
static constexpr size_t fields_number = 1;
static constexpr std::array<const char *, fields_number> field_names = {
"color"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 1;
};

flatbuffers::Offset<TestSimpleTableWithEnum> CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
Expand Down Expand Up @@ -1053,14 +1053,14 @@ struct Stat::Traits {
static auto constexpr Create = CreateStat;
static constexpr auto name = "Stat";
static constexpr auto fully_qualified_name = "MyGame.Example.Stat";
static constexpr std::array<const char *, 3> field_names = {
static constexpr size_t fields_number = 3;
static constexpr std::array<const char *, fields_number> field_names = {
"id",
"val",
"count"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 3;
};

inline flatbuffers::Offset<Stat> CreateStatDirect(
Expand Down Expand Up @@ -1151,12 +1151,12 @@ struct Referrable::Traits {
static auto constexpr Create = CreateReferrable;
static constexpr auto name = "Referrable";
static constexpr auto fully_qualified_name = "MyGame.Example.Referrable";
static constexpr std::array<const char *, 1> field_names = {
static constexpr size_t fields_number = 1;
static constexpr std::array<const char *, fields_number> field_names = {
"id"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 1;
};

flatbuffers::Offset<Referrable> CreateReferrable(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
Expand Down Expand Up @@ -2056,7 +2056,8 @@ struct Monster::Traits {
static auto constexpr Create = CreateMonster;
static constexpr auto name = "Monster";
static constexpr auto fully_qualified_name = "MyGame.Example.Monster";
static constexpr std::array<const char *, 50> field_names = {
static constexpr size_t fields_number = 50;
static constexpr std::array<const char *, fields_number> field_names = {
"pos",
"mana",
"hp",
Expand Down Expand Up @@ -2110,7 +2111,6 @@ struct Monster::Traits {
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 50;
};

inline flatbuffers::Offset<Monster> CreateMonsterDirect(
Expand Down Expand Up @@ -2476,7 +2476,8 @@ struct TypeAliases::Traits {
static auto constexpr Create = CreateTypeAliases;
static constexpr auto name = "TypeAliases";
static constexpr auto fully_qualified_name = "MyGame.Example.TypeAliases";
static constexpr std::array<const char *, 12> field_names = {
static constexpr size_t fields_number = 12;
static constexpr std::array<const char *, fields_number> field_names = {
"i8",
"u8",
"i16",
Expand All @@ -2492,7 +2493,6 @@ struct TypeAliases::Traits {
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 12;
};

inline flatbuffers::Offset<TypeAliases> CreateTypeAliasesDirect(
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp17/generated_cpp17/optional_scalars_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ struct ScalarStuff::Traits {
static auto constexpr Create = CreateScalarStuff;
static constexpr auto name = "ScalarStuff";
static constexpr auto fully_qualified_name = "optional_scalars.ScalarStuff";
static constexpr std::array<const char *, 36> field_names = {
static constexpr size_t fields_number = 36;
static constexpr std::array<const char *, fields_number> field_names = {
"just_i8",
"maybe_i8",
"default_i8",
Expand Down Expand Up @@ -679,7 +680,6 @@ struct ScalarStuff::Traits {
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 36;
};

flatbuffers::Offset<ScalarStuff> CreateScalarStuff(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
Expand Down
16 changes: 8 additions & 8 deletions tests/cpp17/generated_cpp17/union_vector_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ struct Rapunzel::Traits {
using type = Rapunzel;
static constexpr auto name = "Rapunzel";
static constexpr auto fully_qualified_name = "Rapunzel";
static constexpr std::array<const char *, 1> field_names = {
static constexpr size_t fields_number = 1;
static constexpr std::array<const char *, fields_number> field_names = {
"hair_length"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 1;
};

FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
Expand Down Expand Up @@ -218,12 +218,12 @@ struct BookReader::Traits {
using type = BookReader;
static constexpr auto name = "BookReader";
static constexpr auto fully_qualified_name = "BookReader";
static constexpr std::array<const char *, 1> field_names = {
static constexpr size_t fields_number = 1;
static constexpr std::array<const char *, fields_number> field_names = {
"books_read"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 1;
};

struct AttackerT : public flatbuffers::NativeTable {
Expand Down Expand Up @@ -293,12 +293,12 @@ struct Attacker::Traits {
static auto constexpr Create = CreateAttacker;
static constexpr auto name = "Attacker";
static constexpr auto fully_qualified_name = "Attacker";
static constexpr std::array<const char *, 1> field_names = {
static constexpr size_t fields_number = 1;
static constexpr std::array<const char *, fields_number> field_names = {
"sword_attack_damage"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 1;
};

flatbuffers::Offset<Attacker> CreateAttacker(flatbuffers::FlatBufferBuilder &_fbb, const AttackerT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
Expand Down Expand Up @@ -432,15 +432,15 @@ struct Movie::Traits {
static auto constexpr Create = CreateMovie;
static constexpr auto name = "Movie";
static constexpr auto fully_qualified_name = "Movie";
static constexpr std::array<const char *, 4> field_names = {
static constexpr size_t fields_number = 4;
static constexpr std::array<const char *, fields_number> field_names = {
"main_character_type",
"main_character",
"characters_type",
"characters"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 4;
};

inline flatbuffers::Offset<Movie> CreateMovieDirect(
Expand Down

0 comments on commit 5d77820

Please sign in to comment.