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 /src | |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/serialization/qjsonarray.cpp | 84 | ||||
-rw-r--r-- | src/corelib/serialization/qjsonarray.h | 93 |
2 files changed, 130 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; }; |