Commit 88abae64 authored by LouisP's avatar LouisP Committed by Wouter van Oortmerssen

Add inequality operator (inspired from #263) (#5257)

parent a7461433
......@@ -18,8 +18,11 @@ struct Weapon;
struct WeaponT;
bool operator==(const Vec3 &lhs, const Vec3 &rhs);
bool operator!=(const Vec3 &lhs, const Vec3 &rhs);
bool operator==(const MonsterT &lhs, const MonsterT &rhs);
bool operator!=(const MonsterT &lhs, const MonsterT &rhs);
bool operator==(const WeaponT &lhs, const WeaponT &rhs);
bool operator!=(const WeaponT &lhs, const WeaponT &rhs);
inline const flatbuffers::TypeTable *Vec3TypeTable();
......@@ -155,6 +158,11 @@ inline bool operator==(const EquipmentUnion &lhs, const EquipmentUnion &rhs) {
}
}
}
inline bool operator!=(const EquipmentUnion &lhs, const EquipmentUnion &rhs) {
return !(lhs == rhs);
}
bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *obj, Equipment type);
bool VerifyEquipmentVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
......@@ -201,6 +209,11 @@ inline bool operator==(const Vec3 &lhs, const Vec3 &rhs) {
(lhs.z() == rhs.z());
}
inline bool operator!=(const Vec3 &lhs, const Vec3 &rhs) {
return !(lhs == rhs);
}
struct MonsterT : public flatbuffers::NativeTable {
typedef Monster TableType;
flatbuffers::unique_ptr<Vec3> pos;
......@@ -230,6 +243,11 @@ inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) {
(lhs.equipped == rhs.equipped);
}
inline bool operator!=(const MonsterT &lhs, const MonsterT &rhs) {
return !(lhs == rhs);
}
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -441,6 +459,11 @@ inline bool operator==(const WeaponT &lhs, const WeaponT &rhs) {
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......
......@@ -250,6 +250,8 @@ class CppGenerator : public BaseGenerator {
NativeName(Name(struct_def), &struct_def, parser_.opts);
code_ += "bool operator==(const " + nativeName + " &lhs, const " +
nativeName + " &rhs);";
code_ += "bool operator!=(const " + nativeName + " &lhs, const " +
nativeName + " &rhs);";
}
}
code_ += "";
......@@ -1174,6 +1176,14 @@ class CppGenerator : public BaseGenerator {
code_ += " }";
code_ += " }";
code_ += "}";
code_ += "";
code_ +=
"inline bool operator!=(const {{NAME}}Union &lhs, const "
"{{NAME}}Union &rhs) {";
code_ += " return !(lhs == rhs);";
code_ += "}";
code_ += "";
}
}
......@@ -1583,6 +1593,14 @@ class CppGenerator : public BaseGenerator {
"{{NATIVE_NAME}} &{{CMP_RHS}}) {";
code_ += "{{CMP_OP}}";
code_ += "}";
code_ += "";
code_ +=
"inline bool operator!=(const {{NATIVE_NAME}} &lhs, const "
"{{NATIVE_NAME}} &rhs) {";
code_ += " return !(lhs == rhs);";
code_ += "}";
code_ += "";
}
void GenOperatorNewDelete(const StructDef &struct_def) {
......
......@@ -12,6 +12,7 @@ struct MonsterExtra;
struct MonsterExtraT;
bool operator==(const MonsterExtraT &lhs, const MonsterExtraT &rhs);
bool operator!=(const MonsterExtraT &lhs, const MonsterExtraT &rhs);
inline const flatbuffers::TypeTable *MonsterExtraTypeTable();
......@@ -43,6 +44,11 @@ inline bool operator==(const MonsterExtraT &lhs, const MonsterExtraT &rhs) {
(lhs.testd_ninf == rhs.testd_ninf);
}
inline bool operator!=(const MonsterExtraT &lhs, const MonsterExtraT &rhs) {
return !(lhs == rhs);
}
struct MonsterExtra FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterExtraT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......
......@@ -45,21 +45,31 @@ struct TypeAliasesT;
} // namespace Example
bool operator==(const InParentNamespaceT &lhs, const InParentNamespaceT &rhs);
bool operator!=(const InParentNamespaceT &lhs, const InParentNamespaceT &rhs);
namespace Example2 {
bool operator==(const MonsterT &lhs, const MonsterT &rhs);
bool operator!=(const MonsterT &lhs, const MonsterT &rhs);
} // namespace Example2
namespace Example {
bool operator==(const Test &lhs, const Test &rhs);
bool operator!=(const Test &lhs, const Test &rhs);
bool operator==(const TestSimpleTableWithEnumT &lhs, const TestSimpleTableWithEnumT &rhs);
bool operator!=(const TestSimpleTableWithEnumT &lhs, const TestSimpleTableWithEnumT &rhs);
bool operator==(const Vec3 &lhs, const Vec3 &rhs);
bool operator!=(const Vec3 &lhs, const Vec3 &rhs);
bool operator==(const Ability &lhs, const Ability &rhs);
bool operator!=(const Ability &lhs, const Ability &rhs);
bool operator==(const StatT &lhs, const StatT &rhs);
bool operator!=(const StatT &lhs, const StatT &rhs);
bool operator==(const ReferrableT &lhs, const ReferrableT &rhs);
bool operator!=(const ReferrableT &lhs, const ReferrableT &rhs);
bool operator==(const MonsterT &lhs, const MonsterT &rhs);
bool operator!=(const MonsterT &lhs, const MonsterT &rhs);
bool operator==(const TypeAliasesT &lhs, const TypeAliasesT &rhs);
bool operator!=(const TypeAliasesT &lhs, const TypeAliasesT &rhs);
} // namespace Example
......@@ -260,6 +270,11 @@ inline bool operator==(const AnyUnion &lhs, const AnyUnion &rhs) {
}
}
}
inline bool operator!=(const AnyUnion &lhs, const AnyUnion &rhs) {
return !(lhs == rhs);
}
bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type);
bool VerifyAnyVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
......@@ -396,6 +411,11 @@ inline bool operator==(const AnyUniqueAliasesUnion &lhs, const AnyUniqueAliasesU
}
}
}
inline bool operator!=(const AnyUniqueAliasesUnion &lhs, const AnyUniqueAliasesUnion &rhs) {
return !(lhs == rhs);
}
bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type);
bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
......@@ -505,6 +525,11 @@ inline bool operator==(const AnyAmbiguousAliasesUnion &lhs, const AnyAmbiguousAl
}
}
}
inline bool operator!=(const AnyAmbiguousAliasesUnion &lhs, const AnyAmbiguousAliasesUnion &rhs) {
return !(lhs == rhs);
}
bool VerifyAnyAmbiguousAliases(flatbuffers::Verifier &verifier, const void *obj, AnyAmbiguousAliases type);
bool VerifyAnyAmbiguousAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
......@@ -545,6 +570,11 @@ inline bool operator==(const Test &lhs, const Test &rhs) {
(lhs.b() == rhs.b());
}
inline bool operator!=(const Test &lhs, const Test &rhs) {
return !(lhs == rhs);
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
private:
float x_;
......@@ -624,6 +654,11 @@ inline bool operator==(const Vec3 &lhs, const Vec3 &rhs) {
(lhs.test3() == rhs.test3());
}
inline bool operator!=(const Vec3 &lhs, const Vec3 &rhs) {
return !(lhs == rhs);
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
private:
uint32_t id_;
......@@ -664,6 +699,11 @@ inline bool operator==(const Ability &lhs, const Ability &rhs) {
(lhs.distance() == rhs.distance());
}
inline bool operator!=(const Ability &lhs, const Ability &rhs) {
return !(lhs == rhs);
}
} // namespace Example
struct InParentNamespaceT : public flatbuffers::NativeTable {
......@@ -676,6 +716,11 @@ 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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -725,6 +770,11 @@ 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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -779,6 +829,11 @@ inline bool operator==(const TestSimpleTableWithEnumT &lhs, const TestSimpleTabl
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -849,6 +904,11 @@ inline bool operator==(const StatT &lhs, const StatT &rhs) {
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -954,6 +1014,11 @@ inline bool operator==(const ReferrableT &lhs, const ReferrableT &rhs) {
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -1128,6 +1193,11 @@ inline bool operator==(const MonsterT &lhs, const MonsterT &rhs) {
(lhs.vector_of_enums == rhs.vector_of_enums);
}
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;
......@@ -2040,6 +2110,11 @@ inline bool operator==(const TypeAliasesT &lhs, const TypeAliasesT &rhs) {
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......
......@@ -2423,19 +2423,25 @@ void EqualOperatorTest() {
MonsterT a;
MonsterT b;
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
b.mana = 33;
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.mana = 150;
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
b.inventory.push_back(3);
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.inventory.clear();
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
b.test.type = Any_Monster;
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
}
// For testing any binaries, e.g. from fuzzing.
......
......@@ -17,9 +17,13 @@ struct Movie;
struct MovieT;
bool operator==(const AttackerT &lhs, const AttackerT &rhs);
bool operator!=(const AttackerT &lhs, const AttackerT &rhs);
bool operator==(const Rapunzel &lhs, const Rapunzel &rhs);
bool operator!=(const Rapunzel &lhs, const Rapunzel &rhs);
bool operator==(const BookReader &lhs, const BookReader &rhs);
bool operator!=(const BookReader &lhs, const BookReader &rhs);
bool operator==(const MovieT &lhs, const MovieT &rhs);
bool operator!=(const MovieT &lhs, const MovieT &rhs);
inline const flatbuffers::TypeTable *AttackerTypeTable();
......@@ -180,6 +184,11 @@ inline bool operator==(const CharacterUnion &lhs, const CharacterUnion &rhs) {
}
}
}
inline bool operator!=(const CharacterUnion &lhs, const CharacterUnion &rhs) {
return !(lhs == rhs);
}
bool VerifyCharacter(flatbuffers::Verifier &verifier, const void *obj, Character type);
bool VerifyCharacterVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
......@@ -208,6 +217,11 @@ inline bool operator==(const Rapunzel &lhs, const Rapunzel &rhs) {
(lhs.hair_length() == rhs.hair_length());
}
inline bool operator!=(const Rapunzel &lhs, const Rapunzel &rhs) {
return !(lhs == rhs);
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
private:
int32_t books_read_;
......@@ -233,6 +247,11 @@ inline bool operator==(const BookReader &lhs, const BookReader &rhs) {
(lhs.books_read() == rhs.books_read());
}
inline bool operator!=(const BookReader &lhs, const BookReader &rhs) {
return !(lhs == rhs);
}
struct AttackerT : public flatbuffers::NativeTable {
typedef Attacker TableType;
int32_t sword_attack_damage;
......@@ -246,6 +265,11 @@ inline bool operator==(const AttackerT &lhs, const AttackerT &rhs) {
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......@@ -312,6 +336,11 @@ inline bool operator==(const MovieT &lhs, const MovieT &rhs) {
(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;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment