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.
This commit is contained in:
cosmith-nvidia
2025-11-04 16:43:00 -08:00
committed by GitHub
parent 5fe90a9160
commit 4173b84d4b
5 changed files with 49 additions and 5 deletions

View File

@@ -537,14 +537,13 @@ if(FLATBUFFERS_BUILD_TESTS)
add_definitions(-DFLATBUFFERS_TEST_PATH_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/)
# The flattest target needs some generated files
SET(FLATC_OPT --cpp --gen-mutable --gen-object-api --reflect-names)
SET(FLATC_OPT_COMP ${FLATC_OPT};--gen-compare)
SET(FLATC_OPT_COMP --cpp --gen-compare --gen-mutable --gen-object-api --reflect-names)
SET(FLATC_OPT_SCOPED_ENUMS ${FLATC_OPT_COMP};--scoped-enums)
compile_schema_for_test(tests/alignment_test.fbs "${FLATC_OPT_COMP}")
compile_schema_for_test(tests/arrays_test.fbs "${FLATC_OPT_SCOPED_ENUMS}")
compile_schema_for_test(tests/native_inline_table_test.fbs "${FLATC_OPT_COMP}")
compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT}")
compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT_COMP}")
compile_schema_for_test(tests/key_field/key_field_sample.fbs "${FLATC_OPT_COMP}")
compile_schema_for_test(tests/64bit/test_64bit.fbs "${FLATC_OPT_COMP};--bfbs-gen-embed")
compile_schema_for_test(tests/64bit/evolution/v1.fbs "${FLATC_OPT_COMP}")

View File

@@ -355,7 +355,7 @@ flatc(
)
flatc(
["--cpp", "--gen-mutable", "--gen-object-api", "--reflect-names"],
["--cpp", "--gen-compare", "--gen-mutable", "--gen-object-api", "--reflect-names"],
schema="native_type_test.fbs",
)

View File

@@ -512,7 +512,8 @@ class CppGenerator : public BaseGenerator {
// Generate forward declarations for all equal operators
if (opts_.generate_object_based_api && opts_.gen_compare) {
for (const auto& struct_def : parser_.structs_.vec) {
if (!struct_def->generated) {
const auto native_type = struct_def->attributes.Lookup("native_type");
if (!struct_def->generated && !native_type) {
SetNameSpace(struct_def->defined_namespace);
auto nativeName = NativeName(Name(*struct_def), struct_def, opts_);
code_ += "bool operator==(const " + nativeName + " &lhs, const " +
@@ -2190,6 +2191,12 @@ class CppGenerator : public BaseGenerator {
void GenCompareOperator(const StructDef& struct_def,
const std::string& accessSuffix = "") {
// Do not generate compare operators for native types.
const auto native_type = struct_def.attributes.Lookup("native_type");
if (native_type) {
return;
}
std::string compare_op;
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {

View File

@@ -29,6 +29,11 @@ struct ApplicationData;
struct ApplicationDataBuilder;
struct ApplicationDataT;
bool operator==(const MatrixT &lhs, const MatrixT &rhs);
bool operator!=(const MatrixT &lhs, const MatrixT &rhs);
bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs);
bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs);
inline const ::flatbuffers::TypeTable *Vector3DTypeTable();
inline const ::flatbuffers::TypeTable *Vector3DAltTypeTable();
@@ -377,6 +382,19 @@ inline ::flatbuffers::Offset<ApplicationData> CreateApplicationDataDirect(
::flatbuffers::Offset<ApplicationData> CreateApplicationData(::flatbuffers::FlatBufferBuilder &_fbb, const ApplicationDataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
inline bool operator==(const MatrixT &lhs, const MatrixT &rhs) {
return
(lhs.rows == rhs.rows) &&
(lhs.columns == rhs.columns) &&
(lhs.values == rhs.values);
}
inline bool operator!=(const MatrixT &lhs, const MatrixT &rhs) {
return !(lhs == rhs);
}
inline MatrixT *Matrix::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
auto _o = std::unique_ptr<MatrixT>(new MatrixT());
UnPackTo(_o.get(), _resolver);
@@ -409,6 +427,22 @@ inline ::flatbuffers::Offset<Matrix> Matrix::Pack(::flatbuffers::FlatBufferBuild
_values);
}
inline bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs) {
return
(lhs.vectors == rhs.vectors) &&
(lhs.vectors_alt == rhs.vectors_alt) &&
((lhs.position == rhs.position) || (lhs.position && rhs.position && *lhs.position == *rhs.position)) &&
(lhs.position_inline == rhs.position_inline) &&
((lhs.matrix == rhs.matrix) || (lhs.matrix && rhs.matrix && *lhs.matrix == *rhs.matrix)) &&
(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); }));
}
inline bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs) {
return !(lhs == rhs);
}
inline ApplicationDataT::ApplicationDataT(const ApplicationDataT &o)
: vectors(o.vectors),
vectors_alt(o.vectors_alt),

View File

@@ -17,6 +17,10 @@ struct Vector3D {
this->y = _y;
this->z = _z;
}
bool operator==(const Vector3D &other) const {
return (x == other.x) && (y == other.y) && (z == other.z);
}
};
} // namespace Native