QCborValueConstRef/QCborValueRef: use new comparison helper macros

Replace public operators operator==() and operator!=() of
QCborValueConstRef and QCborValueRef classes
to friend methods comparesEqual().
Replace public operator<() of QCborValueConstRef and QCborValueRef
classes to friend methods compareThreeWay() respectively.

Use QT_CORE_REMOVED_SINCE to get rid of current comparison methods
and replace them with a friend.

Delete #if 0 && __has_include(<compare>) blocks,
since they are not required anymore.

Add comparison() test-case for QCborValueConstRef/QCborValueRef
testing.

Add QCborValue::operator==()/QCborValue::operator!=()/
QCborValue::operator<() and QCborValueRef::operator==()/
QCborValueRef::operator!=()/QCborValueRef::operator<() operators
to the removed_api file.

Task-number: QTBUG-120300
Change-Id: I2e8e4e32b7b5b49da321364cc12986e9c64b5f37
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Tatiana Borisova 2024-03-06 15:25:13 +01:00
parent 5401a9a6cd
commit 2b2cd119e1
3 changed files with 107 additions and 34 deletions

View File

@ -2742,6 +2742,16 @@ QString QCborValueConstRef::concreteString(QCborValueConstRef self, const QStrin
return self.d->stringAt(self.i); return self.d->stringAt(self.i);
} }
bool comparesEqual(const QCborValueConstRef &lhs, const QCborValueConstRef &rhs) noexcept
{
return lhs.compare(rhs.concrete()) == 0;
}
bool comparesEqual(const QCborValueConstRef &lhs, const QCborValue &rhs) noexcept
{
return lhs.compare(rhs) == 0;
}
QCborValue QCborValueConstRef::concrete(QCborValueConstRef self) noexcept QCborValue QCborValueConstRef::concrete(QCborValueConstRef self) noexcept
{ {
return self.d->valueAt(self.i); return self.d->valueAt(self.i);

View File

@ -368,22 +368,6 @@ public:
int compare(const QCborValue &other) const int compare(const QCborValue &other) const
{ return concrete().compare(other); } { return concrete().compare(other); }
#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborValue &other) const
{
int c = compare(other);
if (c > 0) return std::strong_ordering::greater;
if (c == 0) return std::strong_ordering::equivalent;
return std::strong_ordering::less;
}
#else
bool operator==(const QCborValue &other) const
{ return compare(other) == 0; }
bool operator!=(const QCborValue &other) const
{ return !(*this == other); }
bool operator<(const QCborValue &other) const
{ return compare(other) < 0; }
#endif
QVariant toVariant() const { return concrete().toVariant(); } QVariant toVariant() const { return concrete().toVariant(); }
inline QJsonValue toJsonValue() const; // in qjsonvalue.h inline QJsonValue toJsonValue() const; // in qjsonvalue.h
@ -405,7 +389,24 @@ protected:
friend class QCborContainerPrivate; friend class QCborContainerPrivate;
QCborValue concrete() const noexcept { return concrete(*this); } QCborValue concrete() const noexcept { return concrete(*this); }
friend Q_CORE_EXPORT bool comparesEqual(const QCborValueConstRef &lhs,
const QCborValueConstRef &rhs) noexcept;
friend Qt::strong_ordering compareThreeWay(const QCborValueConstRef &lhs,
const QCborValueConstRef &rhs) noexcept
{
int c = lhs.compare(rhs.concrete());
return Qt::compareThreeWay(c, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QCborValueConstRef)
friend Q_CORE_EXPORT bool comparesEqual(const QCborValueConstRef &lhs,
const QCborValue &rhs) noexcept;
friend Qt::strong_ordering compareThreeWay(const QCborValueConstRef &lhs,
const QCborValue &rhs) noexcept
{
int c = lhs.compare(rhs);
return Qt::compareThreeWay(c, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QCborValueConstRef, QCborValue)
static Q_CORE_EXPORT QCborValue concrete(QCborValueConstRef that) noexcept; static Q_CORE_EXPORT QCborValue concrete(QCborValueConstRef that) noexcept;
static Q_CORE_EXPORT QCborValue::Type concreteType(QCborValueConstRef that) noexcept Q_DECL_PURE_FUNCTION; static Q_CORE_EXPORT QCborValue::Type concreteType(QCborValueConstRef that) noexcept Q_DECL_PURE_FUNCTION;
static Q_CORE_EXPORT bool static Q_CORE_EXPORT bool
@ -523,19 +524,11 @@ public:
int compare(const QCborValue &other) const int compare(const QCborValue &other) const
{ return concrete().compare(other); } { return concrete().compare(other); }
#if 0 && __has_include(<compare>) #if QT_CORE_REMOVED_SINCE(6, 8)
std::strong_ordering operator<=>(const QCborValue &other) const
{
int c = compare(other);
if (c > 0) return std::strong_ordering::greater;
if (c == 0) return std::strong_ordering::equivalent;
return std::strong_ordering::less;
}
#else
bool operator==(const QCborValue &other) const bool operator==(const QCborValue &other) const
{ return compare(other) == 0; } { return compare(other) == 0; }
bool operator!=(const QCborValue &other) const bool operator!=(const QCborValue &other) const
{ return !(*this == other); } { return !operator==(other); }
bool operator<(const QCborValue &other) const bool operator<(const QCborValue &other) const
{ return compare(other) < 0; } { return compare(other) < 0; }
#endif #endif

View File

@ -27,7 +27,7 @@ private slots:
void tagged(); void tagged();
void extendedTypes_data(); void extendedTypes_data();
void extendedTypes(); void extendedTypes();
void compareCompiles() { QTestPrivate::testAllComparisonOperatorsCompile<QCborValue>(); } void compareCompiles();
void copyCompare_data() { basics_data(); } void copyCompare_data() { basics_data(); }
void copyCompare(); void copyCompare();
@ -79,6 +79,8 @@ private slots:
void mapNested(); void mapNested();
void sorting(); void sorting();
void comparison_data();
void comparison();
void toCbor_data(); void toCbor_data();
void toCbor(); void toCbor();
@ -446,6 +448,16 @@ void tst_QCborValue::extendedTypes_data()
<< QCborValue(uuid.toRfc4122() + "\1\2\3\4") << QCborValue(uuid.toRfc4122()); << QCborValue(uuid.toRfc4122() + "\1\2\3\4") << QCborValue(uuid.toRfc4122());
} }
void tst_QCborValue::compareCompiles()
{
QTestPrivate::testAllComparisonOperatorsCompile<QCborValue>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborValueRef, QCborValueConstRef>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborValueConstRef, QCborValue>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborValueRef, QCborValue>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborValueRef>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborValueConstRef>();
}
void tst_QCborValue::extendedTypes() void tst_QCborValue::extendedTypes()
{ {
QFETCH(QCborValue, extended); QFETCH(QCborValue, extended);
@ -1110,8 +1122,7 @@ void tst_QCborValue::mapMutation()
QVERIFY(v.isTrue()); QVERIFY(v.isTrue());
QCOMPARE(m, QCborMap({{42, true}})); QCOMPARE(m, QCborMap({{42, true}}));
QVERIFY(m.begin()->isTrue()); QVERIFY(m.begin()->isTrue());
QVERIFY(m.begin().value() == v); QT_TEST_EQUALITY_OPS(m.begin().value(), v, true);
QVERIFY(v == m.begin().value());
} }
QVERIFY(m == QCborMap({{42, true}})); QVERIFY(m == QCborMap({{42, true}}));
@ -1135,10 +1146,10 @@ void tst_QCborValue::mapMutation()
auto end = m.end(); auto end = m.end();
QCOMPARE(end - it, 2); QCOMPARE(end - it, 2);
QCOMPARE(it + 2, end); QCOMPARE(it + 2, end);
QCOMPARE(it.key(), QCborValue(42)); QT_TEST_EQUALITY_OPS(it.key(), QCborValue(42), true);
QCOMPARE(it.value(), QCborValue(2.5)); QT_TEST_EQUALITY_OPS(it.value(), QCborValue(2.5), true);
QCOMPARE((++it).value(), QCborValue(nullptr)); QT_TEST_EQUALITY_OPS((++it).value(), QCborValue(nullptr), true);
QCOMPARE(it.key(), QCborValue(nullptr)); QT_TEST_EQUALITY_OPS(it.key(), QCborValue(nullptr), true);
QVERIFY(m2 == m); QVERIFY(m2 == m);
QVERIFY(m == m2); QVERIFY(m == m2);
@ -1191,11 +1202,13 @@ void tst_QCborValue::mapMutateWithCopies()
// see QTBUG-83366 // see QTBUG-83366
QCborMap map; QCborMap map;
map[QLatin1String("value")] = "TEST"; map[QLatin1String("value")] = "TEST";
QT_TEST_EQUALITY_OPS(map[QLatin1String("value")], "TEST", true);
QCOMPARE(map.size(), 1); QCOMPARE(map.size(), 1);
QCOMPARE(map.value("value"), "TEST"); QCOMPARE(map.value("value"), "TEST");
QCborValue v = map.value("value"); QCborValue v = map.value("value");
map[QLatin1String("prop2")] = v; map[QLatin1String("prop2")] = v;
QT_TEST_EQUALITY_OPS(map[QLatin1String("prop2")], v, true);
QCOMPARE(map.size(), 2); QCOMPARE(map.size(), 2);
QCOMPARE(map.value("value"), "TEST"); QCOMPARE(map.value("value"), "TEST");
QCOMPARE(map.value("prop2"), "TEST"); QCOMPARE(map.value("prop2"), "TEST");
@ -1209,6 +1222,7 @@ void tst_QCborValue::mapMutateWithCopies()
// same as previous, but this is a QJsonValueRef // same as previous, but this is a QJsonValueRef
QCborValueRef rv = map[QLatin1String("prop2")]; QCborValueRef rv = map[QLatin1String("prop2")];
rv = map[QLatin1String("value")]; rv = map[QLatin1String("value")];
QT_TEST_EQUALITY_OPS(map[QLatin1String("value")], rv, true);
QCOMPARE(map.size(), 2); QCOMPARE(map.size(), 2);
QCOMPARE(map.value("value"), "TEST"); QCOMPARE(map.value("value"), "TEST");
QCOMPARE(map.value("prop2"), "TEST"); QCOMPARE(map.value("prop2"), "TEST");
@ -1223,6 +1237,7 @@ void tst_QCborValue::mapMutateWithCopies()
// after we create the source QCborValueRef // after we create the source QCborValueRef
QCborValueRef rv = map[QLatin1String("value")]; QCborValueRef rv = map[QLatin1String("value")];
map[QLatin1String("prop2")] = rv; map[QLatin1String("prop2")] = rv;
QT_TEST_EQUALITY_OPS(map[QLatin1String("prop2")], rv, true);
QCOMPARE(map.size(), 2); QCOMPARE(map.size(), 2);
QCOMPARE(map.value("value"), "TEST"); QCOMPARE(map.value("value"), "TEST");
QCOMPARE(map.value("prop2"), "TEST"); QCOMPARE(map.value("prop2"), "TEST");
@ -3068,6 +3083,61 @@ void tst_QCborValue::testlibFormatting()
QCOMPARE(actual, expected); QCOMPARE(actual, expected);
} }
void tst_QCborValue::comparison_data()
{
QTest::addColumn<QCborValue>("lhs");
QTest::addColumn<QCborValue>("rhs");
QTest::addColumn<Qt::strong_ordering>("expectedOrdering");
auto addRow = [](QCborValue lhs, QCborValue rhs, Qt::strong_ordering order) {
QTest::addRow("%s-cmp-%s", qPrintable(lhs.toDiagnosticNotation()),
qPrintable(rhs.toDiagnosticNotation()))
<< lhs << rhs << order;
};
// typical, sorts as expected
addRow(0, 0, Qt::strong_ordering::equivalent);
addRow(1, 0, Qt::strong_ordering::greater);
addRow(0, 1, Qt::strong_ordering::less);
addRow(10.0, 10.0, Qt::strong_ordering::equivalent);
addRow(10.5, 10.8, Qt::strong_ordering::less);
addRow(-10.5, -10.8, Qt::strong_ordering::less);
addRow("Qt","Qt", Qt::strong_ordering::equivalent);
addRow("qt","Qt", Qt::strong_ordering::greater);
// atypical gotchas
addRow(0, -1, Qt::strong_ordering::less);
addRow(10, 10.0, Qt::strong_ordering::less);
addRow(0, "Qt", Qt::strong_ordering::less);
}
void tst_QCborValue::comparison()
{
QFETCH(QCborValue, lhs);
QFETCH(QCborValue, rhs);
QFETCH(Qt::strong_ordering, expectedOrdering);
QCborArray array{lhs, rhs};
QCborValueConstRef lhsCRef = array.constBegin()[0];
QCborValueConstRef rhsCRef = array.constBegin()[1];
QCborValueRef lhsRef = array[0];
QCborValueRef rhsRef = array[1];
// QCborValue vs QCborValue
QT_TEST_ALL_COMPARISON_OPS(lhs, rhs, expectedOrdering);
// QCborValueConstRef vs QCborValueConstRef
QT_TEST_ALL_COMPARISON_OPS(lhsCRef, rhsCRef, expectedOrdering);
// QCborValueRef vs QCborValueRef
QT_TEST_ALL_COMPARISON_OPS(lhsRef, rhsRef, expectedOrdering);
// QCborValue vs QCborValueConstRef (and reverse)
QT_TEST_ALL_COMPARISON_OPS(lhs, rhsCRef, expectedOrdering);
// QCborValue vs QCborValueRef (and reverse)
QT_TEST_ALL_COMPARISON_OPS(lhs, rhsRef, expectedOrdering);
// QCborValueConstRef vs QCborValueRef (and reverse)
QT_TEST_ALL_COMPARISON_OPS(lhsCRef, rhsRef, expectedOrdering);
}
QTEST_MAIN(tst_QCborValue) QTEST_MAIN(tst_QCborValue)
#include "tst_qcborvalue.moc" #include "tst_qcborvalue.moc"