Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: mini_reflect: Add DefaultTypeTable #4614

Merged
merged 5 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions samples/monster_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ struct MonsterT;
struct Weapon;
struct WeaponT;

inline flatbuffers::TypeTable *Vec3TypeTable();

inline flatbuffers::TypeTable *MonsterTypeTable();

inline flatbuffers::TypeTable *WeaponTypeTable();

enum Color {
Color_Red = 0,
Color_Green = 1,
Expand Down Expand Up @@ -185,6 +191,9 @@ struct MonsterT : public flatbuffers::NativeTable {

struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return MonsterTypeTable();
}
enum {
VT_POS = 4,
VT_MANA = 6,
Expand Down Expand Up @@ -384,6 +393,9 @@ struct WeaponT : public flatbuffers::NativeTable {

struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef WeaponT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return WeaponTypeTable();
}
enum {
VT_NAME = 4,
VT_DAMAGE = 6
Expand Down Expand Up @@ -603,12 +615,6 @@ inline void EquipmentUnion::Reset() {
type = Equipment_NONE;
}

inline flatbuffers::TypeTable *Vec3TypeTable();

inline flatbuffers::TypeTable *MonsterTypeTable();

inline flatbuffers::TypeTable *WeaponTypeTable();

inline flatbuffers::TypeTable *ColorTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_CHAR, 0, 0 },
Expand Down
27 changes: 18 additions & 9 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ class CppGenerator : public BaseGenerator {
code_ += "";
}
}
// Generate preablmle code for mini reflection.
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
// To break cyclic dependencies, first pre-declare all tables/structs.
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
const auto &struct_def = **it;
if (!struct_def.generated) {
SetNameSpace(struct_def.defined_namespace);
GenMiniReflectPre(&struct_def);
}
}
}

// Generate code for all the enum declarations.
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
Expand Down Expand Up @@ -280,15 +292,6 @@ class CppGenerator : public BaseGenerator {

// Generate code for mini reflection.
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
// To break cyclic dependencies, first pre-declare all tables/structs.
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
const auto &struct_def = **it;
if (!struct_def.generated) {
SetNameSpace(struct_def.defined_namespace);
GenMiniReflectPre(&struct_def);
}
}
// Then the unions/enums that may refer to them.
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
Expand Down Expand Up @@ -1503,6 +1506,12 @@ class CppGenerator : public BaseGenerator {
if (parser_.opts.generate_object_based_api) {
code_ += " typedef {{NATIVE_NAME}} NativeTableType;";
}
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
code_ += " static flatbuffers::TypeTable *MiniReflectTypeTable() {";
code_ += " return {{STRUCT_NAME}}TypeTable();";
code_ += " }";
}


GenFullyQualifiedNameGetter(struct_def, Name(struct_def));

Expand Down
70 changes: 44 additions & 26 deletions tests/monster_test_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ struct MonsterT;
struct TypeAliases;
struct TypeAliasesT;

} // namespace Example

inline flatbuffers::TypeTable *InParentNamespaceTypeTable();

namespace Example2 {

inline flatbuffers::TypeTable *MonsterTypeTable();

} // namespace Example2

