From 962751a6ec4a0cf9552b1291231edfc60210e77b Mon Sep 17 00:00:00 2001 From: Mika Raento Date: Mon, 21 Jun 2021 21:37:56 +0300 Subject: [PATCH] Improve generated comparisons for tables (#6486) * Improve generated comparisons for tables Previously, the generated code called std::unique_ptr<>::operator== on non-scalar members, which would be correct only if the member was null on both sides. After this CL, comparison is done on the pointed-to values (including using a default-constructed value if the pointer is null). * Don't equate null Tables with all defaults Also removes the cost of default-constructing tables for comparisons. * Regenerate code * fix formatting --- samples/monster_generated.h | 60 ++-- src/idl_gen_cpp.cpp | 26 +- tests/arrays_test_generated.h | 21 +- tests/monster_extra_generated.h | 39 +-- tests/monster_test_generated.h | 261 +++++++++--------- .../namespace_test1_generated.h | 21 +- .../namespace_test2_generated.h | 71 ++--- tests/optional_scalars_generated.h | 91 +++--- tests/test.cpp | 25 ++ tests/union_vector/union_vector_generated.h | 44 +-- 10 files changed, 362 insertions(+), 297 deletions(-) diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 9b70be4a7..574026501 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -235,24 +235,6 @@ struct MonsterT : public flatbuffers::NativeTable { std::vector path{}; }; -inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) { - return - (lhs.pos == rhs.pos) && - (lhs.mana == rhs.mana) && - (lhs.hp == rhs.hp) && - (lhs.name == rhs.name) && - (lhs.inventory == rhs.inventory) && - (lhs.color == rhs.color) && - (lhs.weapons == rhs.weapons) && - (lhs.equipped == rhs.equipped) && - (lhs.path == rhs.path); -} - -inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) { - return !(lhs == rhs); -} - - struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef MonsterT NativeTableType; typedef MonsterBuilder Builder; @@ -470,17 +452,6 @@ struct WeaponT : public flatbuffers::NativeTable { int16_t damage = 0; }; -inline bool operator==(const WeaponT &lhs, const WeaponT &rhs) { - return - (lhs.name == rhs.name) && - (lhs.damage == rhs.damage); -} - -inline bool operator!=(const WeaponT &lhs, const WeaponT &rhs) { - return !(lhs == rhs); -} - - struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef WeaponT NativeTableType; typedef WeaponBuilder Builder; @@ -559,6 +530,25 @@ inline flatbuffers::Offset CreateWeaponDirect( flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, const WeaponT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) { + return + ((lhs.pos == rhs.pos) || (lhs.pos && rhs.pos && *lhs.pos == *rhs.pos)) && + (lhs.mana == rhs.mana) && + (lhs.hp == rhs.hp) && + (lhs.name == rhs.name) && + (lhs.inventory == rhs.inventory) && + (lhs.color == rhs.color) && + (lhs.weapons == rhs.weapons) && + (lhs.equipped == rhs.equipped) && + (lhs.path == rhs.path); +} + +inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) { + return !(lhs == rhs); +} + + inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new MonsterT()); UnPackTo(_o.get(), _resolver); @@ -612,6 +602,18 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder _path); } + +inline bool operator==(const WeaponT &lhs, const WeaponT &rhs) { + return + (lhs.name == rhs.name) && + (lhs.damage == rhs.damage); +} + +inline bool operator!=(const WeaponT &lhs, const WeaponT &rhs) { + return !(lhs == rhs); +} + + inline WeaponT *Weapon::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new WeaponT()); UnPackTo(_o.get(), _resolver); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 201b32c9f..c80ec90f2 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1819,7 +1819,18 @@ class CppGenerator : public BaseGenerator { field.value.type.element != BASE_TYPE_UTYPE)) { if (!compare_op.empty()) { compare_op += " &&\n "; } auto accessor = Name(field) + accessSuffix; - compare_op += "(lhs." + accessor + " == rhs." + accessor + ")"; + if (struct_def.fixed || + field.value.type.base_type != BASE_TYPE_STRUCT) { + compare_op += "(lhs." + accessor + " == rhs." + accessor + ")"; + } else { + // Deep compare of std::unique_ptr. Null is not equal to empty. + std::string both_null = + "(lhs." + accessor + " == rhs." + accessor + ")"; + std::string not_null_and_equal = "(lhs." + accessor + " && rhs." + + accessor + " && *lhs." + accessor + + " == *rhs." + accessor + ")"; + compare_op += "(" + both_null + " || " + not_null_and_equal + ")"; + } } } @@ -1885,10 +1896,19 @@ class CppGenerator : public BaseGenerator { GenOperatorNewDelete(struct_def); GenDefaultConstructor(struct_def); code_ += "};"; - if (opts_.gen_compare) GenCompareOperator(struct_def); code_ += ""; } + void GenNativeTablePost(const StructDef &struct_def) { + if (opts_.gen_compare) { + const auto native_name = NativeName(Name(struct_def), &struct_def, opts_); + code_.SetValue("STRUCT_NAME", Name(struct_def)); + code_.SetValue("NATIVE_NAME", native_name); + GenCompareOperator(struct_def); + code_ += ""; + } + } + // Generate the code to call the appropriate Verify function(s) for a field. void GenVerifyCall(const FieldDef &field, const char *prefix) { code_.SetValue("PRE", prefix); @@ -2999,6 +3019,8 @@ class CppGenerator : public BaseGenerator { // Generate code for tables that needs to come after the regular definition. void GenTablePost(const StructDef &struct_def) { + if (opts_.generate_object_based_api) { GenNativeTablePost(struct_def); } + code_.SetValue("STRUCT_NAME", Name(struct_def)); code_.SetValue("NATIVE_NAME", NativeName(Name(struct_def), &struct_def, opts_)); diff --git a/tests/arrays_test_generated.h b/tests/arrays_test_generated.h index d7660cfa6..e2163ee64 100644 --- a/tests/arrays_test_generated.h +++ b/tests/arrays_test_generated.h @@ -267,16 +267,6 @@ struct ArrayTableT : public flatbuffers::NativeTable { flatbuffers::unique_ptr a{}; }; -inline bool operator==(const ArrayTableT &lhs, const ArrayTableT &rhs) { - return - (lhs.a == rhs.a); -} - -inline bool operator!=(const ArrayTableT &lhs, const ArrayTableT &rhs) { - return !(lhs == rhs); -} - - struct ArrayTable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef ArrayTableT NativeTableType; typedef ArrayTableBuilder Builder; @@ -330,6 +320,17 @@ inline flatbuffers::Offset CreateArrayTable( flatbuffers::Offset CreateArrayTable(flatbuffers::FlatBufferBuilder &_fbb, const ArrayTableT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const ArrayTableT &lhs, const ArrayTableT &rhs) { + return + ((lhs.a == rhs.a) || (lhs.a && rhs.a && *lhs.a == *rhs.a)); +} + +inline bool operator!=(const ArrayTableT &lhs, const ArrayTableT &rhs) { + return !(lhs == rhs); +} + + inline ArrayTableT *ArrayTable::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new ArrayTableT()); UnPackTo(_o.get(), _resolver); diff --git a/tests/monster_extra_generated.h b/tests/monster_extra_generated.h index be4801fb4..bf7c86e34 100644 --- a/tests/monster_extra_generated.h +++ b/tests/monster_extra_generated.h @@ -31,25 +31,6 @@ struct MonsterExtraT : public flatbuffers::NativeTable { std::vector fvec{}; }; -inline bool operator==(const MonsterExtraT &lhs, const MonsterExtraT &rhs) { - return - (lhs.d0 == rhs.d0) && - (lhs.d1 == rhs.d1) && - (lhs.d2 == rhs.d2) && - (lhs.d3 == rhs.d3) && - (lhs.f0 == rhs.f0) && - (lhs.f1 == rhs.f1) && - (lhs.f2 == rhs.f2) && - (lhs.f3 == rhs.f3) && - (lhs.dvec == rhs.dvec) && - (lhs.fvec == rhs.fvec); -} - -inline bool operator!=(const MonsterExtraT &lhs, const MonsterExtraT &rhs) { - return !(lhs == rhs); -} - - struct MonsterExtra FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef MonsterExtraT NativeTableType; typedef MonsterExtraBuilder Builder; @@ -250,6 +231,26 @@ inline flatbuffers::Offset CreateMonsterExtraDirect( flatbuffers::Offset CreateMonsterExtra(flatbuffers::FlatBufferBuilder &_fbb, const MonsterExtraT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const MonsterExtraT &lhs, const MonsterExtraT &rhs) { + return + (lhs.d0 == rhs.d0) && + (lhs.d1 == rhs.d1) && + (lhs.d2 == rhs.d2) && + (lhs.d3 == rhs.d3) && + (lhs.f0 == rhs.f0) && + (lhs.f1 == rhs.f1) && + (lhs.f2 == rhs.f2) && + (lhs.f3 == rhs.f3) && + (lhs.dvec == rhs.dvec) && + (lhs.fvec == rhs.fvec); +} + +inline bool operator!=(const MonsterExtraT &lhs, const MonsterExtraT &rhs) { + return !(lhs == rhs); +} + + inline MonsterExtraT *MonsterExtra::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new MonsterExtraT()); UnPackTo(_o.get(), _resolver); diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 4ec7ed041..ce4ec2412 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -842,15 +842,6 @@ struct InParentNamespaceT : public flatbuffers::NativeTable { typedef InParentNamespace TableType; }; -inline bool operator==(const InParentNamespaceT &, const InParentNamespaceT &) { - return true; -} - -inline bool operator!=(const InParentNamespaceT &lhs, const InParentNamespaceT &rhs) { - return !(lhs == rhs); -} - - struct InParentNamespace FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef InParentNamespaceT NativeTableType; typedef InParentNamespaceBuilder Builder; @@ -895,15 +886,6 @@ struct MonsterT : public flatbuffers::NativeTable { typedef Monster TableType; }; -inline bool operator==(const MonsterT &, const MonsterT &) { - return true; -} - -inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) { - return !(lhs == rhs); -} - - struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef MonsterT NativeTableType; typedef MonsterBuilder Builder; @@ -951,16 +933,6 @@ struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable { MyGame::Example::Color color = MyGame::Example::Color_Green; }; -inline bool operator==(const TestSimpleTableWithEnumT &lhs, const TestSimpleTableWithEnumT &rhs) { - return - (lhs.color == rhs.color); -} - -inline bool operator!=(const TestSimpleTableWithEnumT &lhs, const TestSimpleTableWithEnumT &rhs) { - return !(lhs == rhs); -} - - struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef TestSimpleTableWithEnumT NativeTableType; typedef TestSimpleTableWithEnumBuilder Builder; @@ -1021,18 +993,6 @@ struct StatT : public flatbuffers::NativeTable { uint16_t count = 0; }; -inline bool operator==(const StatT &lhs, const StatT &rhs) { - return - (lhs.id == rhs.id) && - (lhs.val == rhs.val) && - (lhs.count == rhs.count); -} - -inline bool operator!=(const StatT &lhs, const StatT &rhs) { - return !(lhs == rhs); -} - - struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef StatT NativeTableType; typedef StatBuilder Builder; @@ -1137,16 +1097,6 @@ struct ReferrableT : public flatbuffers::NativeTable { uint64_t id = 0; }; -inline bool operator==(const ReferrableT &lhs, const ReferrableT &rhs) { - return - (lhs.id == rhs.id); -} - -inline bool operator!=(const ReferrableT &lhs, const ReferrableT &rhs) { - return !(lhs == rhs); -} - - struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef ReferrableT NativeTableType; typedef ReferrableBuilder Builder; @@ -1257,62 +1207,6 @@ struct MonsterT : public flatbuffers::NativeTable { std::vector> scalar_key_sorted_tables{}; }; -inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) { - return - (lhs.pos == rhs.pos) && - (lhs.mana == rhs.mana) && - (lhs.hp == rhs.hp) && - (lhs.name == rhs.name) && - (lhs.inventory == rhs.inventory) && - (lhs.color == rhs.color) && - (lhs.test == rhs.test) && - (lhs.test4 == rhs.test4) && - (lhs.testarrayofstring == rhs.testarrayofstring) && - (lhs.testarrayoftables == rhs.testarrayoftables) && - (lhs.enemy == rhs.enemy) && - (lhs.testnestedflatbuffer == rhs.testnestedflatbuffer) && - (lhs.testempty == rhs.testempty) && - (lhs.testbool == rhs.testbool) && - (lhs.testhashs32_fnv1 == rhs.testhashs32_fnv1) && - (lhs.testhashu32_fnv1 == rhs.testhashu32_fnv1) && - (lhs.testhashs64_fnv1 == rhs.testhashs64_fnv1) && - (lhs.testhashu64_fnv1 == rhs.testhashu64_fnv1) && - (lhs.testhashs32_fnv1a == rhs.testhashs32_fnv1a) && - (lhs.testhashu32_fnv1a == rhs.testhashu32_fnv1a) && - (lhs.testhashs64_fnv1a == rhs.testhashs64_fnv1a) && - (lhs.testhashu64_fnv1a == rhs.testhashu64_fnv1a) && - (lhs.testarrayofbools == rhs.testarrayofbools) && - (lhs.testf == rhs.testf) && - (lhs.testf2 == rhs.testf2) && - (lhs.testf3 == rhs.testf3) && - (lhs.testarrayofstring2 == rhs.testarrayofstring2) && - (lhs.testarrayofsortedstruct == rhs.testarrayofsortedstruct) && - (lhs.flex == rhs.flex) && - (lhs.test5 == rhs.test5) && - (lhs.vector_of_longs == rhs.vector_of_longs) && - (lhs.vector_of_doubles == rhs.vector_of_doubles) && - (lhs.parent_namespace_test == rhs.parent_namespace_test) && - (lhs.vector_of_referrables == rhs.vector_of_referrables) && - (lhs.single_weak_reference == rhs.single_weak_reference) && - (lhs.vector_of_weak_references == rhs.vector_of_weak_references) && - (lhs.vector_of_strong_referrables == rhs.vector_of_strong_referrables) && - (lhs.co_owning_reference == rhs.co_owning_reference) && - (lhs.vector_of_co_owning_references == rhs.vector_of_co_owning_references) && - (lhs.non_owning_reference == rhs.non_owning_reference) && - (lhs.vector_of_non_owning_references == rhs.vector_of_non_owning_references) && - (lhs.any_unique == rhs.any_unique) && - (lhs.any_ambiguous == rhs.any_ambiguous) && - (lhs.vector_of_enums == rhs.vector_of_enums) && - (lhs.signed_enum == rhs.signed_enum) && - (lhs.testrequirednestedflatbuffer == rhs.testrequirednestedflatbuffer) && - (lhs.scalar_key_sorted_tables == rhs.scalar_key_sorted_tables); -} - -inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) { - return !(lhs == rhs); -} - - /// an example documentation comment: "monster object" struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef MonsterT NativeTableType; @@ -2242,27 +2136,6 @@ struct TypeAliasesT : public flatbuffers::NativeTable { std::vector vf64{}; }; -inline bool operator==(const TypeAliasesT &lhs, const TypeAliasesT &rhs) { - return - (lhs.i8 == rhs.i8) && - (lhs.u8 == rhs.u8) && - (lhs.i16 == rhs.i16) && - (lhs.u16 == rhs.u16) && - (lhs.i32 == rhs.i32) && - (lhs.u32 == rhs.u32) && - (lhs.i64 == rhs.i64) && - (lhs.u64 == rhs.u64) && - (lhs.f32 == rhs.f32) && - (lhs.f64 == rhs.f64) && - (lhs.v8 == rhs.v8) && - (lhs.vf64 == rhs.vf64); -} - -inline bool operator!=(const TypeAliasesT &lhs, const TypeAliasesT &rhs) { - return !(lhs == rhs); -} - - struct TypeAliases FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef TypeAliasesT NativeTableType; typedef TypeAliasesBuilder Builder; @@ -2495,6 +2368,16 @@ flatbuffers::Offset CreateTypeAliases(flatbuffers::FlatBufferBuilde } // namespace Example + +inline bool operator==(const InParentNamespaceT &, const InParentNamespaceT &) { + return true; +} + +inline bool operator!=(const InParentNamespaceT &lhs, const InParentNamespaceT &rhs) { + return !(lhs == rhs); +} + + inline InParentNamespaceT *InParentNamespace::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new InParentNamespaceT()); UnPackTo(_o.get(), _resolver); @@ -2520,6 +2403,16 @@ inline flatbuffers::Offset CreateInParentNamespace(flatbuffer namespace Example2 { + +inline bool operator==(const MonsterT &, const MonsterT &) { + return true; +} + +inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) { + return !(lhs == rhs); +} + + inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new MonsterT()); UnPackTo(_o.get(), _resolver); @@ -2547,6 +2440,17 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder namespace Example { + +inline bool operator==(const TestSimpleTableWithEnumT &lhs, const TestSimpleTableWithEnumT &rhs) { + return + (lhs.color == rhs.color); +} + +inline bool operator!=(const TestSimpleTableWithEnumT &lhs, const TestSimpleTableWithEnumT &rhs) { + return !(lhs == rhs); +} + + inline TestSimpleTableWithEnumT *TestSimpleTableWithEnum::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new TestSimpleTableWithEnumT()); UnPackTo(_o.get(), _resolver); @@ -2573,6 +2477,19 @@ inline flatbuffers::Offset CreateTestSimpleTableWithEnu _color); } + +inline bool operator==(const StatT &lhs, const StatT &rhs) { + return + (lhs.id == rhs.id) && + (lhs.val == rhs.val) && + (lhs.count == rhs.count); +} + +inline bool operator!=(const StatT &lhs, const StatT &rhs) { + return !(lhs == rhs); +} + + inline StatT *Stat::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new StatT()); UnPackTo(_o.get(), _resolver); @@ -2605,6 +2522,17 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb _count); } + +inline bool operator==(const ReferrableT &lhs, const ReferrableT &rhs) { + return + (lhs.id == rhs.id); +} + +inline bool operator!=(const ReferrableT &lhs, const ReferrableT &rhs) { + return !(lhs == rhs); +} + + inline ReferrableT *Referrable::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new ReferrableT()); UnPackTo(_o.get(), _resolver); @@ -2631,6 +2559,63 @@ inline flatbuffers::Offset CreateReferrable(flatbuffers::FlatBufferB _id); } + +inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) { + return + ((lhs.pos == rhs.pos) || (lhs.pos && rhs.pos && *lhs.pos == *rhs.pos)) && + (lhs.mana == rhs.mana) && + (lhs.hp == rhs.hp) && + (lhs.name == rhs.name) && + (lhs.inventory == rhs.inventory) && + (lhs.color == rhs.color) && + (lhs.test == rhs.test) && + (lhs.test4 == rhs.test4) && + (lhs.testarrayofstring == rhs.testarrayofstring) && + (lhs.testarrayoftables == rhs.testarrayoftables) && + ((lhs.enemy == rhs.enemy) || (lhs.enemy && rhs.enemy && *lhs.enemy == *rhs.enemy)) && + (lhs.testnestedflatbuffer == rhs.testnestedflatbuffer) && + ((lhs.testempty == rhs.testempty) || (lhs.testempty && rhs.testempty && *lhs.testempty == *rhs.testempty)) && + (lhs.testbool == rhs.testbool) && + (lhs.testhashs32_fnv1 == rhs.testhashs32_fnv1) && + (lhs.testhashu32_fnv1 == rhs.testhashu32_fnv1) && + (lhs.testhashs64_fnv1 == rhs.testhashs64_fnv1) && + (lhs.testhashu64_fnv1 == rhs.testhashu64_fnv1) && + (lhs.testhashs32_fnv1a == rhs.testhashs32_fnv1a) && + (lhs.testhashu32_fnv1a == rhs.testhashu32_fnv1a) && + (lhs.testhashs64_fnv1a == rhs.testhashs64_fnv1a) && + (lhs.testhashu64_fnv1a == rhs.testhashu64_fnv1a) && + (lhs.testarrayofbools == rhs.testarrayofbools) && + (lhs.testf == rhs.testf) && + (lhs.testf2 == rhs.testf2) && + (lhs.testf3 == rhs.testf3) && + (lhs.testarrayofstring2 == rhs.testarrayofstring2) && + (lhs.testarrayofsortedstruct == rhs.testarrayofsortedstruct) && + (lhs.flex == rhs.flex) && + (lhs.test5 == rhs.test5) && + (lhs.vector_of_longs == rhs.vector_of_longs) && + (lhs.vector_of_doubles == rhs.vector_of_doubles) && + ((lhs.parent_namespace_test == rhs.parent_namespace_test) || (lhs.parent_namespace_test && rhs.parent_namespace_test && *lhs.parent_namespace_test == *rhs.parent_namespace_test)) && + (lhs.vector_of_referrables == rhs.vector_of_referrables) && + (lhs.single_weak_reference == rhs.single_weak_reference) && + (lhs.vector_of_weak_references == rhs.vector_of_weak_references) && + (lhs.vector_of_strong_referrables == rhs.vector_of_strong_referrables) && + (lhs.co_owning_reference == rhs.co_owning_reference) && + (lhs.vector_of_co_owning_references == rhs.vector_of_co_owning_references) && + (lhs.non_owning_reference == rhs.non_owning_reference) && + (lhs.vector_of_non_owning_references == rhs.vector_of_non_owning_references) && + (lhs.any_unique == rhs.any_unique) && + (lhs.any_ambiguous == rhs.any_ambiguous) && + (lhs.vector_of_enums == rhs.vector_of_enums) && + (lhs.signed_enum == rhs.signed_enum) && + (lhs.testrequirednestedflatbuffer == rhs.testrequirednestedflatbuffer) && + (lhs.scalar_key_sorted_tables == rhs.scalar_key_sorted_tables); +} + +inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) { + return !(lhs == rhs); +} + + inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new MonsterT()); UnPackTo(_o.get(), _resolver); @@ -2811,6 +2796,28 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder _scalar_key_sorted_tables); } + +inline bool operator==(const TypeAliasesT &lhs, const TypeAliasesT &rhs) { + return + (lhs.i8 == rhs.i8) && + (lhs.u8 == rhs.u8) && + (lhs.i16 == rhs.i16) && + (lhs.u16 == rhs.u16) && + (lhs.i32 == rhs.i32) && + (lhs.u32 == rhs.u32) && + (lhs.i64 == rhs.i64) && + (lhs.u64 == rhs.u64) && + (lhs.f32 == rhs.f32) && + (lhs.f64 == rhs.f64) && + (lhs.v8 == rhs.v8) && + (lhs.vf64 == rhs.vf64); +} + +inline bool operator!=(const TypeAliasesT &lhs, const TypeAliasesT &rhs) { + return !(lhs == rhs); +} + + inline TypeAliasesT *TypeAliases::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new TypeAliasesT()); UnPackTo(_o.get(), _resolver); diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 416faafba..09b8cef1d 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -215,16 +215,6 @@ struct TableInNestedNST : public flatbuffers::NativeTable { int32_t foo = 0; }; -inline bool operator==(const TableInNestedNST &lhs, const TableInNestedNST &rhs) { - return - (lhs.foo == rhs.foo); -} - -inline bool operator!=(const TableInNestedNST &lhs, const TableInNestedNST &rhs) { - return !(lhs == rhs); -} - - struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef TableInNestedNST NativeTableType; typedef TableInNestedNSBuilder Builder; @@ -281,6 +271,17 @@ inline flatbuffers::Offset CreateTableInNestedNS( flatbuffers::Offset CreateTableInNestedNS(flatbuffers::FlatBufferBuilder &_fbb, const TableInNestedNST *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const TableInNestedNST &lhs, const TableInNestedNST &rhs) { + return + (lhs.foo == rhs.foo); +} + +inline bool operator!=(const TableInNestedNST &lhs, const TableInNestedNST &rhs) { + return !(lhs == rhs); +} + + inline TableInNestedNST *TableInNestedNS::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new TableInNestedNST()); UnPackTo(_o.get(), _resolver); diff --git a/tests/namespace_test/namespace_test2_generated.h b/tests/namespace_test/namespace_test2_generated.h index 201f5fcf2..d6a2f4972 100644 --- a/tests/namespace_test/namespace_test2_generated.h +++ b/tests/namespace_test/namespace_test2_generated.h @@ -68,19 +68,6 @@ struct TableInFirstNST : public flatbuffers::NativeTable { flatbuffers::unique_ptr foo_struct{}; }; -inline bool operator==(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { - return - (lhs.foo_table == rhs.foo_table) && - (lhs.foo_enum == rhs.foo_enum) && - (lhs.foo_union == rhs.foo_union) && - (lhs.foo_struct == rhs.foo_struct); -} - -inline bool operator!=(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { - return !(lhs == rhs); -} - - struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef TableInFirstNST NativeTableType; typedef TableInFirstNSBuilder Builder; @@ -209,17 +196,6 @@ struct TableInCT : public flatbuffers::NativeTable { flatbuffers::unique_ptr refer_to_a2{}; }; -inline bool operator==(const TableInCT &lhs, const TableInCT &rhs) { - return - (lhs.refer_to_a1 == rhs.refer_to_a1) && - (lhs.refer_to_a2 == rhs.refer_to_a2); -} - -inline bool operator!=(const TableInCT &lhs, const TableInCT &rhs) { - return !(lhs == rhs); -} - - struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef TableInCT NativeTableType; typedef TableInCBuilder Builder; @@ -303,16 +279,6 @@ struct SecondTableInAT : public flatbuffers::NativeTable { flatbuffers::unique_ptr refer_to_c{}; }; -inline bool operator==(const SecondTableInAT &lhs, const SecondTableInAT &rhs) { - return - (lhs.refer_to_c == rhs.refer_to_c); -} - -inline bool operator!=(const SecondTableInAT &lhs, const SecondTableInAT &rhs) { - return !(lhs == rhs); -} - - struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef SecondTableInAT NativeTableType; typedef SecondTableInABuilder Builder; @@ -370,6 +336,20 @@ inline flatbuffers::Offset CreateSecondTableInA( flatbuffers::Offset CreateSecondTableInA(flatbuffers::FlatBufferBuilder &_fbb, const SecondTableInAT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { + return + ((lhs.foo_table == rhs.foo_table) || (lhs.foo_table && rhs.foo_table && *lhs.foo_table == *rhs.foo_table)) && + (lhs.foo_enum == rhs.foo_enum) && + (lhs.foo_union == rhs.foo_union) && + ((lhs.foo_struct == rhs.foo_struct) || (lhs.foo_struct && rhs.foo_struct && *lhs.foo_struct == *rhs.foo_struct)); +} + +inline bool operator!=(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { + return !(lhs == rhs); +} + + inline TableInFirstNST *TableInFirstNS::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new TableInFirstNST()); UnPackTo(_o.get(), _resolver); @@ -412,6 +392,18 @@ inline flatbuffers::Offset CreateTableInFirstNS(flatbuffers::Fla namespace NamespaceC { + +inline bool operator==(const TableInCT &lhs, const TableInCT &rhs) { + return + ((lhs.refer_to_a1 == rhs.refer_to_a1) || (lhs.refer_to_a1 && rhs.refer_to_a1 && *lhs.refer_to_a1 == *rhs.refer_to_a1)) && + ((lhs.refer_to_a2 == rhs.refer_to_a2) || (lhs.refer_to_a2 && rhs.refer_to_a2 && *lhs.refer_to_a2 == *rhs.refer_to_a2)); +} + +inline bool operator!=(const TableInCT &lhs, const TableInCT &rhs) { + return !(lhs == rhs); +} + + inline TableInCT *TableInC::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new TableInCT()); UnPackTo(_o.get(), _resolver); @@ -445,6 +437,17 @@ inline flatbuffers::Offset CreateTableInC(flatbuffers::FlatBufferBuild namespace NamespaceA { + +inline bool operator==(const SecondTableInAT &lhs, const SecondTableInAT &rhs) { + return + ((lhs.refer_to_c == rhs.refer_to_c) || (lhs.refer_to_c && rhs.refer_to_c && *lhs.refer_to_c == *rhs.refer_to_c)); +} + +inline bool operator!=(const SecondTableInAT &lhs, const SecondTableInAT &rhs) { + return !(lhs == rhs); +} + + inline SecondTableInAT *SecondTableInA::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new SecondTableInAT()); UnPackTo(_o.get(), _resolver); diff --git a/tests/optional_scalars_generated.h b/tests/optional_scalars_generated.h index 963bfa4b2..e2fec8239 100644 --- a/tests/optional_scalars_generated.h +++ b/tests/optional_scalars_generated.h @@ -90,51 +90,6 @@ struct ScalarStuffT : public flatbuffers::NativeTable { optional_scalars::OptionalByte default_enum = optional_scalars::OptionalByte_One; }; -inline bool operator==(const ScalarStuffT &lhs, const ScalarStuffT &rhs) { - return - (lhs.just_i8 == rhs.just_i8) && - (lhs.maybe_i8 == rhs.maybe_i8) && - (lhs.default_i8 == rhs.default_i8) && - (lhs.just_u8 == rhs.just_u8) && - (lhs.maybe_u8 == rhs.maybe_u8) && - (lhs.default_u8 == rhs.default_u8) && - (lhs.just_i16 == rhs.just_i16) && - (lhs.maybe_i16 == rhs.maybe_i16) && - (lhs.default_i16 == rhs.default_i16) && - (lhs.just_u16 == rhs.just_u16) && - (lhs.maybe_u16 == rhs.maybe_u16) && - (lhs.default_u16 == rhs.default_u16) && - (lhs.just_i32 == rhs.just_i32) && - (lhs.maybe_i32 == rhs.maybe_i32) && - (lhs.default_i32 == rhs.default_i32) && - (lhs.just_u32 == rhs.just_u32) && - (lhs.maybe_u32 == rhs.maybe_u32) && - (lhs.default_u32 == rhs.default_u32) && - (lhs.just_i64 == rhs.just_i64) && - (lhs.maybe_i64 == rhs.maybe_i64) && - (lhs.default_i64 == rhs.default_i64) && - (lhs.just_u64 == rhs.just_u64) && - (lhs.maybe_u64 == rhs.maybe_u64) && - (lhs.default_u64 == rhs.default_u64) && - (lhs.just_f32 == rhs.just_f32) && - (lhs.maybe_f32 == rhs.maybe_f32) && - (lhs.default_f32 == rhs.default_f32) && - (lhs.just_f64 == rhs.just_f64) && - (lhs.maybe_f64 == rhs.maybe_f64) && - (lhs.default_f64 == rhs.default_f64) && - (lhs.just_bool == rhs.just_bool) && - (lhs.maybe_bool == rhs.maybe_bool) && - (lhs.default_bool == rhs.default_bool) && - (lhs.just_enum == rhs.just_enum) && - (lhs.maybe_enum == rhs.maybe_enum) && - (lhs.default_enum == rhs.default_enum); -} - -inline bool operator!=(const ScalarStuffT &lhs, const ScalarStuffT &rhs) { - return !(lhs == rhs); -} - - struct ScalarStuff FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef ScalarStuffT NativeTableType; typedef ScalarStuffBuilder Builder; @@ -643,6 +598,52 @@ inline flatbuffers::Offset CreateScalarStuff( flatbuffers::Offset CreateScalarStuff(flatbuffers::FlatBufferBuilder &_fbb, const ScalarStuffT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const ScalarStuffT &lhs, const ScalarStuffT &rhs) { + return + (lhs.just_i8 == rhs.just_i8) && + (lhs.maybe_i8 == rhs.maybe_i8) && + (lhs.default_i8 == rhs.default_i8) && + (lhs.just_u8 == rhs.just_u8) && + (lhs.maybe_u8 == rhs.maybe_u8) && + (lhs.default_u8 == rhs.default_u8) && + (lhs.just_i16 == rhs.just_i16) && + (lhs.maybe_i16 == rhs.maybe_i16) && + (lhs.default_i16 == rhs.default_i16) && + (lhs.just_u16 == rhs.just_u16) && + (lhs.maybe_u16 == rhs.maybe_u16) && + (lhs.default_u16 == rhs.default_u16) && + (lhs.just_i32 == rhs.just_i32) && + (lhs.maybe_i32 == rhs.maybe_i32) && + (lhs.default_i32 == rhs.default_i32) && + (lhs.just_u32 == rhs.just_u32) && + (lhs.maybe_u32 == rhs.maybe_u32) && + (lhs.default_u32 == rhs.default_u32) && + (lhs.just_i64 == rhs.just_i64) && + (lhs.maybe_i64 == rhs.maybe_i64) && + (lhs.default_i64 == rhs.default_i64) && + (lhs.just_u64 == rhs.just_u64) && + (lhs.maybe_u64 == rhs.maybe_u64) && + (lhs.default_u64 == rhs.default_u64) && + (lhs.just_f32 == rhs.just_f32) && + (lhs.maybe_f32 == rhs.maybe_f32) && + (lhs.default_f32 == rhs.default_f32) && + (lhs.just_f64 == rhs.just_f64) && + (lhs.maybe_f64 == rhs.maybe_f64) && + (lhs.default_f64 == rhs.default_f64) && + (lhs.just_bool == rhs.just_bool) && + (lhs.maybe_bool == rhs.maybe_bool) && + (lhs.default_bool == rhs.default_bool) && + (lhs.just_enum == rhs.just_enum) && + (lhs.maybe_enum == rhs.maybe_enum) && + (lhs.default_enum == rhs.default_enum); +} + +inline bool operator!=(const ScalarStuffT &lhs, const ScalarStuffT &rhs) { + return !(lhs == rhs); +} + + inline ScalarStuffT *ScalarStuff::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new ScalarStuffT()); UnPackTo(_o.get(), _resolver); diff --git a/tests/test.cpp b/tests/test.cpp index dcfa787ca..ce9ce6706 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -3320,6 +3320,31 @@ void EqualOperatorTest() { TEST_EQ(b == a, true); TEST_EQ(b != a, false); + a.enemy.reset(new MonsterT()); + TEST_EQ(b != a, true); + a.enemy->mana = 33; + TEST_EQ(b == a, false); + TEST_EQ(b != a, true); + + b.enemy.reset(new MonsterT()); + TEST_EQ(b == a, false); + TEST_EQ(b != a, true); + b.enemy->mana = 33; + TEST_EQ(b == a, true); + TEST_EQ(b != a, false); + + a.enemy.reset(nullptr); + TEST_EQ(b == a, false); + TEST_EQ(b != a, true); + b.enemy->mana = 150; + TEST_EQ(b == a, false); + TEST_EQ(b != a, true); + a.enemy.reset(new MonsterT()); + TEST_EQ(b == a, true); + TEST_EQ(b != a, false); + + b.enemy.reset(nullptr); + b.test.type = Any_Monster; TEST_EQ(b == a, false); TEST_EQ(b != a, true); diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index b5a67f988..1cac36bfe 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -274,16 +274,6 @@ struct AttackerT : public flatbuffers::NativeTable { int32_t sword_attack_damage = 0; }; -inline bool operator==(const AttackerT &lhs, const AttackerT &rhs) { - return - (lhs.sword_attack_damage == rhs.sword_attack_damage); -} - -inline bool operator!=(const AttackerT &lhs, const AttackerT &rhs) { - return !(lhs == rhs); -} - - struct Attacker FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef AttackerT NativeTableType; typedef AttackerBuilder Builder; @@ -349,17 +339,6 @@ struct MovieT : public flatbuffers::NativeTable { std::vector characters{}; }; -inline bool operator==(const MovieT &lhs, const MovieT &rhs) { - return - (lhs.main_character == rhs.main_character) && - (lhs.characters == rhs.characters); -} - -inline bool operator!=(const MovieT &lhs, const MovieT &rhs) { - return !(lhs == rhs); -} - - struct Movie FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { typedef MovieT NativeTableType; typedef MovieBuilder Builder; @@ -490,6 +469,17 @@ inline flatbuffers::Offset CreateMovieDirect( flatbuffers::Offset CreateMovie(flatbuffers::FlatBufferBuilder &_fbb, const MovieT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const AttackerT &lhs, const AttackerT &rhs) { + return + (lhs.sword_attack_damage == rhs.sword_attack_damage); +} + +inline bool operator!=(const AttackerT &lhs, const AttackerT &rhs) { + return !(lhs == rhs); +} + + inline AttackerT *Attacker::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new AttackerT()); UnPackTo(_o.get(), _resolver); @@ -516,6 +506,18 @@ inline flatbuffers::Offset CreateAttacker(flatbuffers::FlatBufferBuild _sword_attack_damage); } + +inline bool operator==(const MovieT &lhs, const MovieT &rhs) { + return + (lhs.main_character == rhs.main_character) && + (lhs.characters == rhs.characters); +} + +inline bool operator!=(const MovieT &lhs, const MovieT &rhs) { + return !(lhs == rhs); +} + + inline MovieT *Movie::UnPack(const flatbuffers::resolver_function_t *_resolver) const { auto _o = std::unique_ptr(new MovieT()); UnPackTo(_o.get(), _resolver);