From a4341827ac17c14541ea69c67c76aaae5a09ddcc Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 26 Apr 2024 13:07:28 +0200 Subject: [PATCH] QEasingCurve: use comparison helper macros Like with the QLine(F) case, we have to use QT_CORE_REMOVED_SINCE to remove the old member operators, but also need to guard the new friend functions with !(QT_CORE_REMOVED_SINCE), because QEasingCurve is also one of the core types, and on Windows the metatype interface for it is instantiated in removed_api.cpp. Task-number: QTBUG-120308 Change-Id: I1bd66f7230afd3eba868d05fd496ab13a0331178 Reviewed-by: Tatiana Borisova --- src/corelib/compat/removed_api.cpp | 7 ++++ src/corelib/tools/qeasingcurve.cpp | 41 +++++++++++-------- src/corelib/tools/qeasingcurve.h | 8 ++++ .../corelib/tools/qeasingcurve/CMakeLists.txt | 2 + .../tools/qeasingcurve/tst_qeasingcurve.cpp | 25 +++++++---- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 1b987f78868..05bcda09438 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -952,6 +952,13 @@ bool QDir::operator==(const QDir &dir) const return comparesEqual(*this, dir); } +#include "qeasingcurve.h" + +bool QEasingCurve::operator==(const QEasingCurve &other) const +{ + return comparesEqual(*this, other); +} + #include "qfileinfo.h" // inlined API bool QFileInfo::operator==(const QFileInfo &fileinfo) const diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index d8b3367de3d..52602a0256e 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.cpp @@ -1153,32 +1153,37 @@ QEasingCurve::~QEasingCurve() */ /*! - Compare this easing curve with \a other and returns \c true if they are - equal. It will also compare the properties of a curve. - */ -bool QEasingCurve::operator==(const QEasingCurve &other) const -{ - bool res = d_ptr->func == other.d_ptr->func - && d_ptr->type == other.d_ptr->type; - if (res) { - if (d_ptr->config && other.d_ptr->config) { - // catch the config content - res = d_ptr->config->operator==(*(other.d_ptr->config)); + \fn bool QEasingCurve::operator==(const QEasingCurve &lhs, const QEasingCurve &rhs) - } else if (d_ptr->config || other.d_ptr->config) { + Compares easing curve \a lhs with \a rhs and returns \c true if they are + equal; otherwise returns \c false. + It will also compare the properties of the curves. + */ +bool comparesEqual(const QEasingCurve &lhs, const QEasingCurve &rhs) noexcept +{ + bool res = lhs.d_ptr->func == rhs.d_ptr->func + && lhs.d_ptr->type == rhs.d_ptr->type; + if (res) { + if (lhs.d_ptr->config && rhs.d_ptr->config) { + // catch the config content + res = lhs.d_ptr->config->operator==(*(rhs.d_ptr->config)); + + } else if (lhs.d_ptr->config || rhs.d_ptr->config) { // one one has a config object, which could contain default values - res = qFuzzyCompare(amplitude(), other.amplitude()) - && qFuzzyCompare(period(), other.period()) - && qFuzzyCompare(overshoot(), other.overshoot()); + res = qFuzzyCompare(lhs.amplitude(), rhs.amplitude()) + && qFuzzyCompare(lhs.period(), rhs.period()) + && qFuzzyCompare(lhs.overshoot(), rhs.overshoot()); } } return res; } /*! - \fn bool QEasingCurve::operator!=(const QEasingCurve &other) const - Compare this easing curve with \a other and returns \c true if they are not equal. - It will also compare the properties of a curve. + \fn bool QEasingCurve::operator!=(const QEasingCurve &lhs, const QEasingCurve &rhs) + + Compares easing curve \a lhs with \a rhs and returns \c true if they are + not equal; otherwise returns \c false. + It will also compare the properties of the curves. \sa operator==() */ diff --git a/src/corelib/tools/qeasingcurve.h b/src/corelib/tools/qeasingcurve.h index 5b112d7d7d8..61e9aa247d8 100644 --- a/src/corelib/tools/qeasingcurve.h +++ b/src/corelib/tools/qeasingcurve.h @@ -8,6 +8,7 @@ QT_REQUIRE_CONFIG(easingcurve); +#include #include #include @@ -47,9 +48,11 @@ public: void swap(QEasingCurve &other) noexcept { qt_ptr_swap(d_ptr, other.d_ptr); } +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QEasingCurve &other) const; inline bool operator!=(const QEasingCurve &other) const { return !(this->operator==(other)); } +#endif qreal amplitude() const; void setAmplitude(qreal amplitude); @@ -80,6 +83,11 @@ private: #ifndef QT_NO_DATASTREAM friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QEasingCurve &); friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QEasingCurve &); +#endif + friend Q_CORE_EXPORT bool + comparesEqual(const QEasingCurve &lhs, const QEasingCurve &rhs) noexcept; +#if !QT_CORE_REMOVED_SINCE(6, 8) + Q_DECLARE_EQUALITY_COMPARABLE(QEasingCurve) #endif }; Q_DECLARE_SHARED(QEasingCurve) diff --git a/tests/auto/corelib/tools/qeasingcurve/CMakeLists.txt b/tests/auto/corelib/tools/qeasingcurve/CMakeLists.txt index 3f76f8a38fd..2569e0c7a91 100644 --- a/tests/auto/corelib/tools/qeasingcurve/CMakeLists.txt +++ b/tests/auto/corelib/tools/qeasingcurve/CMakeLists.txt @@ -14,4 +14,6 @@ endif() qt_internal_add_test(tst_qeasingcurve SOURCES tst_qeasingcurve.cpp + LIBRARIES + Qt::TestPrivate ) diff --git a/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp b/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp index fc8c1a3e5c8..0a933a1e2b3 100644 --- a/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp +++ b/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include @@ -16,6 +17,7 @@ private slots: void valueForProgress_data(); void valueForProgress(); void setCustomType(); + void comparisonCompiles(); void operators(); void properties(); void metaTypes(); @@ -420,6 +422,11 @@ void tst_QEasingCurve::setCustomType() QCOMPARE(curve.valueForProgress(0.99), 0.99); } +void tst_QEasingCurve::comparisonCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QEasingCurve::operators() { { // member-swap() @@ -447,28 +454,28 @@ void tst_QEasingCurve::operators() curve2 = curve; curve2.setOvershoot(qreal(1.70158)); QCOMPARE(curve.overshoot(), curve2.overshoot()); - QVERIFY(curve2 == curve); + QT_TEST_EQUALITY_OPS(curve2, curve, true); curve.setOvershoot(3.0); - QVERIFY(curve2 != curve); + QT_TEST_EQUALITY_OPS(curve2, curve, false); curve2.setOvershoot(3.0); - QVERIFY(curve2 == curve); + QT_TEST_EQUALITY_OPS(curve2, curve, true); curve2.setType(QEasingCurve::Linear); QCOMPARE(curve.overshoot(), curve2.overshoot()); - QVERIFY(curve2 != curve); + QT_TEST_EQUALITY_OPS(curve2, curve, false); curve2.setType(QEasingCurve::InBack); QCOMPARE(curve.overshoot(), curve2.overshoot()); - QVERIFY(curve2 == curve); + QT_TEST_EQUALITY_OPS(curve2, curve, true); QEasingCurve curve3; QEasingCurve curve4; curve4.setAmplitude(curve4.amplitude()); QEasingCurve curve5; curve5.setAmplitude(0.12345); - QVERIFY(curve3 == curve4); // default value and not assigned - QVERIFY(curve3 != curve5); // unassinged and other value - QVERIFY(curve4 != curve5); + QT_TEST_EQUALITY_OPS(curve3, curve4, true); // default value and not assigned + QT_TEST_EQUALITY_OPS(curve3, curve5, false); // unassinged and other value + QT_TEST_EQUALITY_OPS(curve4, curve5, false); } class tst_QEasingProperties : public QObject @@ -890,7 +897,7 @@ void tst_QEasingCurve::streamInOut() dsw << orig; dsr >> copy; - QCOMPARE(copy == orig, equality); + QT_TEST_EQUALITY_OPS(copy, orig, equality); } QTEST_MAIN(tst_QEasingCurve)