namespace Example {

inline flatbuffers::TypeTable *TestTypeTable();

inline flatbuffers::TypeTable *TestSimpleTableWithEnumTypeTable();

inline flatbuffers::TypeTable *Vec3TypeTable();

inline flatbuffers::TypeTable *AbilityTypeTable();

inline flatbuffers::TypeTable *StatTypeTable();

inline flatbuffers::TypeTable *MonsterTypeTable();

inline flatbuffers::TypeTable *TypeAliasesTypeTable();

enum Color {
Color_Red = 1,
Color_Green = 2,
Expand Down Expand Up @@ -332,6 +358,9 @@ struct InParentNamespaceT : public flatbuffers::NativeTable {

struct InParentNamespace FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef InParentNamespaceT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return InParentNamespaceTypeTable();
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
verifier.EndTable();
Expand Down Expand Up @@ -374,6 +403,9 @@ struct MonsterT : public flatbuffers::NativeTable {

struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return MonsterTypeTable();
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
verifier.EndTable();
Expand Down Expand Up @@ -420,6 +452,9 @@ struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable {

struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef TestSimpleTableWithEnumT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return TestSimpleTableWithEnumTypeTable();
}
enum {
VT_COLOR = 4
};
Expand Down Expand Up @@ -480,6 +515,9 @@ struct StatT : public flatbuffers::NativeTable {

struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef StatT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return StatTypeTable();
}
enum {
VT_ID = 4,
VT_VAL = 6,
Expand Down Expand Up @@ -623,6 +661,9 @@ struct MonsterT : public flatbuffers::NativeTable {
/// an example documentation comment: monster object
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return MonsterTypeTable();
}
enum {
VT_POS = 4,
VT_MANA = 6,
Expand Down Expand Up @@ -1261,6 +1302,9 @@ struct TypeAliasesT : public flatbuffers::NativeTable {

struct TypeAliases FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef TypeAliasesT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return TypeAliasesTypeTable();
}
enum {
VT_I8 = 4,
VT_U8 = 6,
Expand Down Expand Up @@ -1890,32 +1934,6 @@ inline void AnyUnion::Reset() {
type = Any_NONE;
}

} // namespace Example

inline flatbuffers::TypeTable *InParentNamespaceTypeTable();

namespace Example2 {

inline flatbuffers::TypeTable *MonsterTypeTable();

} // namespace Example2

namespace Example {

inline flatbuffers::TypeTable *TestTypeTable();

inline flatbuffers::TypeTable *TestSimpleTableWithEnumTypeTable();

inline flatbuffers::TypeTable *Vec3TypeTable();

inline flatbuffers::TypeTable *AbilityTypeTable();

inline flatbuffers::TypeTable *StatTypeTable();

inline flatbuffers::TypeTable *MonsterTypeTable();

inline flatbuffers::TypeTable *TypeAliasesTypeTable();

inline flatbuffers::TypeTable *ColorTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_CHAR, 0, 0 },
Expand Down
11 changes: 7 additions & 4 deletions tests/namespace_test/namespace_test1_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct TableInNestedNS;

struct StructInNestedNS;

inline flatbuffers::TypeTable *TableInNestedNSTypeTable();

inline flatbuffers::TypeTable *StructInNestedNSTypeTable();

enum EnumInNestedNS {
EnumInNestedNS_A = 0,
EnumInNestedNS_B = 1,
Expand Down Expand Up @@ -74,6 +78,9 @@ MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS {
STRUCT_END(StructInNestedNS, 8);

struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return TableInNestedNSTypeTable();
}
enum {
VT_FOO = 4
};
Expand Down Expand Up @@ -116,10 +123,6 @@ inline flatbuffers::Offset<TableInNestedNS> CreateTableInNestedNS(
return builder_.Finish();
}

inline flatbuffers::TypeTable *TableInNestedNSTypeTable();

inline flatbuffers::TypeTable *StructInNestedNSTypeTable();

inline flatbuffers::TypeTable *EnumInNestedNSTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_CHAR, 0, 0 },
Expand Down
37 changes: 23 additions & 14 deletions tests/namespace_test/namespace_test2_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ namespace NamespaceA {

struct SecondTableInA;

inline flatbuffers::TypeTable *TableInFirstNSTypeTable();

} // namespace NamespaceA

namespace NamespaceC {

inline flatbuffers::TypeTable *TableInCTypeTable();

} // namespace NamespaceC

namespace NamespaceA {

inline flatbuffers::TypeTable *SecondTableInATypeTable();

struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return TableInFirstNSTypeTable();
}
enum {
VT_FOO_TABLE = 4,
VT_FOO_ENUM = 6,
Expand Down Expand Up @@ -99,6 +116,9 @@ inline flatbuffers::Offset<TableInFirstNS> CreateTableInFirstNS(
namespace NamespaceC {

struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return TableInCTypeTable();
}
enum {
VT_REFER_TO_A1 = 4,
VT_REFER_TO_A2 = 6
Expand Down Expand Up @@ -161,6 +181,9 @@ inline flatbuffers::Offset<TableInC> CreateTableInC(
namespace NamespaceA {

struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return SecondTableInATypeTable();
}
enum {
VT_REFER_TO_C = 4
};
Expand Down Expand Up @@ -212,20 +235,6 @@ namespace NamespaceC {

namespace NamespaceA {

inline flatbuffers::TypeTable *TableInFirstNSTypeTable();

} // namespace NamespaceA

namespace NamespaceC {

inline flatbuffers::TypeTable *TableInCTypeTable();

} // namespace NamespaceC

namespace NamespaceA {

inline flatbuffers::TypeTable *SecondTableInATypeTable();

inline flatbuffers::TypeTable *TableInFirstNSTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, 0 },
Expand Down
2 changes: 1 addition & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) {
}

void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
auto s = flatbuffers::FlatBufferToString(flatbuf, MonsterTypeTable());
auto s = flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
TEST_EQ_STR(
s.c_str(),
"{ "
Expand Down
22 changes: 14 additions & 8 deletions tests/union_vector/union_vector_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ struct BookReader;
struct Movie;
struct MovieT;

inline flatbuffers::TypeTable *AttackerTypeTable();

inline flatbuffers::TypeTable *RapunzelTypeTable();

inline flatbuffers::TypeTable *BookReaderTypeTable();

inline flatbuffers::TypeTable *MovieTypeTable();

enum Character {
Character_NONE = 0,
Character_MuLan = 1,
Expand Down Expand Up @@ -183,6 +191,9 @@ struct AttackerT : public flatbuffers::NativeTable {

struct Attacker FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef AttackerT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return AttackerTypeTable();
}
enum {
VT_SWORD_ATTACK_DAMAGE = 4
};
Expand Down Expand Up @@ -240,6 +251,9 @@ struct MovieT : public flatbuffers::NativeTable {

struct Movie FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MovieT NativeTableType;
static flatbuffers::TypeTable *MiniReflectTypeTable() {
return MovieTypeTable();
}
enum {
VT_MAIN_CHARACTER_TYPE = 4,
VT_MAIN_CHARACTER = 6,
Expand Down Expand Up @@ -595,14 +609,6 @@ inline void CharacterUnion::Reset() {
type = Character_NONE;
}

inline flatbuffers::TypeTable *AttackerTypeTable();

inline flatbuffers::TypeTable *RapunzelTypeTable();

inline flatbuffers::TypeTable *BookReaderTypeTable();

inline flatbuffers::TypeTable *MovieTypeTable();

inline flatbuffers::TypeTable *CharacterTypeTable() {
static flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, -1 },
Expand Down