From 5d77820b3468053fe4d6e03d0e316ae76d4f5b8c Mon Sep 17 00:00:00 2001 From: bakinovsky-m Date: Thu, 5 Aug 2021 21:41:49 +0300 Subject: [PATCH] [C++] Using calculated fields_number for field_names size (#6769) * using already defined "fields_number" in "field_names" size calculation * tests * forgotten test --- src/idl_gen_cpp.cpp | 12 ++---- .../generated_cpp17/monster_test_generated.h | 40 +++++++++---------- .../optional_scalars_generated.h | 4 +- .../generated_cpp17/union_vector_generated.h | 16 ++++---- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 455637127b7..1ee9b74d239 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -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(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; @@ -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( @@ -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_ += ""; diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index c95be273f39..57c82b04365 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -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 field_names = { + static constexpr size_t fields_number = 2; + static constexpr std::array field_names = { "a", "b" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 2; }; FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS { @@ -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 field_names = { + static constexpr size_t fields_number = 6; + static constexpr std::array field_names = { "x", "y", "z", @@ -636,7 +637,6 @@ struct Vec3::Traits { }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 6; }; FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { @@ -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 field_names = { + static constexpr size_t fields_number = 2; + static constexpr std::array field_names = { "id", "distance" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 2; }; FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructOfStructs FLATBUFFERS_FINAL_CLASS { @@ -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 field_names = { + static constexpr size_t fields_number = 3; + static constexpr std::array field_names = { "a", "b", "c" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 3; }; } // namespace Example @@ -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 field_names = {}; static constexpr size_t fields_number = 0; + static constexpr std::array field_names = {}; }; flatbuffers::Offset 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 constexpr auto name = "Monster"; static constexpr auto fully_qualified_name = "MyGame.Example2.Monster"; - static constexpr std::array field_names = {}; static constexpr size_t fields_number = 0; + static constexpr std::array field_names = {}; }; flatbuffers::Offset 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 constexpr auto name = "TestSimpleTableWithEnum"; static constexpr auto fully_qualified_name = "MyGame.Example.TestSimpleTableWithEnum"; - static constexpr std::array field_names = { + static constexpr size_t fields_number = 1; + static constexpr std::array field_names = { "color" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 1; }; flatbuffers::Offset 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 constexpr auto name = "Stat"; static constexpr auto fully_qualified_name = "MyGame.Example.Stat"; - static constexpr std::array field_names = { + static constexpr size_t fields_number = 3; + static constexpr std::array field_names = { "id", "val", "count" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 3; }; inline flatbuffers::Offset CreateStatDirect( @@ -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 field_names = { + static constexpr size_t fields_number = 1; + static constexpr std::array field_names = { "id" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 1; }; flatbuffers::Offset 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 constexpr auto name = "Monster"; static constexpr auto fully_qualified_name = "MyGame.Example.Monster"; - static constexpr std::array field_names = { + static constexpr size_t fields_number = 50; + static constexpr std::array field_names = { "pos", "mana", "hp", @@ -2110,7 +2111,6 @@ struct Monster::Traits { }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 50; }; inline flatbuffers::Offset CreateMonsterDirect( @@ -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 field_names = { + static constexpr size_t fields_number = 12; + static constexpr std::array field_names = { "i8", "u8", "i16", @@ -2492,7 +2493,6 @@ struct TypeAliases::Traits { }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 12; }; inline flatbuffers::Offset CreateTypeAliasesDirect( diff --git a/tests/cpp17/generated_cpp17/optional_scalars_generated.h b/tests/cpp17/generated_cpp17/optional_scalars_generated.h index 4e99e7c299a..641e5e2673d 100644 --- a/tests/cpp17/generated_cpp17/optional_scalars_generated.h +++ b/tests/cpp17/generated_cpp17/optional_scalars_generated.h @@ -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 field_names = { + static constexpr size_t fields_number = 36; + static constexpr std::array field_names = { "just_i8", "maybe_i8", "default_i8", @@ -679,7 +680,6 @@ struct ScalarStuff::Traits { }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 36; }; flatbuffers::Offset CreateScalarStuff(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); diff --git a/tests/cpp17/generated_cpp17/union_vector_generated.h b/tests/cpp17/generated_cpp17/union_vector_generated.h index 70097c7fa0a..b73ecc71bf0 100644 --- a/tests/cpp17/generated_cpp17/union_vector_generated.h +++ b/tests/cpp17/generated_cpp17/union_vector_generated.h @@ -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 field_names = { + static constexpr size_t fields_number = 1; + static constexpr std::array field_names = { "hair_length" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 1; }; FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS { @@ -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 field_names = { + static constexpr size_t fields_number = 1; + static constexpr std::array field_names = { "books_read" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 1; }; struct AttackerT : public flatbuffers::NativeTable { @@ -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 field_names = { + static constexpr size_t fields_number = 1; + static constexpr std::array field_names = { "sword_attack_damage" }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 1; }; flatbuffers::Offset 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 constexpr auto name = "Movie"; static constexpr auto fully_qualified_name = "Movie"; - static constexpr std::array field_names = { + static constexpr size_t fields_number = 4; + static constexpr std::array field_names = { "main_character_type", "main_character", "characters_type", @@ -440,7 +441,6 @@ struct Movie::Traits { }; template using FieldType = decltype(std::declval().get_field()); - static constexpr size_t fields_number = 4; }; inline flatbuffers::Offset CreateMovieDirect(