diff options
author | Tatiana Borisova <tatiana.borisova@qt.io> | 2024-03-21 16:28:23 +0100 |
---|---|---|
committer | Tatiana Borisova <tatiana.borisova@qt.io> | 2024-03-26 23:11:09 +0100 |
commit | e7667357712fe35b66a140916c18d8616b2e4f9c (patch) | |
tree | 4db7c386b8c70ace5d5c4e9e8096f4f0059cf9eb | |
parent | 3b186ceef81e233fcaab94a10c98a582c27cfcfd (diff) |
QJsonArray iterators: use new comparison helper macros
New comparison macros are used for following classes:
- QJsonArray::iterator
- QJsonArray::const_iterator
Replace public operators operator==(), operator!=(), operator!<(), etc
of classes to friend methods comparesEqual(), compareThreeWay();
Use *_helper methods to have an access to protected members of
QCborValueConstRef class from friend functions.
Task-number: QTBUG-120300
Change-Id: I9b41b619107ce69d8b6dab4938232fab841aab51
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/serialization/qjsonarray.cpp | 84 | ||||
-rw-r--r-- | src/corelib/serialization/qjsonarray.h | 93 | ||||
-rw-r--r-- | tests/auto/corelib/serialization/json/tst_qtjson.cpp | 35 |
3 files changed, 165 insertions, 47 deletions
diff --git a/src/corelib/serialization/qjsonarray.cpp b/src/corelib/serialization/qjsonarray.cpp index 3ed91d4d0a..0c1b0ac7c8 100644 --- a/src/corelib/serialization/qjsonarray.cpp +++ b/src/corelib/serialization/qjsonarray.cpp @@ -603,6 +603,10 @@ bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept \inmodule QtCore \brief The QJsonArray::iterator class provides an STL-style non-const iterator for QJsonArray. + \compares strong + \compareswith strong QJsonArray::const_iterator + \endcompareswith + QJsonArray::iterator allows you to iterate over a QJsonArray and to modify the array item associated with the iterator. If you want to iterate over a const QJsonArray, use @@ -707,55 +711,55 @@ bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept */ /*! - \fn bool QJsonArray::iterator::operator==(const iterator &other) const - \fn bool QJsonArray::iterator::operator==(const const_iterator &other) const + \fn bool QJsonArray::iterator::operator==(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::iterator::operator==(const iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to the same item as this + Returns \c true if \a lhs points to the same item as \a rhs iterator; otherwise returns \c false. \sa operator!=() */ /*! - \fn bool QJsonArray::iterator::operator!=(const iterator &other) const - \fn bool QJsonArray::iterator::operator!=(const const_iterator &other) const + \fn bool QJsonArray::iterator::operator!=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::iterator::operator!=(const iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to a different item than this + Returns \c true if \a lhs points to a different item than \a rhs iterator; otherwise returns \c false. \sa operator==() */ /*! - \fn bool QJsonArray::iterator::operator<(const iterator& other) const - \fn bool QJsonArray::iterator::operator<(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator<(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::iterator::operator<(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonArray::iterator::operator<=(const iterator& other) const - \fn bool QJsonArray::iterator::operator<=(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator<=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::iterator::operator<=(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + or equal to the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonArray::iterator::operator>(const iterator& other) const - \fn bool QJsonArray::iterator::operator>(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator>(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::iterator::operator>(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonArray::iterator::operator>=(const iterator& other) const - \fn bool QJsonArray::iterator::operator>=(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator>=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::iterator::operator>=(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than or equal to the item pointed to by the \a rhs iterator. */ /*! \fn QJsonArray::iterator &QJsonArray::iterator::operator++() @@ -838,6 +842,10 @@ bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept \inmodule QtCore \brief The QJsonArray::const_iterator class provides an STL-style const iterator for QJsonArray. + \compares strong + \compareswith strong QJsonArray::iterator + \endcompareswith + QJsonArray::const_iterator allows you to iterate over a QJsonArray. If you want to modify the QJsonArray as you iterate over it, use QJsonArray::iterator instead. It is generally a @@ -930,48 +938,48 @@ bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept \sa operator+() */ -/*! \fn bool QJsonArray::const_iterator::operator==(const const_iterator &other) const +/*! \fn bool QJsonArray::const_iterator::operator==(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to the same item as this + Returns \c true if \a lhs points to the same item as \a rhs iterator; otherwise returns \c false. \sa operator!=() */ -/*! \fn bool QJsonArray::const_iterator::operator!=(const const_iterator &other) const +/*! \fn bool QJsonArray::const_iterator::operator!=(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to a different item than this + Returns \c true if \a lhs points to a different item than \a rhs iterator; otherwise returns \c false. \sa operator==() */ /*! - \fn bool QJsonArray::const_iterator::operator<(const const_iterator& other) const + \fn bool QJsonArray::const_iterator::operator<(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonArray::const_iterator::operator<=(const const_iterator& other) const + \fn bool QJsonArray::const_iterator::operator<=(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + or equal to the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonArray::const_iterator::operator>(const const_iterator& other) const + \fn bool QJsonArray::const_iterator::operator>(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonArray::const_iterator::operator>=(const const_iterator& other) const + \fn bool QJsonArray::const_iterator::operator>=(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than or equal to the item pointed to by the \a rhs iterator. */ /*! \fn QJsonArray::const_iterator &QJsonArray::const_iterator::operator++() diff --git a/src/corelib/serialization/qjsonarray.h b/src/corelib/serialization/qjsonarray.h index a2baa7738c..26a04e9196 100644 --- a/src/corelib/serialization/qjsonarray.h +++ b/src/corelib/serialization/qjsonarray.h @@ -94,24 +94,26 @@ public: inline QJsonValueRef *operator->() { return &item; } inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); } +#if QT_CORE_REMOVED_SINCE(6, 8) inline bool operator==(const iterator &o) const { return item.d == o.item.d && item.index == o.item.index; } - inline bool operator!=(const iterator &o) const { return !(*this == o); } + inline bool operator!=(const iterator &o) const { return !operator==(o); } inline bool operator<(const iterator &other) const { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; } inline bool operator<=(const iterator &other) const { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; } - inline bool operator>(const iterator &other) const { return !(*this <= other); } - inline bool operator>=(const iterator &other) const { return !(*this < other); } + inline bool operator>(const iterator &other) const { return !operator<=(other); } + inline bool operator>=(const iterator &other) const { return !operator<(other); } inline bool operator==(const const_iterator &o) const { return item.d == o.item.d && item.index == o.item.index; } - inline bool operator!=(const const_iterator &o) const { return !(*this == o); } + inline bool operator!=(const const_iterator &o) const { return !operator==(o); } inline bool operator<(const const_iterator &other) const { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; } inline bool operator<=(const const_iterator &other) const { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; } - inline bool operator>(const const_iterator &other) const { return !(*this <= other); } - inline bool operator>=(const const_iterator &other) const { return !(*this < other); } + inline bool operator>(const const_iterator &other) const { return !operator<=(other); } + inline bool operator>=(const const_iterator &other) const { return !operator<(other); } +#endif inline iterator &operator++() { ++item.index; return *this; } inline iterator operator++(int) { iterator n = *this; ++item.index; return n; } inline iterator &operator--() { item.index--; return *this; } @@ -123,6 +125,53 @@ public: inline qsizetype operator-(iterator j) const { return item.index - j.item.index; } private: + // Helper functions + static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept + { + return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index; + } + + static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept + { + return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index; + } + + static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs, + const iterator &rhs) noexcept + { + Q_ASSERT(lhs.item.d == rhs.item.d); + return Qt::compareThreeWay(lhs.item.index, rhs.item.index); + } + + static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs, + const const_iterator &rhs) noexcept + { + Q_ASSERT(lhs.item.d == rhs.item.d); + return Qt::compareThreeWay(lhs.item.index, rhs.item.index); + } + + // Compare friends + friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept + { + return comparesEqual_helper(lhs, rhs); + } + friend Qt::strong_ordering compareThreeWay(const iterator &lhs, + const iterator &rhs) noexcept + { + return compareThreeWay_helper(lhs, rhs); + } + Q_DECLARE_STRONGLY_ORDERED(iterator) + friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept + { + return comparesEqual_helper(lhs, rhs); + } + friend Qt::strong_ordering compareThreeWay(const iterator &lhs, + const const_iterator &rhs) noexcept + { + return compareThreeWay_helper(lhs, rhs); + } + Q_DECLARE_STRONGLY_ORDERED(iterator, const_iterator) + QJsonValueRef item; friend class QJsonArray; }; @@ -152,15 +201,17 @@ public: inline const QJsonValueConstRef *operator->() const { return &item; } inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); } +#if QT_CORE_REMOVED_SINCE(6, 8) inline bool operator==(const const_iterator &o) const { return item.d == o.item.d && item.index == o.item.index; } - inline bool operator!=(const const_iterator &o) const { return !(*this == o); } + inline bool operator!=(const const_iterator &o) const { return !operator==(o); } inline bool operator<(const const_iterator &other) const { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; } inline bool operator<=(const const_iterator &other) const { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; } - inline bool operator>(const const_iterator &other) const { return !(*this <= other); } - inline bool operator>=(const const_iterator &other) const { return !(*this < other); } + inline bool operator>(const const_iterator &other) const { return !operator<=(other); } + inline bool operator>=(const const_iterator &other) const { return !operator<(other); } +#endif inline const_iterator &operator++() { ++item.index; return *this; } inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; } inline const_iterator &operator--() { item.index--; return *this; } @@ -172,6 +223,30 @@ public: inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; } private: + // Helper functions + static bool comparesEqual_helper(const const_iterator &lhs, + const const_iterator &rhs) noexcept + { + return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index; + } + static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs, + const const_iterator &rhs) noexcept + { + Q_ASSERT(lhs.item.d == rhs.item.d); + return Qt::compareThreeWay(lhs.item.index, rhs.item.index); + } + + // Compare friends + friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept + { + return comparesEqual_helper(lhs, rhs); + } + friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs, + const const_iterator &rhs) noexcept + { + return compareThreeWay_helper(lhs, rhs); + } + Q_DECLARE_STRONGLY_ORDERED(const_iterator) QJsonValueConstRef item; friend class QJsonArray; }; diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 2f3e8e07d5..51af8ab78e 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -176,6 +176,10 @@ void tst_QtJson::initTestCase() void tst_QtJson::compareCompiles() { QTestPrivate::testEqualityOperatorsCompile<QJsonArray>(); + QTestPrivate::testAllComparisonOperatorsCompile<QJsonArray::iterator>(); + QTestPrivate::testAllComparisonOperatorsCompile<QJsonArray::const_iterator>(); + QTestPrivate::testAllComparisonOperatorsCompile<QJsonArray::iterator, + QJsonArray::const_iterator>(); QTestPrivate::testEqualityOperatorsCompile<QJsonDocument>(); QTestPrivate::testEqualityOperatorsCompile<QJsonObject>(); QTestPrivate::testEqualityOperatorsCompile<QJsonValue>(); @@ -1210,7 +1214,11 @@ void tst_QtJson::testArrayIteration() int i = 0; for (QJsonArray::iterator it = array.begin(); it != array.end(); ++it, ++i) { QJsonValue value = (*it); + QJsonArray::iterator it1 = it; QCOMPARE((double)i, value.toDouble()); + QT_TEST_EQUALITY_OPS(QJsonArray::iterator(), QJsonArray::iterator(), true); + QT_TEST_EQUALITY_OPS(QJsonArray::iterator(), it, false); + QT_TEST_EQUALITY_OPS(it1, it, true); } QCOMPARE(array.begin()->toDouble(), array.constBegin()->toDouble()); @@ -1265,6 +1273,13 @@ void tst_QtJson::testArrayIteration() QCOMPARE(QJsonValue(*it2).toDouble(), 7.); it2 = it - 1; QCOMPARE(QJsonValue(*it2).toDouble(), 1.); + QT_TEST_EQUALITY_OPS(it, it2, false); + it = array.begin(); + QT_TEST_EQUALITY_OPS(it, array.begin(), true); + it2 = it + 5; + QT_TEST_ALL_COMPARISON_OPS(it2, it, Qt::strong_ordering::greater); + it += 5; + QT_TEST_EQUALITY_OPS(it, it2, true); } { @@ -1284,6 +1299,26 @@ void tst_QtJson::testArrayIteration() it = array.erase(it); QCOMPARE(array.size() , 0); QCOMPARE(it, array.end()); + QT_TEST_EQUALITY_OPS(it, array.end(), true); + + { + int i = 0; + for (QJsonArray::const_iterator it = array.constBegin(); + it != array.constEnd(); ++it, ++i) { + QJsonArray::const_iterator it1 = it; + QT_TEST_EQUALITY_OPS(QJsonArray::const_iterator(), QJsonArray::const_iterator(), true); + QT_TEST_EQUALITY_OPS(QJsonArray::const_iterator(), it, false); + QT_TEST_EQUALITY_OPS(it1, it, true); + } + } + + { + QJsonArray::iterator nonConstIt = array.begin(); + QJsonArray::const_iterator it = array.constBegin(); + QT_TEST_EQUALITY_OPS(nonConstIt, it, true); + it+=1; + QT_TEST_ALL_COMPARISON_OPS(nonConstIt, it, Qt::strong_ordering::less); + } } void tst_QtJson::testObjectFind() |