QSet: update (in)equality operators

Use comparison helper macros for the (in)equality operators.
Note that we cannot use the "public" macros, because we need to provide
attributes for the comparison of the same type, so use the _HELPER
macro instead.

Convert the operators to hidden friends. We do not need to use
QT_CORE_REMOVED_SINCE in this case, because the class is a template,
so the operators were not exported.

Task-number: QTBUG-120305
Change-Id: I575ab953c4693b6550cd764919671df8dfa5624a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ivan Solovev 2024-08-01 14:10:08 +02:00
parent 80bc015244
commit f9f3bf79dc
4 changed files with 40 additions and 12 deletions

View File

@ -37,15 +37,18 @@ public:
inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); } inline void swap(QSet<T> &other) noexcept { q_hash.swap(other.q_hash); }
#ifndef Q_QDOC #ifndef Q_QDOC
template <typename U = T> private:
QTypeTraits::compare_eq_result_container<QSet, U> operator==(const QSet<T> &other) const template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>
{ return q_hash == other.q_hash; } friend bool comparesEqual(const QSet &lhs, const QSet &rhs) noexcept
template <typename U = T> {
QTypeTraits::compare_eq_result_container<QSet, U> operator!=(const QSet<T> &other) const return lhs.q_hash == rhs.q_hash;
{ return q_hash != other.q_hash; } }
QT_DECLARE_EQUALITY_OPERATORS_HELPER(QSet, QSet, /* non-constexpr */, noexcept,
template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>)
public:
#else #else
bool operator==(const QSet &other) const; friend bool operator==(const QSet &lhs, const QSet &rhs) noexcept;
bool operator!=(const QSet &other) const; friend bool operator!=(const QSet &lhs, const QSet &rhs) noexcept;
#endif #endif
inline qsizetype size() const { return q_hash.size(); } inline qsizetype size() const { return q_hash.size(); }

View File

