Add compare operator to code generated for c++ (#4940)

* Add operator== for c++ genated code

New "--gen-compare" option for flatc to generate compare operators. The operators are defined based on object based api types.

Inspired by issue #263.

* Improve compare operator for c++.
Thanks for the code review.

- Improve robustness against future schema extensions
- Code style
- Fix --rust generation in generate_code.sh
This commit is contained in:
Thomas
2018-09-22 01:53:59 +02:00
committed by Wouter van Oortmerssen
parent 873a60b0d8
commit 33791dc7b0
11 changed files with 384 additions and 11 deletions

View File

@@ -17,6 +17,10 @@ struct MonsterT;
struct Weapon;
struct WeaponT;
bool operator==(const Vec3 &lhs, const Vec3 &rhs);
bool operator==(const MonsterT &lhs, const MonsterT &rhs);
bool operator==(const WeaponT &lhs, const WeaponT &rhs);
inline const flatbuffers::TypeTable *Vec3TypeTable();
inline const flatbuffers::TypeTable *MonsterTypeTable();
@@ -133,6 +137,22 @@ struct EquipmentUnion {
}
};
inline bool operator==(const EquipmentUnion &lhs, const EquipmentUnion &rhs) {
if (lhs.type != rhs.type) return false;
switch (lhs.type) {
case Equipment_NONE: {
return true;
}
case Equipment_Weapon: {
return *(reinterpret_cast<const WeaponT *>(lhs.value)) ==
*(reinterpret_cast<const WeaponT *>(rhs.value));
}
default: {
return false;
}
}
}
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);
@@ -172,6 +192,13 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
};
FLATBUFFERS_STRUCT_END(Vec3, 12);
inline bool operator==(const Vec3 &lhs, const Vec3 &rhs) {
return
(lhs.x() == rhs.x()) &&
(lhs.y() == rhs.y()) &&
(lhs.z() == rhs.z());
}
struct MonsterT : public flatbuffers::NativeTable {
typedef Monster TableType;
flatbuffers::unique_ptr<Vec3> pos;
@@ -189,6 +216,18 @@ struct MonsterT : public flatbuffers::NativeTable {
}
};
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);
}
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MonsterT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
@@ -391,6 +430,12 @@ struct WeaponT : public flatbuffers::NativeTable {
}
};
inline bool operator==(const WeaponT &lhs, const WeaponT &rhs) {
return
(lhs.name == rhs.name) &&
(lhs.damage == rhs.damage);
}
struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef WeaponT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {