From ed71387d1cc06afff42ac844a3887778685ce793 Mon Sep 17 00:00:00 2001 From: Tatiana Borisova Date: Tue, 23 Apr 2024 15:16:40 +0200 Subject: [PATCH] QtPrivate::ResultIteratorBase: use modernize comparisons Replace class operators operator==(), operator!=() of QtPrivate::ResultIteratorBase to friend method comparesEqual() and Q_DECLARE_EQUALITY_COMPARABLE macro. Use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of current comparison methods and replace them with a friend. Task-number: QTBUG-120304 Change-Id: Ib9a50a400df86d1dc034d2a0cfee804109a2b93f Reviewed-by: Ivan Solovev --- src/corelib/compat/removed_api.cpp | 14 ++++++++ src/corelib/thread/qresultstore.cpp | 10 ------ src/corelib/thread/qresultstore.h | 9 +++++ .../thread/qresultstore/CMakeLists.txt | 1 + .../thread/qresultstore/tst_qresultstore.cpp | 33 ++++++++++++------- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 05bcda0943..8bb7dad008 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -1085,6 +1085,20 @@ bool QRegularExpression::operator==(const QRegularExpression &other) const } #endif // QT_CONFIG(regularexpression) +#if QT_CONFIG(future) +#include "qresultstore.h" + +bool QtPrivate::ResultIteratorBase::operator==(const QtPrivate::ResultIteratorBase &other) const +{ + return comparesEqual(*this, other); +} + +bool QtPrivate::ResultIteratorBase::operator!=(const QtPrivate::ResultIteratorBase &other) const +{ + return !comparesEqual(*this, other); +} +#endif // QT_CONFIG(future) + #include "qstring.h" // inlined API #if QT_CONFIG(thread) diff --git a/src/corelib/thread/qresultstore.cpp b/src/corelib/thread/qresultstore.cpp index 14ed7c6b87..8b7601f5b0 100644 --- a/src/corelib/thread/qresultstore.cpp +++ b/src/corelib/thread/qresultstore.cpp @@ -88,16 +88,6 @@ void ResultIteratorBase::batchedAdvance() m_vectorIndex = 0; } -bool ResultIteratorBase::operator==(const ResultIteratorBase &other) const -{ - return (mapIterator == other.mapIterator && m_vectorIndex == other.m_vectorIndex); -} - -bool ResultIteratorBase::operator!=(const ResultIteratorBase &other) const -{ - return !operator==(other); -} - bool ResultIteratorBase::isVector() const { return mapIterator.value().isVector(); diff --git a/src/corelib/thread/qresultstore.h b/src/corelib/thread/qresultstore.h index 30ce1fe904..f21068206f 100644 --- a/src/corelib/thread/qresultstore.h +++ b/src/corelib/thread/qresultstore.h @@ -46,12 +46,21 @@ public: ResultIteratorBase operator++(); int batchSize() const; void batchedAdvance(); +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const ResultIteratorBase &other) const; bool operator!=(const ResultIteratorBase &other) const; +#endif bool isVector() const; bool canIncrementVectorIndex() const; bool isValid() const; +private: + friend bool comparesEqual(const ResultIteratorBase &lhs, + const ResultIteratorBase &rhs) noexcept + { + return (lhs.mapIterator == rhs.mapIterator && lhs.m_vectorIndex == rhs.m_vectorIndex); + } + Q_DECLARE_EQUALITY_COMPARABLE(ResultIteratorBase) protected: QMap::const_iterator mapIterator; int m_vectorIndex; diff --git a/tests/auto/corelib/thread/qresultstore/CMakeLists.txt b/tests/auto/corelib/thread/qresultstore/CMakeLists.txt index 0f9d8d9e52..5abfc14ac6 100644 --- a/tests/auto/corelib/thread/qresultstore/CMakeLists.txt +++ b/tests/auto/corelib/thread/qresultstore/CMakeLists.txt @@ -16,4 +16,5 @@ qt_internal_add_test(tst_qresultstore tst_qresultstore.cpp LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) diff --git a/tests/auto/corelib/thread/qresultstore/tst_qresultstore.cpp b/tests/auto/corelib/thread/qresultstore/tst_qresultstore.cpp index 265b2cd1f6..722184a72a 100644 --- a/tests/auto/corelib/thread/qresultstore/tst_qresultstore.cpp +++ b/tests/auto/corelib/thread/qresultstore/tst_qresultstore.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include - +#include #include using namespace QtPrivate; @@ -23,6 +23,7 @@ class tst_QtConcurrentResultStore : public QObject public slots: void init(); private slots: + void compareCompiles(); void construction(); void iterators(); void addResult(); @@ -52,6 +53,11 @@ void tst_QtConcurrentResultStore::init() vec1 = QList { 4, 5 }; } +void tst_QtConcurrentResultStore::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QtConcurrentResultStore::construction() { ResultStoreBase store; @@ -74,17 +80,20 @@ void tst_QtConcurrentResultStore::iterators() storebase.addResult(1, &int1); // ResultStoreBase does not take ownership, only ResultStore<> does. ResultIteratorBase it = storebase.begin(); QCOMPARE(it.resultIndex(), 0); - QCOMPARE(it, storebase.begin()); + QT_TEST_EQUALITY_OPS(it, storebase.begin(), true); QVERIFY(it != storebase.end()); ++it; QCOMPARE(it.resultIndex(), 1); QVERIFY(it != storebase.begin()); QVERIFY(it != storebase.end()); + QT_TEST_EQUALITY_OPS(it, storebase.begin(), false); + QT_TEST_EQUALITY_OPS(it, storebase.end(), false); ++it; QVERIFY(it != storebase.begin()); QCOMPARE(it, storebase.end()); + QT_TEST_EQUALITY_OPS(it, storebase.end(), true); } } @@ -147,8 +156,8 @@ void tst_QtConcurrentResultStore::addResults() store.addResults(-1, &vec1); ResultIteratorBase it = store.begin(); QCOMPARE(it.resultIndex(), 0); - QCOMPARE(it, store.begin()); - QVERIFY(it != store.end()); + QT_TEST_EQUALITY_OPS(it, store.begin(), true); + QT_TEST_EQUALITY_OPS(it, store.end(), false); ++it; QCOMPARE(it.resultIndex(), 1); @@ -162,7 +171,7 @@ void tst_QtConcurrentResultStore::addResults() QCOMPARE(it.resultIndex(), 3); ++it; - QCOMPARE(it, store.end()); + QT_TEST_EQUALITY_OPS(it, store.end(), true); QList empty; const auto countBefore = store.count(); @@ -184,22 +193,22 @@ void tst_QtConcurrentResultStore::resultIndex() ResultIteratorBase it = store.begin(); QCOMPARE(it.resultIndex(), 0); - QVERIFY(it == store.begin()); - QVERIFY(it != store.end()); + QT_TEST_EQUALITY_OPS(it, store.begin(), true); + QT_TEST_EQUALITY_OPS(it, store.end(), false); ++it; QCOMPARE(it.resultIndex(), 1); - QVERIFY(it != store.begin()); - QVERIFY(it != store.end()); + QT_TEST_EQUALITY_OPS(it, store.begin(), false); + QT_TEST_EQUALITY_OPS(it, store.end(), false); ++it; QCOMPARE(it.resultIndex(), 2); - QVERIFY(it != store.end()); + QT_TEST_EQUALITY_OPS(it, store.end(), false); ++it; QCOMPARE(it.resultIndex(), 3); - QVERIFY(it != store.end()); + QT_TEST_EQUALITY_OPS(it, store.end(), false); ++it; - QVERIFY(it == store.end()); + QT_TEST_EQUALITY_OPS(it, store.end(), true); QCOMPARE(store.resultAt(0).value(), int0); QCOMPARE(store.resultAt(1).value(), vec0[0]);