@ -5,6 +5,7 @@
\class QSet \class QSet
\inmodule QtCore \inmodule QtCore
\brief The QSet class is a template class that provides a hash-table-based set. \brief The QSet class is a template class that provides a hash-table-based set.
\compares equality
\ingroup tools \ingroup tools
\ingroup shared \ingroup shared
@ -105,9 +106,9 @@
*/ */
/*! /*!
\fn template <class T> bool QSet<T>::operator==(const QSet<T> &other) const \fn template <class T> bool QSet<T>::operator==(const QSet<T> lhs, const QSet<T> &rhs)
Returns \c true if the \a other set is equal to this set; otherwise Returns \c true if the \a lhs set is equal to the \a rhs set; otherwise
returns \c false. returns \c false.
Two sets are considered equal if they contain the same elements. Two sets are considered equal if they contain the same elements.
@ -118,9 +119,9 @@
*/ */
/*! /*!
\fn template <class T> bool QSet<T>::operator!=(const QSet<T> &other) const \fn template <class T> bool QSet<T>::operator!=(const QSet<T> lhs, const QSet<T> &rhs)
Returns \c true if the \a other set is not equal to this set; otherwise Returns \c true if the \a lhs set is not equal to the \a rhs set; otherwise
returns \c false. returns \c false.
Two sets are considered equal if they contain the same elements. Two sets are considered equal if they contain the same elements.

View File

@ -14,6 +14,8 @@ endif()
qt_internal_add_test(tst_qset qt_internal_add_test(tst_qset
SOURCES SOURCES
tst_qset.cpp tst_qset.cpp
LIBRARIES
Qt::TestPrivate
) )
qt_internal_undefine_global_definition(tst_qset QT_NO_JAVA_STYLE_ITERATORS) qt_internal_undefine_global_definition(tst_qset QT_NO_JAVA_STYLE_ITERATORS)

View File

@ -5,6 +5,8 @@
#include <qset.h> #include <qset.h>
#include <qdebug.h> #include <qdebug.h>
#include <private/qcomparisontesthelper_p.h>
int toNumber(const QString &str) int toNumber(const QString &str)
{ {
int res = 0; int res = 0;
@ -18,6 +20,7 @@ class tst_QSet : public QObject
Q_OBJECT Q_OBJECT
private slots: private slots:
void comparisonCompiles();
void operator_eq(); void operator_eq();
void swap(); void swap();
void size(); void size();
@ -56,50 +59,66 @@ struct IdentityTracker {
inline size_t qHash(IdentityTracker key) { return qHash(key.value); } inline size_t qHash(IdentityTracker key) { return qHash(key.value); }
inline bool operator==(IdentityTracker lhs, IdentityTracker rhs) { return lhs.value == rhs.value; } inline bool operator==(IdentityTracker lhs, IdentityTracker rhs) { return lhs.value == rhs.value; }
void tst_QSet::comparisonCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QSet<int>>();
QTestPrivate::testEqualityOperatorsCompile<QSet<QString>>();
}
void tst_QSet::operator_eq() void tst_QSet::operator_eq()
{ {
{ {
QSet<int> set1, set2; QSet<int> set1, set2;
QVERIFY(set1 == set2); QVERIFY(set1 == set2);
QVERIFY(!(set1 != set2)); QVERIFY(!(set1 != set2));
QT_TEST_EQUALITY_OPS(set1, set2, true);
set1.insert(1); set1.insert(1);
QVERIFY(set1 != set2); QVERIFY(set1 != set2);
QVERIFY(!(set1 == set2)); QVERIFY(!(set1 == set2));
QT_TEST_EQUALITY_OPS(set1, set2, false);
set2.insert(1); set2.insert(1);
QVERIFY(set1 == set2); QVERIFY(set1 == set2);
QVERIFY(!(set1 != set2)); QVERIFY(!(set1 != set2));
QT_TEST_EQUALITY_OPS(set1, set2, true);
set2.insert(1); set2.insert(1);
QVERIFY(set1 == set2); QVERIFY(set1 == set2);
QVERIFY(!(set1 != set2)); QVERIFY(!(set1 != set2));
QT_TEST_EQUALITY_OPS(set1, set2, true);
set1.insert(2); set1.insert(2);
QVERIFY(set1 != set2); QVERIFY(set1 != set2);
QVERIFY(!(set1 == set2)); QVERIFY(!(set1 == set2));
QT_TEST_EQUALITY_OPS(set1, set2, false);
} }
{ {
QSet<QString> set1, set2; QSet<QString> set1, set2;
QVERIFY(set1 == set2); QVERIFY(set1 == set2);
QVERIFY(!(set1 != set2)); QVERIFY(!(set1 != set2));
QT_TEST_EQUALITY_OPS(set1, set2, true);
set1.insert("one"); set1.insert("one");
QVERIFY(set1 != set2); QVERIFY(set1 != set2);
QVERIFY(!(set1 == set2)); QVERIFY(!(set1 == set2));
QT_TEST_EQUALITY_OPS(set1, set2, false);
set2.insert("one"); set2.insert("one");
QVERIFY(set1 == set2); QVERIFY(set1 == set2);
QVERIFY(!(set1 != set2)); QVERIFY(!(set1 != set2));
QT_TEST_EQUALITY_OPS(set1, set2, true);
set2.insert("one"); set2.insert("one");
QVERIFY(set1 == set2); QVERIFY(set1 == set2);
QVERIFY(!(set1 != set2)); QVERIFY(!(set1 != set2));
QT_TEST_EQUALITY_OPS(set1, set2, true);
set1.insert("two"); set1.insert("two");
QVERIFY(set1 != set2); QVERIFY(set1 != set2);
QVERIFY(!(set1 == set2)); QVERIFY(!(set1 == set2));
QT_TEST_EQUALITY_OPS(set1, set2, false);
} }
{ {
@ -111,6 +130,7 @@ void tst_QSet::operator_eq()
QVERIFY(a != b); QVERIFY(a != b);
QVERIFY(!(a == b)); QVERIFY(!(a == b));
QT_TEST_EQUALITY_OPS(a, b, false);
} }
{ {
@ -118,9 +138,11 @@ void tst_QSet::operator_eq()
s1.reserve(100); s1.reserve(100);
s2.reserve(4); s2.reserve(4);
QVERIFY(s1 == s2); QVERIFY(s1 == s2);
QT_TEST_EQUALITY_OPS(s1, s2, true);
s1 << 100 << 200 << 300 << 400; s1 << 100 << 200 << 300 << 400;
s2 << 400 << 300 << 200 << 100; s2 << 400 << 300 << 200 << 100;
QVERIFY(s1 == s2); QVERIFY(s1 == s2);
QT_TEST_EQUALITY_OPS(s1, s2, true);
} }
} }