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); }
#ifndef Q_QDOC
template <typename U = T>
QTypeTraits::compare_eq_result_container<QSet, U> operator==(const QSet<T> &other) const
{ return q_hash == other.q_hash; }
template <typename U = T>
QTypeTraits::compare_eq_result_container<QSet, U> operator!=(const QSet<T> &other) const
{ return q_hash != other.q_hash; }
private:
template <typename U = T, QTypeTraits::compare_eq_result_container<QSet, U> = true>
friend bool comparesEqual(const QSet &lhs, const QSet &rhs) noexcept
{
return lhs.q_hash == rhs.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
bool operator==(const QSet &other) const;
bool operator!=(const QSet &other) const;
friend bool operator==(const QSet &lhs, const QSet &rhs) noexcept;
friend bool operator!=(const QSet &lhs, const QSet &rhs) noexcept;
#endif
inline qsizetype size() const { return q_hash.size(); }

View File

@ -5,6 +5,7 @@
\class QSet
\inmodule QtCore
\brief The QSet class is a template class that provides a hash-table-based set.
\compares equality
\ingroup tools
\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.
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.
Two sets are considered equal if they contain the same elements.

View File

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

View File

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