QCborMap iterators: use new comparison helper macros

New comparison macros are used for following classes:
- QCborMap::Iterator
- QCborMap::ConstIterator

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: I71b6febaf3f31ea7ba668d91c375b0a7b6827d21
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Tatiana Borisova 2024-03-22 16:35:02 +01:00
parent de2a467a77
commit 92cc21b389
3 changed files with 168 additions and 70 deletions

View File

@ -1238,6 +1238,10 @@ void QCborMap::detach(qsizetype reserved)
\brief The QCborMap::Iterator class provides an STL-style non-const iterator for QCborMap.
\compares strong
\compareswith strong ConstIterator
\endcompareswith
QCborMap::Iterator allows you to iterate over a QCborMap and to modify the
value (but not the key) stored under a particular key. If you want to
iterate over a const QCborMap, you should use QCborMap::ConstIterator. It
@ -1359,56 +1363,56 @@ void QCborMap::detach(qsizetype reserved)
*/
/*!
\fn bool QCborMap::Iterator::operator==(const Iterator &other) const
\fn bool QCborMap::Iterator::operator==(const ConstIterator &other) const
\fn bool QCborMap::Iterator::operator==(const Iterator &lhs, const Iterator &rhs)
\fn bool QCborMap::Iterator::operator==(const Iterator &lhs, const ConstIterator &rhs)
Returns \c true if \a other points to the same entry in the map as this
Returns \c true if \a lhs points to the same entry in the map as \a rhs
iterator; otherwise returns \c false.
\sa operator!=()
*/
/*!
\fn bool QCborMap::Iterator::operator!=(const Iterator &other) const
\fn bool QCborMap::Iterator::operator!=(const ConstIterator &other) const
\fn bool QCborMap::Iterator::operator!=(const Iterator &lhs, const Iterator &rhs)
\fn bool QCborMap::Iterator::operator!=(const Iterator &lhs, const ConstIterator &rhs)
Returns \c true if \a other points to a different entry in the map than
this iterator; otherwise returns \c false.
Returns \c true if \a lhs points to a different entry in the map than
\a rhs iterator; otherwise returns \c false.
\sa operator==()
*/
/*!
\fn bool QCborMap::Iterator::operator<(const Iterator& other) const
\fn bool QCborMap::Iterator::operator<(const ConstIterator& other) const
\fn bool QCborMap::Iterator::operator<(const Iterator &lhs, const Iterator &rhs)
\fn bool QCborMap::Iterator::operator<(const Iterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs before the entry pointed to by the \a other iterator.
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs before the entry pointed to by the \a rhs iterator.
*/
/*!
\fn bool QCborMap::Iterator::operator<=(const Iterator& other) const
\fn bool QCborMap::Iterator::operator<=(const ConstIterator& other) const
\fn bool QCborMap::Iterator::operator<=(const Iterator &lhs, const Iterator &rhs)
\fn bool QCborMap::Iterator::operator<=(const Iterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs before or is the same entry as is pointed to by the \a other
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs before or is the same entry as is pointed to by the \a rhs
iterator.
*/
/*!
\fn bool QCborMap::Iterator::operator>(const Iterator& other) const
\fn bool QCborMap::Iterator::operator>(const ConstIterator& other) const
\fn bool QCborMap::Iterator::operator>(const Iterator &lhs, const Iterator &rhs)
\fn bool QCborMap::Iterator::operator>(const Iterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs after the entry pointed to by the \a other iterator.
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs after the entry pointed to by the \a rhs iterator.
*/
/*!
\fn bool QCborMap::Iterator::operator>=(const Iterator& other) const
\fn bool QCborMap::Iterator::operator>=(const ConstIterator& other) const
\fn bool QCborMap::Iterator::operator>=(const Iterator &lhs, const Iterator &rhs)
\fn bool QCborMap::Iterator::operator>=(const Iterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs after or is the same entry as is pointed to by the \a other
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs after or is the same entry as is pointed to by the \a rhs
iterator.
*/
@ -1504,6 +1508,10 @@ void QCborMap::detach(qsizetype reserved)
\brief The QCborMap::ConstIterator class provides an STL-style const iterator for QCborMap.
\compares strong
\compareswith strong Iterator
\endcompareswith
QCborMap::ConstIterator allows you to iterate over a QCborMap. If you want
to modify the QCborMap as you iterate over it, you must use
QCborMap::Iterator instead. It is generally good practice to use
@ -1604,56 +1612,50 @@ void QCborMap::detach(qsizetype reserved)
*/
/*!
\fn bool QCborMap::ConstIterator::operator==(const ConstIterator &other) const
\fn bool QCborMap::ConstIterator::operator==(const Iterator &other) const
\fn bool QCborMap::ConstIterator::operator==(const ConstIterator &lhs, const ConstIterator &rhs)
Returns \c true if \a other points to the same entry in the map as this
Returns \c true if \a lhs points to the same entry in the map as \a rhs
iterator; otherwise returns \c false.
\sa operator!=()
*/
/*!
\fn bool QCborMap::ConstIterator::operator!=(const ConstIterator &other) const
\fn bool QCborMap::ConstIterator::operator!=(const Iterator &other) const
\fn bool QCborMap::ConstIterator::operator!=(const ConstIterator &lhs, const ConstIterator &rhs)
Returns \c true if \a other points to a different entry in the map than
this iterator; otherwise returns \c false.
Returns \c true if \a lhs points to a different entry in the map than
\a rhs iterator; otherwise returns \c false.
\sa operator==()
*/
/*!
\fn bool QCborMap::ConstIterator::operator<(const Iterator &other) const
\fn bool QCborMap::ConstIterator::operator<(const ConstIterator &other) const
\fn bool QCborMap::ConstIterator::operator<(const ConstIterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs before the entry pointed to by the \a other iterator.
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs before the entry pointed to by the \a rhs iterator.
*/
/*!
\fn bool QCborMap::ConstIterator::operator<=(const Iterator &other) const
\fn bool QCborMap::ConstIterator::operator<=(const ConstIterator &other) const
\fn bool QCborMap::ConstIterator::operator<=(const ConstIterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs before or is the same entry as is pointed to by the \a other
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs before or is the same entry as is pointed to by the \a rhs
iterator.
*/
/*!
\fn bool QCborMap::ConstIterator::operator>(const Iterator &other) const
\fn bool QCborMap::ConstIterator::operator>(const ConstIterator &other) const
\fn bool QCborMap::ConstIterator::operator>(const ConstIterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs after the entry pointed to by the \a other iterator.
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs after the entry pointed to by the \a rhs iterator.
*/
/*!
\fn bool QCborMap::ConstIterator::operator>=(const Iterator &other) const
\fn bool QCborMap::ConstIterator::operator>=(const ConstIterator &other) const
\fn bool QCborMap::ConstIterator::operator>=(const ConstIterator &lhs, const ConstIterator &rhs)
Returns \c true if the entry in the map pointed to by this iterator
occurs after or is the same entry as is pointed to by the \a other
Returns \c true if the entry in the map pointed to by \a lhs iterator
occurs after or is the same entry as is pointed to by the \a rhs
iterator.
*/

