[C++17] Add compile-time reflection for fields. (#6324)

* [C++17] Add compile-time reflection for fields.

Included in this commit is the following:

  - The C++ generator has been modified so that,
    when in C++17 mode, it will emit Table and
    Struct field traits that can be used at com-
    pile time as a form of static reflection. This
    includes field types, field names, and a tuple
    of field getter results.

  - Diffs to the cpp17 generated files. No other
    generated files are affected.

  - A unit test that also serves as an example. It
    demonstrates how to use the full power of this
    reflection to implement a full recursive
    JSON-like stringifier for Flatbuffers types,
    but without needing any runtime access to the
    *.fbs definition files; the computation is
    done using only static reflection.

Tested on Linux with gcc 10.2.0.

Fixes #6285.

* Fix int-conversion warning on MSVC.

* Try to fix std::to_string ambiguity on MSVC.

* Fix clang-format diffs.

* Fix more clang-format diffs.

* Fix last clang-format diff.

* Enable C++17 build/test for VC 19 platform in CI.

* Forgot to add value to cmake command line variable.

* Various fixes/changes in response to @vglavnyy's feedback.

* Replace "fields pack" with index-based getters.

* Fix MSVC error.

* Fix clang-format diffs.

* getter_for method returns result instead of address-of-getter.

* Next round of reviewer suggestions.

* Use type instead of hardcoded struct name.

* Fix clang-format diff.

* Add test for FieldType since it is not used in the stringify test.

* Add fields_number field to Traits struct.

* Add --cpp-static-reflection flag and put those features behind it.

* Fix clang-format diffs.

* Remove <tuple> include.
This commit is contained in:
David P. Sicilia
2021-03-05 13:01:40 -05:00
committed by GitHub
parent 4033ff5892
commit a69815f72c
9 changed files with 834 additions and 11 deletions

View File

@@ -474,6 +474,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
int8_t padding0__;
public:
struct Traits;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return TestTypeTable();
}
@@ -501,9 +502,28 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
void mutate_b(int8_t _b) {
flatbuffers::WriteScalar(&b_, _b);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return a();
else if constexpr (Index == 1) return b();
else static_assert(Index != Index, "Invalid Field Index");
}
};
FLATBUFFERS_STRUCT_END(Test, 4);
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 = {
"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 {
private:
float x_;
@@ -517,6 +537,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
int16_t padding2__;
public:
struct Traits;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return Vec3TypeTable();
}
@@ -584,15 +605,43 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
MyGame::Example::Test &mutable_test3() {
return test3_;
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return x();
else if constexpr (Index == 1) return y();
else if constexpr (Index == 2) return z();
else if constexpr (Index == 3) return test1();
else if constexpr (Index == 4) return test2();
else if constexpr (Index == 5) return test3();
else static_assert(Index != Index, "Invalid Field Index");
}
};
FLATBUFFERS_STRUCT_END(Vec3, 32);
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 = {
"x",
"y",
"z",
"test1",
"test2",
"test3"
};
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 {
private:
uint32_t id_;
uint32_t distance_;
public:
struct Traits;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return AbilityTypeTable();
}
@@ -622,9 +671,28 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
void mutate_distance(uint32_t _distance) {
flatbuffers::WriteScalar(&distance_, _distance);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return id();
else if constexpr (Index == 1) return distance();
else static_assert(Index != Index, "Invalid Field Index");
}
};
FLATBUFFERS_STRUCT_END(Ability, 8);
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 = {
"id",
"distance"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 2;
};
} // namespace Example
struct InParentNamespaceT : public flatbuffers::NativeTable {
@@ -671,6 +739,10 @@ inline flatbuffers::Offset<InParentNamespace> CreateInParentNamespace(
struct InParentNamespace::Traits {
using type = InParentNamespace;
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;
};
flatbuffers::Offset<InParentNamespace> CreateInParentNamespace(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -721,6 +793,10 @@ inline flatbuffers::Offset<Monster> CreateMonster(
struct Monster::Traits {
using type = Monster;
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;
};
flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
@@ -750,6 +826,11 @@ struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Ta
bool mutate_color(MyGame::Example::Color _color) {
return SetField<uint8_t>(VT_COLOR, static_cast<uint8_t>(_color), 2);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return color();
else static_assert(Index != Index, "Invalid Field Index");
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<uint8_t>(verifier, VT_COLOR) &&
@@ -789,6 +870,14 @@ inline flatbuffers::Offset<TestSimpleTableWithEnum> CreateTestSimpleTableWithEnu
struct TestSimpleTableWithEnum::Traits {
using type = TestSimpleTableWithEnum;
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 = {
"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);
@@ -836,6 +925,13 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
int KeyCompareWithValue(uint16_t val) const {
return static_cast<int>(count() > val) - static_cast<int>(count() < val);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return id();
else if constexpr (Index == 1) return val();
else if constexpr (Index == 2) return count();
else static_assert(Index != Index, "Invalid Field Index");
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_ID) &&
@@ -888,6 +984,16 @@ inline flatbuffers::Offset<Stat> CreateStat(
struct Stat::Traits {
using type = Stat;
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 = {
"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(
@@ -932,6 +1038,11 @@ struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
int KeyCompareWithValue(uint64_t val) const {
return static_cast<int>(id() > val) - static_cast<int>(id() < val);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return id();
else static_assert(Index != Index, "Invalid Field Index");
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<uint64_t>(verifier, VT_ID) &&
@@ -971,6 +1082,14 @@ inline flatbuffers::Offset<Referrable> CreateReferrable(
struct Referrable::Traits {
using type = Referrable;
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 = {
"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);
@@ -1423,6 +1542,60 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
flatbuffers::Vector<flatbuffers::Offset<MyGame::Example::Stat>> *mutable_scalar_key_sorted_tables() {
return GetPointer<flatbuffers::Vector<flatbuffers::Offset<MyGame::Example::Stat>> *>(VT_SCALAR_KEY_SORTED_TABLES);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return pos();
else if constexpr (Index == 1) return mana();
else if constexpr (Index == 2) return hp();
else if constexpr (Index == 3) return name();
else if constexpr (Index == 4) return inventory();
else if constexpr (Index == 5) return color();
else if constexpr (Index == 6) return test_type();
else if constexpr (Index == 7) return test();
else if constexpr (Index == 8) return test4();
else if constexpr (Index == 9) return testarrayofstring();
else if constexpr (Index == 10) return testarrayoftables();
else if constexpr (Index == 11) return enemy();
else if constexpr (Index == 12) return testnestedflatbuffer();
else if constexpr (Index == 13) return testempty();
else if constexpr (Index == 14) return testbool();
else if constexpr (Index == 15) return testhashs32_fnv1();
else if constexpr (Index == 16) return testhashu32_fnv1();
else if constexpr (Index == 17) return testhashs64_fnv1();
else if constexpr (Index == 18) return testhashu64_fnv1();
else if constexpr (Index == 19) return testhashs32_fnv1a();
else if constexpr (Index == 20) return testhashu32_fnv1a();
else if constexpr (Index == 21) return testhashs64_fnv1a();
else if constexpr (Index == 22) return testhashu64_fnv1a();
else if constexpr (Index == 23) return testarrayofbools();
else if constexpr (Index == 24) return testf();
else if constexpr (Index == 25) return testf2();
else if constexpr (Index == 26) return testf3();
else if constexpr (Index == 27) return testarrayofstring2();
else if constexpr (Index == 28) return testarrayofsortedstruct();
else if constexpr (Index == 29) return flex();
else if constexpr (Index == 30) return test5();
else if constexpr (Index == 31) return vector_of_longs();
else if constexpr (Index == 32) return vector_of_doubles();
else if constexpr (Index == 33) return parent_namespace_test();
else if constexpr (Index == 34) return vector_of_referrables();
else if constexpr (Index == 35) return single_weak_reference();
else if constexpr (Index == 36) return vector_of_weak_references();
else if constexpr (Index == 37) return vector_of_strong_referrables();
else if constexpr (Index == 38) return co_owning_reference();
else if constexpr (Index == 39) return vector_of_co_owning_references();
else if constexpr (Index == 40) return non_owning_reference();
else if constexpr (Index == 41) return vector_of_non_owning_references();
else if constexpr (Index == 42) return any_unique_type();
else if constexpr (Index == 43) return any_unique();
else if constexpr (Index == 44) return any_ambiguous_type();
else if constexpr (Index == 45) return any_ambiguous();
else if constexpr (Index == 46) return vector_of_enums();
else if constexpr (Index == 47) return signed_enum();
else if constexpr (Index == 48) return testrequirednestedflatbuffer();
else if constexpr (Index == 49) return scalar_key_sorted_tables();
else static_assert(Index != Index, "Invalid Field Index");
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<MyGame::Example::Vec3>(verifier, VT_POS) &&
@@ -1814,6 +1987,63 @@ inline flatbuffers::Offset<Monster> CreateMonster(
struct Monster::Traits {
using type = Monster;
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 = {
"pos",
"mana",
"hp",
"name",
"inventory",
"color",
"test_type",
"test",
"test4",
"testarrayofstring",
"testarrayoftables",
"enemy",
"testnestedflatbuffer",
"testempty",
"testbool",
"testhashs32_fnv1",
"testhashu32_fnv1",
"testhashs64_fnv1",
"testhashu64_fnv1",
"testhashs32_fnv1a",
"testhashu32_fnv1a",
"testhashs64_fnv1a",
"testhashu64_fnv1a",
"testarrayofbools",
"testf",
"testf2",
"testf3",
"testarrayofstring2",
"testarrayofsortedstruct",
"flex",
"test5",
"vector_of_longs",
"vector_of_doubles",
"parent_namespace_test",
"vector_of_referrables",
"single_weak_reference",
"vector_of_weak_references",
"vector_of_strong_referrables",
"co_owning_reference",
"vector_of_co_owning_references",
"non_owning_reference",
"vector_of_non_owning_references",
"any_unique_type",
"any_unique",
"any_ambiguous_type",
"any_ambiguous",
"vector_of_enums",
"signed_enum",
"testrequirednestedflatbuffer",
"scalar_key_sorted_tables"
};
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(
@@ -2054,6 +2284,22 @@ struct TypeAliases FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
flatbuffers::Vector<double> *mutable_vf64() {
return GetPointer<flatbuffers::Vector<double> *>(VT_VF64);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return i8();
else if constexpr (Index == 1) return u8();
else if constexpr (Index == 2) return i16();
else if constexpr (Index == 3) return u16();
else if constexpr (Index == 4) return i32();
else if constexpr (Index == 5) return u32();
else if constexpr (Index == 6) return i64();
else if constexpr (Index == 7) return u64();
else if constexpr (Index == 8) return f32();
else if constexpr (Index == 9) return f64();
else if constexpr (Index == 10) return v8();
else if constexpr (Index == 11) return vf64();
else static_assert(Index != Index, "Invalid Field Index");
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<int8_t>(verifier, VT_I8) &&
@@ -2161,6 +2407,25 @@ inline flatbuffers::Offset<TypeAliases> CreateTypeAliases(
struct TypeAliases::Traits {
using type = TypeAliases;
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 = {
"i8",
"u8",
"i16",
"u16",
"i32",
"u32",
"i64",
"u64",
"f32",
"f64",
"v8",
"vf64"
};
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(

View File

@@ -348,6 +348,46 @@ struct ScalarStuff FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
bool mutate_default_enum(optional_scalars::OptionalByte _default_enum) {
return SetField<int8_t>(VT_DEFAULT_ENUM, static_cast<int8_t>(_default_enum), 1);
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return just_i8();
else if constexpr (Index == 1) return maybe_i8();
else if constexpr (Index == 2) return default_i8();
else if constexpr (Index == 3) return just_u8();
else if constexpr (Index == 4) return maybe_u8();
else if constexpr (Index == 5) return default_u8();
else if constexpr (Index == 6) return just_i16();
else if constexpr (Index == 7) return maybe_i16();
else if constexpr (Index == 8) return default_i16();
else if constexpr (Index == 9) return just_u16();
else if constexpr (Index == 10) return maybe_u16();
else if constexpr (Index == 11) return default_u16();
else if constexpr (Index == 12) return just_i32();
else if constexpr (Index == 13) return maybe_i32();
else if constexpr (Index == 14) return default_i32();
else if constexpr (Index == 15) return just_u32();
else if constexpr (Index == 16) return maybe_u32();
else if constexpr (Index == 17) return default_u32();
else if constexpr (Index == 18) return just_i64();
else if constexpr (Index == 19) return maybe_i64();
else if constexpr (Index == 20) return default_i64();
else if constexpr (Index == 21) return just_u64();
else if constexpr (Index == 22) return maybe_u64();
else if constexpr (Index == 23) return default_u64();
else if constexpr (Index == 24) return just_f32();
else if constexpr (Index == 25) return maybe_f32();
else if constexpr (Index == 26) return default_f32();
else if constexpr (Index == 27) return just_f64();
else if constexpr (Index == 28) return maybe_f64();
else if constexpr (Index == 29) return default_f64();
else if constexpr (Index == 30) return just_bool();
else if constexpr (Index == 31) return maybe_bool();
else if constexpr (Index == 32) return default_bool();
else if constexpr (Index == 33) return just_enum();
else if constexpr (Index == 34) return maybe_enum();
else if constexpr (Index == 35) return default_enum();
else static_assert(Index != Index, "Invalid Field Index");
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<int8_t>(verifier, VT_JUST_I8) &&
@@ -597,6 +637,49 @@ inline flatbuffers::Offset<ScalarStuff> CreateScalarStuff(
struct ScalarStuff::Traits {
using type = ScalarStuff;
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 = {
"just_i8",
"maybe_i8",
"default_i8",
"just_u8",
"maybe_u8",
"default_u8",
"just_i16",
"maybe_i16",
"default_i16",
"just_u16",
"maybe_u16",
"default_u16",
"just_i32",
"maybe_i32",
"default_i32",
"just_u32",
"maybe_u32",
"default_u32",
"just_i64",
"maybe_i64",
"default_i64",
"just_u64",
"maybe_u64",
"default_u64",
"just_f32",
"maybe_f32",
"default_f32",
"just_f64",
"maybe_f64",
"default_f64",
"just_bool",
"maybe_bool",
"default_bool",
"just_enum",
"maybe_enum",
"default_enum"
};
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);