Skip to content

Commit e6469d1

Browse files
Fix --gen-compare to not generate comparators for native types.
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 e6469d1

File tree

6 files changed

+36
-225
lines changed

6 files changed

+36
-225
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.fbs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,11 @@ struct Vector3DAlt (native_type:"Native::Vector3D", native_type_pack_name:"Vecto
1414
c:float;
1515
}
1616

17-
// table Matrix (native_type:"Native::Matrix") {
18-
table Matrix {
19-
rows:int32;
20-
columns:int32;
21-
values:[float];
22-
}
23-
2417
table ApplicationData {
2518
vectors:[Vector3D];
2619
vectors_alt:[Vector3DAlt];
2720
position:Vector3D;
2821
position_inline:Vector3D (native_inline);
29-
matrix:Matrix;
30-
matrices:[Matrix];
3122
}
3223

3324
root_type ApplicationData;

0 commit comments

Comments
 (0)