QJsonObject iterators: use new comparison helper macros

New comparison macros are used for following classes:
- QJsonObject::iterator
- QJsonObject::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: I308e2c0a549ff186c1d6339b38e989b80dc388a3
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Tatiana Borisova 2024-03-22 14:34:59 +01:00
parent 4f6600ee65
commit de2a467a77
3 changed files with 160 additions and 57 deletions

View File

@ -840,6 +840,10 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\brief The QJsonObject::iterator class provides an STL-style non-const iterator for QJsonObject.
\compares strong
\compareswith strong QJsonObject::const_iterator
\endcompareswith
QJsonObject::iterator allows you to iterate over a QJsonObject
and to modify the value (but not the key) stored under
a particular key. If you want to iterate over a const QJsonObject, you
@ -973,55 +977,55 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
*/
/*!
\fn bool QJsonObject::iterator::operator==(const iterator &other) const
\fn bool QJsonObject::iterator::operator==(const const_iterator &other) const
\fn bool QJsonObject::iterator::operator==(const iterator &lhs, const iterator &rhs)
\fn bool QJsonObject::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 QJsonObject::iterator::operator!=(const iterator &other) const
\fn bool QJsonObject::iterator::operator!=(const const_iterator &other) const
\fn bool QJsonObject::iterator::operator!=(const iterator &lhs, const iterator &rhs)
\fn bool QJsonObject::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 QJsonObject::iterator::operator<(const iterator& other) const
\fn bool QJsonObject::iterator::operator<(const const_iterator& other) const
\fn bool QJsonObject::iterator::operator<(const iterator &lhs, const iterator &rhs)
\fn bool QJsonObject::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 QJsonObject::iterator::operator<=(const iterator& other) const
\fn bool QJsonObject::iterator::operator<=(const const_iterator& other) const
\fn bool QJsonObject::iterator::operator<=(const iterator &lhs, const iterator &rhs)
\fn bool QJsonObject::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 QJsonObject::iterator::operator>(const iterator& other) const
\fn bool QJsonObject::iterator::operator>(const const_iterator& other) const
\fn bool QJsonObject::iterator::operator>(const iterator &lhs, const iterator &rhs)
\fn bool QJsonObject::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 QJsonObject::iterator::operator>=(const iterator& other) const
\fn bool QJsonObject::iterator::operator>=(const const_iterator& other) const
\fn bool QJsonObject::iterator::operator>=(const iterator &lhs, const iterator &rhs)
\fn bool QJsonObject::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 QJsonObject::iterator QJsonObject::iterator::operator++()
@ -1110,6 +1114,10 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\since 5.0
\brief The QJsonObject::const_iterator class provides an STL-style const iterator for QJsonObject.
\compares strong
\compareswith strong QJsonObject::iterator
\endcompareswith
QJsonObject::const_iterator allows you to iterate over a QJsonObject.
If you want to modify the QJsonObject as you iterate
over it, you must use QJsonObject::iterator instead. It is generally
@ -1218,50 +1226,48 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
*/
/*! \fn bool QJsonObject::const_iterator::operator==(const const_iterator &other) const
\fn bool QJsonObject::const_iterator::operator==(const iterator &other) const
/*! \fn bool QJsonObject::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 QJsonObject::const_iterator::operator!=(const const_iterator &other) const
\fn bool QJsonObject::const_iterator::operator!=(const iterator &other) const
/*! \fn bool QJsonObject::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 QJsonObject::const_iterator::operator<(const const_iterator& other) const
\fn bool QJsonObject::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 QJsonObject::const_iterator::operator<=(const const_iterator& other) const
\fn bool QJsonObject::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 QJsonObject::const_iterator::operator>(const const_iterator& other) const
\fn bool QJsonObject::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 QJsonObject::const_iterator::operator>=(const const_iterator& other) const
\fn bool QJsonObject::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 QJsonObject::const_iterator QJsonObject::const_iterator::operator++()

View File

@ -107,17 +107,17 @@ public:
inline const QJsonValueConstRef *operator->() const { return &item; }
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 &other) const
{ return item.d == other.item.d && item.index == other.item.index; }
inline bool operator!=(const iterator &other) const { return !(*this == other); }
inline bool operator!=(const iterator &other) const { return !operator==(other); }
bool operator<(const iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
bool operator<=(const iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
bool operator>(const iterator& other) const { return !(*this <= other); }
bool operator>=(const iterator& other) const { return !(*this < other); }
bool operator>(const iterator& other) const { return !operator<=(other); }
bool operator>=(const iterator& other) const { return !operator<(other); }
#endif
inline iterator &operator++() { ++item.index; return *this; }
inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
inline iterator &operator--() { --item.index; return *this; }
@ -129,15 +129,63 @@ public:
qsizetype operator-(iterator j) const { return item.index - j.item.index; }
public:
#if QT_CORE_REMOVED_SINCE(6, 8)
inline bool operator==(const const_iterator &other) const
{ return item.d == other.item.d && 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 !operator==(other); }
bool operator<(const const_iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
bool operator<=(const const_iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
bool operator>(const const_iterator& other) const { return !(*this <= other); }
bool operator>=(const const_iterator& other) const { return !(*this < other); }
bool operator>(const const_iterator& other) const { return operator<=(other); }
bool operator>=(const const_iterator& other) const { return operator<(other); }
#endif
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)
};
friend class iterator;
@ -171,17 +219,17 @@ public:
inline const QJsonValueConstRef operator*() const { return item; }
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 &other) const
{ return item.d == other.item.d && 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 !operator==(other); }
bool operator<(const const_iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
bool operator<=(const const_iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
bool operator>(const const_iterator& other) const { return !(*this <= other); }
bool operator>=(const const_iterator& other) const { return !(*this < other); }
bool operator>(const const_iterator& other) const { return !operator<=(other); }
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 r = *this; ++item.index; return r; }
inline const_iterator &operator--() { --item.index; return *this; }
@ -191,16 +239,43 @@ public:
inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
#if QT_CORE_REMOVED_SINCE(6, 8)
inline bool operator==(const iterator &other) const
{ return item.d == other.item.d && item.index == other.item.index; }
inline bool operator!=(const iterator &other) const { return !(*this == other); }
inline bool operator!=(const iterator &other) const { return !operator==(other); }
bool operator<(const iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
bool operator<=(const iterator& other) const
{ Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
bool operator>(const iterator& other) const { return !(*this <= other); }
bool operator>=(const iterator& other) const { return !(*this < other); }
bool operator>(const iterator& other) const { return !operator<=(other); }
bool operator>=(const iterator& other) const { return !operator<(other); }
#endif
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)
};
friend class const_iterator;

View File

@ -182,6 +182,8 @@ void tst_QtJson::compareCompiles()
QJsonArray::const_iterator>();
QTestPrivate::testEqualityOperatorsCompile<QJsonDocument>();
QTestPrivate::testEqualityOperatorsCompile<QJsonObject>();
QTestPrivate::testEqualityOperatorsCompile<QJsonObject::iterator>();
QTestPrivate::testEqualityOperatorsCompile<QJsonObject::const_iterator>();
QTestPrivate::testEqualityOperatorsCompile<QJsonValue>();
QTestPrivate::testEqualityOperatorsCompile<QJsonValueConstRef>();
QTestPrivate::testEqualityOperatorsCompile<QJsonValueRef>();
@ -192,6 +194,8 @@ void tst_QtJson::compareCompiles()
QTestPrivate::testEqualityOperatorsCompile<QJsonValueConstRef, QJsonValue>();
QTestPrivate::testEqualityOperatorsCompile<QJsonValueRef, QJsonValue>();
QTestPrivate::testEqualityOperatorsCompile<QJsonValueRef, QJsonValueConstRef>();
QTestPrivate::testEqualityOperatorsCompile<QJsonObject::iterator,
QJsonObject::const_iterator>();
}
void tst_QtJson::testValueSimple()
@ -1108,6 +1112,7 @@ void tst_QtJson::testObjectIteration()
for (QJsonObject::iterator it = object.begin(); it != object.end(); ++it) {
QJsonValue value = it.value();
QCOMPARE((double)it.key().toInt(), value.toDouble());
QT_TEST_EQUALITY_OPS(it, QJsonObject::iterator(), false);
}
{
@ -1120,6 +1125,7 @@ void tst_QtJson::testObjectIteration()
QCOMPARE(object.size(), 10);
QCOMPARE(object2.size(), 9);
QVERIFY(next == object2.begin());
QT_TEST_EQUALITY_OPS(next, object2.begin(), true);
double d = 1; // we erased the first item
for (auto it = object2.constBegin(); it != object2.constEnd(); ++it, d += 1) {
@ -1178,6 +1184,7 @@ void tst_QtJson::testObjectIteration()
{
QJsonObject::Iterator it = object.begin();
it += 5;
QT_TEST_ALL_COMPARISON_OPS(it, object.begin(), Qt::strong_ordering::greater);
QCOMPARE(QJsonValue(it.value()).toDouble(), 5.);
it -= 3;
QCOMPARE(QJsonValue(it.value()).toDouble(), 2.);
@ -1192,10 +1199,14 @@ void tst_QtJson::testObjectIteration()
it += 5;
QCOMPARE(QJsonValue(it.value()).toDouble(), 5.);
it -= 3;
QT_TEST_ALL_COMPARISON_OPS(object.constBegin(), it, Qt::strong_ordering::less);
QCOMPARE(QJsonValue(it.value()).toDouble(), 2.);
QJsonObject::ConstIterator it2 = it + 5;
QT_TEST_EQUALITY_OPS(it, it2, false);
QCOMPARE(QJsonValue(it2.value()).toDouble(), 7.);
it2 = it - 1;
QT_TEST_ALL_COMPARISON_OPS(it2, it, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(it2, it - 2, Qt::strong_ordering::greater);
QCOMPARE(QJsonValue(it2.value()).toDouble(), 1.);
}
@ -1204,6 +1215,17 @@ void tst_QtJson::testObjectIteration()
it = object.erase(it);
QCOMPARE(object.size() , 0);
QCOMPARE(it, object.end());
QT_TEST_ALL_COMPARISON_OPS(it, object.end(), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(it, object.constEnd(), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(it, object.begin(),
Qt::strong_ordering::equal); // because object is empty
QT_TEST_ALL_COMPARISON_OPS(it, object.constBegin(), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(QJsonObject::Iterator(),
QJsonObject::Iterator(), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(QJsonObject::ConstIterator(),
QJsonObject::Iterator(), Qt::strong_ordering::equal);
QT_TEST_ALL_COMPARISON_OPS(QJsonObject::ConstIterator(),
QJsonObject::ConstIterator(), Qt::strong_ordering::equal);
}
void tst_QtJson::testArrayIteration()