Skip to content

Commit 4173b84

Browse files
Fix --gen-compare to not generate comparators for native types. (#8681)
Per the definition of --gen-compare: only generate comparators for object API generated structs. Types annoated with native_type must define their own comparators if `--gen-compare` is enabled. Also enables --gen-compare for native_type_test and fixes the test by adding a comparator for the Native::Vector3D type.
1 parent 5fe90a9 commit 4173b84

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,13 @@ if(FLATBUFFERS_BUILD_TESTS)
537537
add_definitions(-DFLATBUFFERS_TEST_PATH_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/)
538538

539539
# The flattest target needs some generated files
540-
SET(FLATC_OPT --cpp --gen-mutable --gen-object-api --reflect-names)
541-
SET(FLATC_OPT_COMP ${FLATC_OPT};--gen-compare)
540+
SET(FLATC_OPT_COMP --cpp --gen-compare --gen-mutable --gen-object-api --reflect-names)
542541
SET(FLATC_OPT_SCOPED_ENUMS ${FLATC_OPT_COMP};--scoped-enums)
543542

544543
compile_schema_for_test(tests/alignment_test.fbs "${FLATC_OPT_COMP}")
545544
compile_schema_for_test(tests/arrays_test.fbs "${FLATC_OPT_SCOPED_ENUMS}")
546545
compile_schema_for_test(tests/native_inline_table_test.fbs "${FLATC_OPT_COMP}")
547-
compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT}")
546+
compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT_COMP}")
548547
compile_schema_for_test(tests/key_field/key_field_sample.fbs "${FLATC_OPT_COMP}")
549548
compile_schema_for_test(tests/64bit/test_64bit.fbs "${FLATC_OPT_COMP};--bfbs-gen-embed")
550549
compile_schema_for_test(tests/64bit/evolution/v1.fbs "${FLATC_OPT_COMP}")

scripts/generate_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def glob(path, pattern):
355355
)
356356

357357
flatc(
358-
["--cpp", "--gen-mutable", "--gen-object-api", "--reflect-names"],
358+
["--cpp", "--gen-compare", "--gen-mutable", "--gen-object-api", "--reflect-names"],
359359
schema="native_type_test.fbs",
360360
)
361361

src/idl_gen_cpp.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ class CppGenerator : public BaseGenerator {
512512
// Generate forward declarations for all equal operators
513513
if (opts_.generate_object_based_api && opts_.gen_compare) {
514514
for (const auto& struct_def : parser_.structs_.vec) {
515-
if (!struct_def->generated) {
515+
const auto native_type = struct_def->attributes.Lookup("native_type");
516+
if (!struct_def->generated && !native_type) {
516517
SetNameSpace(struct_def->defined_namespace);
517518
auto nativeName = NativeName(Name(*struct_def), struct_def, opts_);
518519
code_ += "bool operator==(const " + nativeName + " &lhs, const " +
@@ -2190,6 +2191,12 @@ class CppGenerator : public BaseGenerator {
21902191

21912192
void GenCompareOperator(const StructDef& struct_def,
21922193
const std::string& accessSuffix = "") {
2194+
// Do not generate compare operators for native types.
2195+
const auto native_type = struct_def.attributes.Lookup("native_type");
2196+
if (native_type) {
2197+
return;
2198+
}
2199+
21932200
std::string compare_op;
21942201
for (auto it = struct_def.fields.vec.begin();
21952202
it != struct_def.fields.vec.end(); ++it) {

tests/native_type_test_generated.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ struct ApplicationData;
2929
struct ApplicationDataBuilder;
3030
struct ApplicationDataT;
3131

32+
bool operator==(const MatrixT &lhs, const MatrixT &rhs);
33+
bool operator!=(const MatrixT &lhs, const MatrixT &rhs);
34+
bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs);
35+
bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs);
36+
3237
inline const ::flatbuffers::TypeTable *Vector3DTypeTable();
3338

3439
inline const ::flatbuffers::TypeTable *Vector3DAltTypeTable();
@@ -377,6 +382,19 @@ inline ::flatbuffers::Offset<ApplicationData> CreateApplicationDataDirect(
377382

378383
::flatbuffers::Offset<ApplicationData> CreateApplicationData(::flatbuffers::FlatBufferBuilder &_fbb, const ApplicationDataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
379384

385+
386+
inline bool operator==(const MatrixT &lhs, const MatrixT &rhs) {
387+
return
388+
(lhs.rows == rhs.rows) &&
389+
(lhs.columns == rhs.columns) &&
390+
(lhs.values == rhs.values);
391+
}
392+
393+
inline bool operator!=(const MatrixT &lhs, const MatrixT &rhs) {
394+
return !(lhs == rhs);
395+
}
396+
397+
380398
inline MatrixT *Matrix::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
381399
auto _o = std::unique_ptr<MatrixT>(new MatrixT());
382400
UnPackTo(_o.get(), _resolver);
@@ -409,6 +427,22 @@ inline ::flatbuffers::Offset<Matrix> Matrix::Pack(::flatbuffers::FlatBufferBuild
409427
_values);
410428
}
411429

430+
431+
inline bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs) {
432+
return
433+
(lhs.vectors == rhs.vectors) &&
434+
(lhs.vectors_alt == rhs.vectors_alt) &&
435+
((lhs.position == rhs.position) || (lhs.position && rhs.position && *lhs.position == *rhs.position)) &&
436+
(lhs.position_inline == rhs.position_inline) &&
437+
((lhs.matrix == rhs.matrix) || (lhs.matrix && rhs.matrix && *lhs.matrix == *rhs.matrix)) &&
438+
(lhs.matrices.size() == rhs.matrices.size() && std::equal(lhs.matrices.cbegin(), lhs.matrices.cend(), rhs.matrices.cbegin(), [](std::unique_ptr<Geometry::MatrixT> const &a, std::unique_ptr<Geometry::MatrixT> const &b) { return (a == b) || (a && b && *a == *b); }));
439+
}
440+
441+
inline bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs) {
442+
return !(lhs == rhs);
443+
}
444+
445+
412446
inline ApplicationDataT::ApplicationDataT(const ApplicationDataT &o)
413447
: vectors(o.vectors),
414448
vectors_alt(o.vectors_alt),

tests/native_type_test_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ struct Vector3D {
1717
this->y = _y;
1818
this->z = _z;
1919
}
20+
21+
bool operator==(const Vector3D &other) const {
22+
return (x == other.x) && (y == other.y) && (z == other.z);
23+
}
2024
};
2125
} // namespace Native
2226

0 commit comments

Comments
 (0)