mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
[C++17] Add Traits class for Tables and Factory function within it. (#5678)
* Include flattests_cpp17 in unit tests when C++17 build is enabled.
* [C++17] Generate generic table factory function.
1. For each table, generate a convenient free-standing factory
function that allows creating the table in a generic way by
specifying only the type. This is the first change in a series
of changes to make Flatbuffers generated C++ code more friendly
to code bases that make use of C++ template metaprogramming
techniques to manage the serialization process. Example:
Before :(
// The name of the Flatbuffers type (and namespace) must
// be hard-coded when writing the factory function.
auto monster = MyGame::Example::CreateMonster(fbb, ...);
After :)
using type_to_create = MyGame::Example::Monster;
// No namespace needed on CreateByTagType.
auto monster = CreateByTagType((type_to_create*)nullptr,
fbb, ...);
This feature requires building with C++14 or greater, and thus
it is guarded behind --cpp-std >= c++17 in the flatbuffers C++
generator.
2. Fix a CMake bug to include C++17 unit tests in test suite.
* [C++17] Replace standalone variadic factory function with type_traits.
Add a `type_traits` to each table class. This `type_traits` can be
populated with various compile-time info about the table. Initially,
we have the Create* function and type, but is extensible in the future.
* Remove empty line and fix stale comments.
* Rename type_traits to Traits and move fwd declaration.
* Fix parameter evaluation order issue and use lambda for scope.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
3cd9b6434a
commit
a5d9d0f7d3
@@ -621,6 +621,7 @@ struct InParentNamespaceT : public flatbuffers::NativeTable {
|
||||
struct InParentNamespace FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef InParentNamespaceT NativeTableType;
|
||||
typedef InParentNamespaceBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return InParentNamespaceTypeTable();
|
||||
}
|
||||
@@ -655,6 +656,11 @@ inline flatbuffers::Offset<InParentNamespace> CreateInParentNamespace(
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct InParentNamespace::Traits {
|
||||
using type = InParentNamespace;
|
||||
static auto constexpr Create = CreateInParentNamespace;
|
||||
};
|
||||
|
||||
flatbuffers::Offset<InParentNamespace> CreateInParentNamespace(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
|
||||
|
||||
namespace Example2 {
|
||||
@@ -668,6 +674,7 @@ struct MonsterT : public flatbuffers::NativeTable {
|
||||
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef MonsterT NativeTableType;
|
||||
typedef MonsterBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return MonsterTypeTable();
|
||||
}
|
||||
@@ -702,6 +709,11 @@ inline flatbuffers::Offset<Monster> CreateMonster(
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct Monster::Traits {
|
||||
using type = Monster;
|
||||
static auto constexpr Create = CreateMonster;
|
||||
};
|
||||
|
||||
flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
|
||||
|
||||
} // namespace Example2
|
||||
@@ -719,6 +731,7 @@ struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable {
|
||||
struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef TestSimpleTableWithEnumT NativeTableType;
|
||||
typedef TestSimpleTableWithEnumBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return TestSimpleTableWithEnumTypeTable();
|
||||
}
|
||||
@@ -768,6 +781,11 @@ inline flatbuffers::Offset<TestSimpleTableWithEnum> CreateTestSimpleTableWithEnu
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct TestSimpleTableWithEnum::Traits {
|
||||
using type = TestSimpleTableWithEnum;
|
||||
static auto constexpr Create = CreateTestSimpleTableWithEnum;
|
||||
};
|
||||
|
||||
flatbuffers::Offset<TestSimpleTableWithEnum> CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
|
||||
|
||||
struct StatT : public flatbuffers::NativeTable {
|
||||
@@ -784,6 +802,7 @@ struct StatT : public flatbuffers::NativeTable {
|
||||
struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef StatT NativeTableType;
|
||||
typedef StatBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return StatTypeTable();
|
||||
}
|
||||
@@ -860,6 +879,11 @@ inline flatbuffers::Offset<Stat> CreateStat(
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct Stat::Traits {
|
||||
using type = Stat;
|
||||
static auto constexpr Create = CreateStat;
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Stat> CreateStatDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const char *id = nullptr,
|
||||
@@ -886,6 +910,7 @@ struct ReferrableT : public flatbuffers::NativeTable {
|
||||
struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef ReferrableT NativeTableType;
|
||||
typedef ReferrableBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return ReferrableTypeTable();
|
||||
}
|
||||
@@ -941,6 +966,11 @@ inline flatbuffers::Offset<Referrable> CreateReferrable(
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct Referrable::Traits {
|
||||
using type = Referrable;
|
||||
static auto constexpr Create = CreateReferrable;
|
||||
};
|
||||
|
||||
flatbuffers::Offset<Referrable> CreateReferrable(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
|
||||
|
||||
struct MonsterT : public flatbuffers::NativeTable {
|
||||
@@ -1017,6 +1047,7 @@ struct MonsterT : public flatbuffers::NativeTable {
|
||||
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef MonsterT NativeTableType;
|
||||
typedef MonsterBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return MonsterTypeTable();
|
||||
}
|
||||
@@ -1766,6 +1797,11 @@ inline flatbuffers::Offset<Monster> CreateMonster(
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct Monster::Traits {
|
||||
using type = Monster;
|
||||
static auto constexpr Create = CreateMonster;
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Monster> CreateMonsterDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const MyGame::Example::Vec3 *pos = 0,
|
||||
@@ -1920,6 +1956,7 @@ struct TypeAliasesT : public flatbuffers::NativeTable {
|
||||
struct TypeAliases FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef TypeAliasesT NativeTableType;
|
||||
typedef TypeAliasesBuilder Builder;
|
||||
struct Traits;
|
||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return TypeAliasesTypeTable();
|
||||
}
|
||||
@@ -2114,6 +2151,11 @@ inline flatbuffers::Offset<TypeAliases> CreateTypeAliases(
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
struct TypeAliases::Traits {
|
||||
using type = TypeAliases;
|
||||
static auto constexpr Create = CreateTypeAliases;
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<TypeAliases> CreateTypeAliasesDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
int8_t i8 = 0,
|
||||
|
||||
Reference in New Issue
Block a user