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

@@ -16,6 +16,11 @@ struct BookReader;
struct Movie;
struct MovieT;
bool operator==(const AttackerT &lhs, const AttackerT &rhs);
bool operator==(const Rapunzel &lhs, const Rapunzel &rhs);
bool operator==(const BookReader &lhs, const BookReader &rhs);
bool operator==(const MovieT &lhs, const MovieT &rhs);
inline const flatbuffers::TypeTable *AttackerTypeTable();
inline const flatbuffers::TypeTable *RapunzelTypeTable();
@@ -138,6 +143,42 @@ struct CharacterUnion {
}
};
inline bool operator==(const CharacterUnion &lhs, const CharacterUnion &rhs) {
if (lhs.type != rhs.type) return false;
switch (lhs.type) {
case Character_NONE: {
return true;
}
case Character_MuLan: {
return *(reinterpret_cast<const AttackerT *>(lhs.value)) ==
*(reinterpret_cast<const AttackerT *>(rhs.value));
}
case Character_Rapunzel: {
return *(reinterpret_cast<const Rapunzel *>(lhs.value)) ==
*(reinterpret_cast<const Rapunzel *>(rhs.value));
}
case Character_Belle: {
return *(reinterpret_cast<const BookReader *>(lhs.value)) ==
*(reinterpret_cast<const BookReader *>(rhs.value));
}
case Character_BookFan: {
return *(reinterpret_cast<const BookReader *>(lhs.value)) ==
*(reinterpret_cast<const BookReader *>(rhs.value));
}
case Character_Other: {
return *(reinterpret_cast<const std::string *>(lhs.value)) ==
*(reinterpret_cast<const std::string *>(rhs.value));
}
case Character_Unused: {
return *(reinterpret_cast<const std::string *>(lhs.value)) ==
*(reinterpret_cast<const std::string *>(rhs.value));
}
default: {
return false;
}
}
}
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);
@@ -161,6 +202,11 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS {
};
FLATBUFFERS_STRUCT_END(Rapunzel, 4);
inline bool operator==(const Rapunzel &lhs, const Rapunzel &rhs) {
return
(lhs.hair_length() == rhs.hair_length());
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
private:
int32_t books_read_;
@@ -181,6 +227,11 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
};
FLATBUFFERS_STRUCT_END(BookReader, 4);
inline bool operator==(const BookReader &lhs, const BookReader &rhs) {
return
(lhs.books_read() == rhs.books_read());
}
struct AttackerT : public flatbuffers::NativeTable {
typedef Attacker TableType;
int32_t sword_attack_damage;
@@ -189,6 +240,11 @@ struct AttackerT : public flatbuffers::NativeTable {
}
};
inline bool operator==(const AttackerT &lhs, const AttackerT &rhs) {
return
(lhs.sword_attack_damage == rhs.sword_attack_damage);
}
struct Attacker FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef AttackerT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
@@ -249,6 +305,12 @@ struct MovieT : public flatbuffers::NativeTable {
}
};
inline bool operator==(const MovieT &lhs, const MovieT &rhs) {
return
(lhs.main_character == rhs.main_character) &&
(lhs.characters == rhs.characters);
}
struct Movie FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef MovieT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {