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
This commit is contained in:
Mika Raento
2021-06-21 21:37:56 +03:00
committed by GitHub
parent 06fd6d640c
commit 962751a6ec
10 changed files with 362 additions and 297 deletions

View File

@@ -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<TableInNestedNS> CreateTableInNestedNS(
flatbuffers::Offset<TableInNestedNS> 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<TableInNestedNST>(new TableInNestedNST());
UnPackTo(_o.get(), _resolver);

View File

@@ -68,19 +68,6 @@ struct TableInFirstNST : public flatbuffers::NativeTable {
flatbuffers::unique_ptr<NamespaceA::NamespaceB::StructInNestedNS> 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<NamespaceA::SecondTableInAT> 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<NamespaceC::TableInCT> 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<SecondTableInA> CreateSecondTableInA(
flatbuffers::Offset<SecondTableInA> 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<TableInFirstNST>(new TableInFirstNST());
UnPackTo(_o.get(), _resolver);
@@ -412,6 +392,18 @@ inline flatbuffers::Offset<TableInFirstNS> 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<TableInCT>(new TableInCT());
UnPackTo(_o.get(), _resolver);
@@ -445,6 +437,17 @@ inline flatbuffers::Offset<TableInC> 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<SecondTableInAT>(new SecondTableInAT());
UnPackTo(_o.get(), _resolver);