From bc2ec7119bd95230c4eb9a730a5439472efe01fa Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Fri, 2 Dec 2016 14:25:39 -0800 Subject: [PATCH] Adds typedefs and functions to help write generic code. The following changes have been made to the C++ codegen to enable writing generic code that uses the Table and NativeTable types. - Adds TableType and NativeTableType typedefs to NativeTable and Table structs. - Adds GetFullyQualifiedName() to NativeTables if --gen-name-strings is set. - Adds a static Pack function to Tables that simply calls the global CreateX functions. See cr/140391505 as an example of improved usage. From cl/140529288. Change-Id: Idec137c16129e15c1783f94fabdcea24aeeaaef6 --- samples/monster_generated.h | 14 ++++++++++++++ src/idl_gen_cpp.cpp | 24 ++++++++++++++++++++++++ tests/monster_test_generated.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/samples/monster_generated.h b/samples/monster_generated.h index fa12e0600..1833c62d5 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -92,6 +92,7 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { STRUCT_END(Vec3, 12); struct MonsterT : public flatbuffers::NativeTable { + typedef Monster TableType; std::unique_ptr pos; int16_t mana; int16_t hp; @@ -103,6 +104,7 @@ struct MonsterT : public flatbuffers::NativeTable { }; struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef MonsterT NativeTableType; enum { VT_POS = 4, VT_MANA = 6, @@ -151,6 +153,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.EndTable(); } MonsterT *UnPack(const flatbuffers::resolver_function_t *resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct MonsterBuilder { @@ -212,11 +215,13 @@ inline flatbuffers::Offset CreateMonsterDirect(flatbuffers::FlatBufferB inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *rehasher = nullptr); struct WeaponT : public flatbuffers::NativeTable { + typedef Weapon TableType; std::string name; int16_t damage; }; struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef WeaponT NativeTableType; enum { VT_NAME = 4, VT_DAMAGE = 6 @@ -233,6 +238,7 @@ struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.EndTable(); } WeaponT *UnPack(const flatbuffers::resolver_function_t *resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const WeaponT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct WeaponBuilder { @@ -280,6 +286,10 @@ inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *resolve return _o; } +inline flatbuffers::Offset Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateMonster(_fbb, _o, _rehasher); +} + inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *rehasher) { (void)rehasher; return CreateMonster(_fbb, @@ -302,6 +312,10 @@ inline WeaponT *Weapon::UnPack(const flatbuffers::resolver_function_t *resolver) return _o; } +inline flatbuffers::Offset Weapon::Pack(flatbuffers::FlatBufferBuilder &_fbb, const WeaponT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateWeapon(_fbb, _o, _rehasher); +} + inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, const WeaponT *_o, const flatbuffers::rehasher_function_t *rehasher) { (void)rehasher; return CreateWeapon(_fbb, diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 7ca7d0bd2..8681e1749 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -413,6 +413,16 @@ class CppGenerator : public BaseGenerator { (predecl ? " = nullptr" : "") + ")"; } + std::string TablePackSignature(StructDef &struct_def, bool inclass) { + return std::string(inclass ? "static " : "") + + "flatbuffers::Offset<" + struct_def.name + "> " + + (inclass ? "" : struct_def.name + "::") + + "Pack(flatbuffers::FlatBufferBuilder &_fbb, " + + "const " + NativeName(struct_def.name) + "* _o, " + + "const flatbuffers::rehasher_function_t *_rehasher" + + (inclass ? " = nullptr" : "") + ")"; + } + std::string TableUnPackSignature(StructDef &struct_def, bool inclass) { return NativeName(struct_def.name) + " *" + (inclass ? "" : struct_def.name + "::") + @@ -691,6 +701,9 @@ class CppGenerator : public BaseGenerator { // table. code += "struct " + NativeName(struct_def.name); code += " : public flatbuffers::NativeTable {\n"; + code += " typedef " + struct_def.name + " TableType;\n"; + // Generate GetFullyQualifiedName + GenFullyQualifiedNameGetter(NativeName(struct_def.name), code); for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; @@ -711,6 +724,10 @@ class CppGenerator : public BaseGenerator { code += "struct " + struct_def.name; code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; code += " {\n"; + if (parser_.opts.generate_object_based_api) { + code += " typedef " + NativeName(struct_def.name) + + " NativeTableType;\n"; + } // Generate GetFullyQualifiedName GenFullyQualifiedNameGetter(struct_def.name, code); // Generate field id constants. @@ -874,6 +891,7 @@ class CppGenerator : public BaseGenerator { if (parser_.opts.generate_object_based_api) { // Generate the UnPack() pre declaration. code += " " + TableUnPackSignature(struct_def, true) + ";\n"; + code += " " + TablePackSignature(struct_def, true) + ";\n"; } code += "};\n\n"; // End of table. @@ -1105,6 +1123,12 @@ class CppGenerator : public BaseGenerator { } code += " return _o;\n}\n\n"; + // Generate the X::Pack member function that simply calls the global + // CreateX function. + code += "inline " + TablePackSignature(struct_def, false) + " {\n"; + code += " return Create" + struct_def.name + "(_fbb, _o, _rehasher);\n"; + code += "}\n\n"; + // Generate a CreateX method that works with an unpacked C++ object. code += TableCreateSignature(struct_def, false) + " {\n"; code += " (void)rehasher;\n"; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 6e8831e2a..f1be4ace1 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -151,14 +151,17 @@ STRUCT_END(Vec3, 32); namespace Example2 { struct MonsterT : public flatbuffers::NativeTable { + typedef Monster TableType; }; struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef MonsterT NativeTableType; bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && verifier.EndTable(); } MonsterT *UnPack(const flatbuffers::resolver_function_t *resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct MonsterBuilder { @@ -184,10 +187,12 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder namespace Example { struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable { + typedef TestSimpleTableWithEnum TableType; Color color; }; struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef TestSimpleTableWithEnumT NativeTableType; enum { VT_COLOR = 4 }; @@ -199,6 +204,7 @@ struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Ta verifier.EndTable(); } TestSimpleTableWithEnumT *UnPack(const flatbuffers::resolver_function_t *resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct TestSimpleTableWithEnumBuilder { @@ -223,12 +229,14 @@ inline flatbuffers::Offset CreateTestSimpleTableWithEnu inline flatbuffers::Offset CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *rehasher = nullptr); struct StatT : public flatbuffers::NativeTable { + typedef Stat TableType; std::string id; int64_t val; uint16_t count; }; struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef StatT NativeTableType; enum { VT_ID = 4, VT_VAL = 6, @@ -249,6 +257,7 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.EndTable(); } StatT *UnPack(const flatbuffers::resolver_function_t *resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const StatT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct StatBuilder { @@ -286,6 +295,7 @@ inline flatbuffers::Offset CreateStatDirect(flatbuffers::FlatBufferBuilder inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, const StatT *_o, const flatbuffers::rehasher_function_t *rehasher = nullptr); struct MonsterT : public flatbuffers::NativeTable { + typedef Monster TableType; std::unique_ptr pos; int16_t mana; int16_t hp; @@ -317,6 +327,7 @@ struct MonsterT : public flatbuffers::NativeTable { /// an example documentation comment: monster object struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef MonsterT NativeTableType; enum { VT_POS = 4, VT_MANA = 6, @@ -455,6 +466,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.EndTable(); } MonsterT *UnPack(const flatbuffers::resolver_function_t *resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct MonsterBuilder { @@ -602,6 +614,10 @@ inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *resolve return _o; } +inline flatbuffers::Offset Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateMonster(_fbb, _o, _rehasher); +} + inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *rehasher) { (void)rehasher; (void)_o; @@ -619,6 +635,10 @@ inline TestSimpleTableWithEnumT *TestSimpleTableWithEnum::UnPack(const flatbuffe return _o; } +inline flatbuffers::Offset TestSimpleTableWithEnum::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateTestSimpleTableWithEnum(_fbb, _o, _rehasher); +} + inline flatbuffers::Offset CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *rehasher) { (void)rehasher; return CreateTestSimpleTableWithEnum(_fbb, @@ -634,6 +654,10 @@ inline StatT *Stat::UnPack(const flatbuffers::resolver_function_t *resolver) con return _o; } +inline flatbuffers::Offset Stat::Pack(flatbuffers::FlatBufferBuilder &_fbb, const StatT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateStat(_fbb, _o, _rehasher); +} + inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, const StatT *_o, const flatbuffers::rehasher_function_t *rehasher) { (void)rehasher; return CreateStat(_fbb, @@ -676,6 +700,10 @@ inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *resolve return _o; } +inline flatbuffers::Offset Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateMonster(_fbb, _o, _rehasher); +} + inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *rehasher) { (void)rehasher; return CreateMonster(_fbb,