View File

@ -61,18 +61,20 @@ public:
key() const { return QCborValueRef(item.d, item.i - 1); }
QCborValueRef value() const { return item; }
#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const Iterator &o) const { return !(*this == o); }
bool operator!=(const Iterator &o) const { return !operator==(o); }
bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const ConstIterator &o) const { return !(*this == o); }
bool operator!=(const ConstIterator &o) const { return !operator==(o); }
bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
#endif
Iterator &operator++() { item.i += 2; return *this; }
Iterator operator++(int) { Iterator n = *this; item.i += 2; return n; }
Iterator &operator--() { item.i -= 2; return *this; }
@ -82,6 +84,54 @@ public:
Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + 2 * j }); }
Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - 2 * j }); }
qsizetype operator-(Iterator j) const { return (item.i - j.item.i) / 2; }
private:
// Helper functions
static bool comparesEqual_helper(const Iterator &lhs, const Iterator &rhs) noexcept
{
return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i;
}
static bool comparesEqual_helper(const Iterator &lhs, const ConstIterator &rhs) noexcept
{
return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i;
}
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.i, rhs.item.i);
}
static Qt::strong_ordering compareThreeWay_helper(const Iterator &lhs,
const ConstIterator &rhs) noexcept
{
Q_ASSERT(lhs.item.d == rhs.item.d);
return Qt::compareThreeWay(lhs.item.i, rhs.item.i);
}
// 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 ConstIterator &rhs) noexcept
{
return comparesEqual_helper(lhs, rhs);
}
friend Qt::strong_ordering compareThreeWay(const Iterator &lhs,
const ConstIterator &rhs) noexcept
{
return compareThreeWay_helper(lhs, rhs);
}
Q_DECLARE_STRONGLY_ORDERED(Iterator, ConstIterator)
};
class ConstIterator {
@ -121,18 +171,20 @@ public:
key() const { return QCborValueRef(item.d, item.i - 1); }
QCborValueConstRef value() const { return item; }
#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const Iterator &o) const { return !(*this == o); }
bool operator!=(const Iterator &o) const { return !operator==(o); }
bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const ConstIterator &o) const { return !(*this == o); }
bool operator!=(const ConstIterator &o) const { return !operator==(o); }
bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
#endif
ConstIterator &operator++() { item.i += 2; return *this; }
ConstIterator operator++(int) { ConstIterator n = *this; item.i += 2; return n; }
ConstIterator &operator--() { item.i -= 2; return *this; }
@ -142,6 +194,31 @@ public:
ConstIterator operator+(qsizetype j) const { return ConstIterator{ item.d, item.i + 2 * j }; }
ConstIterator operator-(qsizetype j) const { return ConstIterator{ item.d, item.i - 2 * j }; }
qsizetype operator-(ConstIterator j) const { return (item.i - j.item.i) / 2; }
private:
// Helper functions
static bool comparesEqual_helper(const ConstIterator &lhs,
const ConstIterator &rhs) noexcept
{
return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i;
}
static Qt::strong_ordering compareThreeWay_helper(const ConstIterator &lhs,
const ConstIterator &rhs) noexcept
{
Q_ASSERT(lhs.item.d == rhs.item.d);
return Qt::compareThreeWay(lhs.item.i, rhs.item.i);
}
// Compare friends
friend bool comparesEqual(const ConstIterator &lhs, const ConstIterator &rhs) noexcept
{
return comparesEqual_helper(lhs, rhs);
}
friend Qt::strong_ordering compareThreeWay(const ConstIterator &lhs,
const ConstIterator &rhs) noexcept
{
return compareThreeWay_helper(lhs, rhs);
}
Q_DECLARE_STRONGLY_ORDERED(ConstIterator)
};
QCborMap() noexcept;
@ -169,25 +246,25 @@ public:
QList<QCborValue> keys() const;
QCborValue value(qint64 key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
QCborValue value(QLatin1StringView key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
QCborValue value(const QString & key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
QCborValue value(const QCborValue &key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
template<size_t N> QT_ASCII_CAST_WARN const QCborValue value(const char (&key)[N]) const
{ return value(QString::fromUtf8(key, N - 1)); }
#endif
const QCborValue operator[](qint64 key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
const QCborValue operator[](QLatin1StringView key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
const QCborValue operator[](const QString & key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
const QCborValue operator[](const QCborValue &key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
{ const_iterator it = find(key); return comparesEqual(it, end()) ? QCborValue() : it.value(); }
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
template<size_t N> QT_ASCII_CAST_WARN const QCborValue operator[](const char (&key)[N]) const
{ return operator[](QString::fromUtf8(key, N - 1)); }
@ -198,29 +275,29 @@ public:
QCborValueRef operator[](const QCborValue &key);
QCborValue take(qint64 key)
{ const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) return extract(it); return QCborValue(); }
QCborValue take(QLatin1StringView key)
{ const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) return extract(it); return QCborValue(); }
QCborValue take(const QString &key)
{ const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) return extract(it); return QCborValue(); }
QCborValue take(const QCborValue &key)
{ const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) return extract(it); return QCborValue(); }
void remove(qint64 key)
{ const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) erase(it); }
void remove(QLatin1StringView key)
{ const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) erase(it); }
void remove(const QString & key)
{ const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) erase(it); }
void remove(const QCborValue &key)
{ const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
{ const_iterator it = constFind(key); if (!comparesEqual(it, constEnd())) erase(it); }
bool contains(qint64 key) const
{ const_iterator it = find(key); return it != end(); }
{ const_iterator it = find(key); return !comparesEqual(it, end()); }
bool contains(QLatin1StringView key) const
{ const_iterator it = find(key); return it != end(); }
{ const_iterator it = find(key); return !comparesEqual(it, end()); }
bool contains(const QString & key) const
{ const_iterator it = find(key); return it != end(); }
{ const_iterator it = find(key); return !comparesEqual(it, end()); }
bool contains(const QCborValue &key) const
{ const_iterator it = find(key); return it != end(); }
{ const_iterator it = find(key); return !comparesEqual(it, end()); }
int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION;
#if QT_CORE_REMOVED_SINCE(6, 8)

View File

@ -461,6 +461,8 @@ void tst_QCborValue::compareCompiles()
QTestPrivate::testAllComparisonOperatorsCompile<QCborArray::Iterator>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborArray::ConstIterator>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborMap>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborMap::Iterator>();
QTestPrivate::testAllComparisonOperatorsCompile<QCborMap::ConstIterator>();
// QCborValue, Ref and ConstRef
QTestPrivate::testAllComparisonOperatorsCompile<QCborValueRef, QCborValueConstRef>();
@ -744,6 +746,13 @@ void tst_QCborValue::mapNonEmptyDetach()
{ QCborMap copy(m); auto it = m.find(QLatin1String("3")); QVERIFY(it == m.end()); }
{ QCborMap copy(m); auto it = m.find(QString("3")); QVERIFY(it == m.end()); }
{ QCborMap copy(m); auto it = m.find(QCborValue(3)); QVERIFY(it == m.end()); }
QT_TEST_EQUALITY_OPS(m.constBegin(), m.constEnd(), false);
QT_TEST_EQUALITY_OPS(m.begin(), m.end(), false);
QT_TEST_EQUALITY_OPS(m.constFind(3), m.constEnd(), true);
QT_TEST_EQUALITY_OPS(m.find(3), m.end(), true);
QT_TEST_EQUALITY_OPS(m.find(3), m.constEnd(), true);
QT_TEST_EQUALITY_OPS(m.constFind(3), m.end(), true);
}
void tst_QCborValue::arrayInitializerList()
@ -1179,8 +1188,18 @@ void tst_QCborValue::mapMutation()
m2 = m;
auto it = m.begin(); // detaches again
auto end = m.end();
auto it1 = m.constBegin(); // detaches again
auto end2 = m.constEnd();
QCOMPARE(end - it, 2);
QT_TEST_ALL_COMPARISON_OPS(it, it + 1, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(it, it1 + 1, Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(it, it - 1, Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(it, it1 - 1, Qt::strong_ordering::greater);
QT_TEST_EQUALITY_OPS(it, it1, true);
QCOMPARE(it + 2, end);
QT_TEST_EQUALITY_OPS(it + 2, end, true);
QT_TEST_EQUALITY_OPS(it + 2, end2, true);
QT_TEST_EQUALITY_OPS(it1 + 2, end2, true);
QT_TEST_EQUALITY_OPS(it.key(), QCborValue(42), true);
QT_TEST_EQUALITY_OPS(it.value(), QCborValue(2.5), true);
QT_TEST_EQUALITY_OPS((++it).value(), QCborValue(nullptr), true);