[C++] Using calculated fields_number for field_names size (#6769)

* using already defined "fields_number" in "field_names" size calculation

* tests

* forgotten test
This commit is contained in:
bakinovsky-m
2021-08-05 21:41:49 +03:00
committed by GitHub
parent 909ce970ae
commit 5d77820b34
4 changed files with 33 additions and 39 deletions

View File

@@ -2156,14 +2156,8 @@ class CppGenerator : public BaseGenerator {
// }; // };
// //
void GenFieldNames(const StructDef &struct_def) { 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_ += " static constexpr std::array<\\";
code_.SetValue( code_ += "const char *, fields_number> field_names = {\\";
"FIELD_COUNT",
std::to_string(static_cast<long long>(non_deprecated_field_count)));
code_ += "const char *, {{FIELD_COUNT}}> field_names = {\\";
if (struct_def.fields.vec.empty()) { if (struct_def.fields.vec.empty()) {
code_ += "};"; code_ += "};";
return; return;
@@ -2185,7 +2179,7 @@ class CppGenerator : public BaseGenerator {
} }
void GenFieldsNumber(const StructDef &struct_def) { 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(), struct_def.fields.vec.begin(), struct_def.fields.vec.end(),
[](const FieldDef *field) { return !field->deprecated; }); [](const FieldDef *field) { return !field->deprecated; });
code_.SetValue( code_.SetValue(
@@ -2209,9 +2203,9 @@ class CppGenerator : public BaseGenerator {
code_ += code_ +=
" static constexpr auto fully_qualified_name = " " static constexpr auto fully_qualified_name = "
"\"{{FULLY_QUALIFIED_NAME}}\";"; "\"{{FULLY_QUALIFIED_NAME}}\";";
GenFieldsNumber(struct_def);
GenFieldNames(struct_def); GenFieldNames(struct_def);
GenFieldTypeHelper(struct_def); GenFieldTypeHelper(struct_def);
GenFieldsNumber(struct_def);
} }
code_ += "};"; code_ += "};";
code_ += ""; code_ += "";

View File

@@ -519,13 +519,13 @@ struct Test::Traits {
using type = Test; using type = Test;
static constexpr auto name = "Test"; static constexpr auto name = "Test";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "a",
"b" "b"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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 { FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
@@ -626,7 +626,8 @@ struct Vec3::Traits {
using type = Vec3; using type = Vec3;
static constexpr auto name = "Vec3"; static constexpr auto name = "Vec3";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "x",
"y", "y",
"z", "z",
@@ -636,7 +637,6 @@ struct Vec3::Traits {
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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 { FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
@@ -688,13 +688,13 @@ struct Ability::Traits {
using type = Ability; using type = Ability;
static constexpr auto name = "Ability"; static constexpr auto name = "Ability";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "id",
"distance" "distance"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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 { FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructOfStructs FLATBUFFERS_FINAL_CLASS {
@@ -750,14 +750,14 @@ struct StructOfStructs::Traits {
using type = StructOfStructs; using type = StructOfStructs;
static constexpr auto name = "StructOfStructs"; static constexpr auto name = "StructOfStructs";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "a",
"b", "b",
"c" "c"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>()); using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 3;
}; };
} // namespace Example } // namespace Example
@@ -808,8 +808,8 @@ struct InParentNamespace::Traits {
static auto constexpr Create = CreateInParentNamespace; static auto constexpr Create = CreateInParentNamespace;
static constexpr auto name = "InParentNamespace"; static constexpr auto name = "InParentNamespace";
static constexpr auto fully_qualified_name = "MyGame.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 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); flatbuffers::Offset<InParentNamespace> CreateInParentNamespace(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -862,8 +862,8 @@ struct Monster::Traits {
static auto constexpr Create = CreateMonster; static auto constexpr Create = CreateMonster;
static constexpr auto name = "Monster"; static constexpr auto name = "Monster";
static constexpr auto fully_qualified_name = "MyGame.Example2.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 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); flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -939,12 +939,12 @@ struct TestSimpleTableWithEnum::Traits {
static auto constexpr Create = CreateTestSimpleTableWithEnum; static auto constexpr Create = CreateTestSimpleTableWithEnum;
static constexpr auto name = "TestSimpleTableWithEnum"; static constexpr auto name = "TestSimpleTableWithEnum";
static constexpr auto fully_qualified_name = "MyGame.Example.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" "color"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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); flatbuffers::Offset<TestSimpleTableWithEnum> CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -1053,14 +1053,14 @@ struct Stat::Traits {
static auto constexpr Create = CreateStat; static auto constexpr Create = CreateStat;
static constexpr auto name = "Stat"; static constexpr auto name = "Stat";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "id",
"val", "val",
"count" "count"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>()); using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 3;
}; };
inline flatbuffers::Offset<Stat> CreateStatDirect( inline flatbuffers::Offset<Stat> CreateStatDirect(
@@ -1151,12 +1151,12 @@ struct Referrable::Traits {
static auto constexpr Create = CreateReferrable; static auto constexpr Create = CreateReferrable;
static constexpr auto name = "Referrable"; static constexpr auto name = "Referrable";
static constexpr auto fully_qualified_name = "MyGame.Example.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" "id"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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); flatbuffers::Offset<Referrable> CreateReferrable(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -2056,7 +2056,8 @@ struct Monster::Traits {
static auto constexpr Create = CreateMonster; static auto constexpr Create = CreateMonster;
static constexpr auto name = "Monster"; static constexpr auto name = "Monster";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "pos",
"mana", "mana",
"hp", "hp",
@@ -2110,7 +2111,6 @@ struct Monster::Traits {
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>()); using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 50;
}; };
inline flatbuffers::Offset<Monster> CreateMonsterDirect( inline flatbuffers::Offset<Monster> CreateMonsterDirect(
@@ -2476,7 +2476,8 @@ struct TypeAliases::Traits {
static auto constexpr Create = CreateTypeAliases; static auto constexpr Create = CreateTypeAliases;
static constexpr auto name = "TypeAliases"; static constexpr auto name = "TypeAliases";
static constexpr auto fully_qualified_name = "MyGame.Example.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", "i8",
"u8", "u8",
"i16", "i16",
@@ -2492,7 +2493,6 @@ struct TypeAliases::Traits {
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>()); using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 12;
}; };
inline flatbuffers::Offset<TypeAliases> CreateTypeAliasesDirect( inline flatbuffers::Offset<TypeAliases> CreateTypeAliasesDirect(

View File

@@ -639,7 +639,8 @@ struct ScalarStuff::Traits {
static auto constexpr Create = CreateScalarStuff; static auto constexpr Create = CreateScalarStuff;
static constexpr auto name = "ScalarStuff"; static constexpr auto name = "ScalarStuff";
static constexpr auto fully_qualified_name = "optional_scalars.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", "just_i8",
"maybe_i8", "maybe_i8",
"default_i8", "default_i8",
@@ -679,7 +680,6 @@ struct ScalarStuff::Traits {
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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); flatbuffers::Offset<ScalarStuff> CreateScalarStuff(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);

View File

@@ -177,12 +177,12 @@ struct Rapunzel::Traits {
using type = Rapunzel; using type = Rapunzel;
static constexpr auto name = "Rapunzel"; static constexpr auto name = "Rapunzel";
static constexpr auto fully_qualified_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" "hair_length"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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 { FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
@@ -218,12 +218,12 @@ struct BookReader::Traits {
using type = BookReader; using type = BookReader;
static constexpr auto name = "BookReader"; static constexpr auto name = "BookReader";
static constexpr auto fully_qualified_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" "books_read"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>()); using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 1;
}; };
struct AttackerT : public flatbuffers::NativeTable { struct AttackerT : public flatbuffers::NativeTable {
@@ -293,12 +293,12 @@ struct Attacker::Traits {
static auto constexpr Create = CreateAttacker; static auto constexpr Create = CreateAttacker;
static constexpr auto name = "Attacker"; static constexpr auto name = "Attacker";
static constexpr auto fully_qualified_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" "sword_attack_damage"
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<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); flatbuffers::Offset<Attacker> CreateAttacker(flatbuffers::FlatBufferBuilder &_fbb, const AttackerT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -432,7 +432,8 @@ struct Movie::Traits {
static auto constexpr Create = CreateMovie; static auto constexpr Create = CreateMovie;
static constexpr auto name = "Movie"; static constexpr auto name = "Movie";
static constexpr auto fully_qualified_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_type",
"main_character", "main_character",
"characters_type", "characters_type",
@@ -440,7 +441,6 @@ struct Movie::Traits {
}; };
template<size_t Index> template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>()); using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 4;
}; };
inline flatbuffers::Offset<Movie> CreateMovieDirect( inline flatbuffers::Offset<Movie> CreateMovieDirect(