Make QMap and QMultiMap comparisons hidden friends

Task-number: QTBUG-87975
Change-Id: I3c84a188cdbb0d09e0e7c66588c7638c8a8328fd
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2020-10-29 11:38:09 +01:00
parent e53a3bd85a
commit 0cd134ed91
3 changed files with 35 additions and 67 deletions

View File

@ -272,18 +272,22 @@ public:
return {};
}
#ifdef Q_QDOC
template <typename AKey, typename AT>
friend bool operator==(const QMap<AKey, AT> &lhs, const QMap<AKey, AT> &rhs);
template <typename AKey, typename AT>
friend bool operator!=(const QMap<AKey, AT> &lhs, const QMap<AKey, AT> &rhs);
#else
// CHANGE: non-member equality comparison
template <typename AKey, typename AT>
friend QTypeTraits::compare_eq_result<AKey, AT> operator==(const QMap<AKey, AT> &lhs, const QMap<AKey, AT> &rhs);
template <typename AKey, typename AT>
friend QTypeTraits::compare_eq_result<AKey, AT> operator!=(const QMap<AKey, AT> &lhs, const QMap<AKey, AT> &rhs);
#endif
template <typename AKey = Key, typename AT = T> friend
QTypeTraits::compare_eq_result<AKey, AT> operator==(const QMap &lhs, const QMap &rhs)
{
if (lhs.d == rhs.d)
return true;
if (!lhs.d)
return rhs == lhs;
Q_ASSERT(lhs.d);
return rhs.d ? (lhs.d->m == rhs.d->m) : lhs.d->m.empty();
}
template <typename AKey = Key, typename AT = T> friend
QTypeTraits::compare_eq_result<AKey, AT> operator!=(const QMap &lhs, const QMap &rhs)
{
return !(lhs == rhs);
}
// TODO: add the other comparison operators; std::map has them.
size_type size() const { return d ? size_type(d->m.size()) : size_type(0); }
@ -734,24 +738,6 @@ public:
Q_DECLARE_ASSOCIATIVE_ITERATOR(Map)
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Map)
template <typename AKey, typename AT>
QTypeTraits::compare_eq_result<AKey, AT> operator==(const QMap<AKey, AT> &lhs, const QMap<AKey, AT> &rhs)
{
if (lhs.d == rhs.d)
return true;
if (!lhs.d)
return rhs == lhs;
Q_ASSERT(lhs.d);
return rhs.d ? (lhs.d->m == rhs.d->m) : lhs.d->m.empty();
}
template <typename AKey, typename AT>
QTypeTraits::compare_eq_result<AKey, AT> operator!=(const QMap<AKey, AT> &lhs, const QMap<AKey, AT> &rhs)
{
return !(lhs == rhs);
}
//
// QMultiMap
//
@ -848,19 +834,22 @@ public:
return {};
}
#ifdef Q_QDOC
template <typename AKey, typename AT>
friend bool operator==(const QMultiMap<AKey, AT> &lhs, const QMultiMap<AKey, AT> &rhs);
template <typename AKey, typename AT>
friend bool operator!=(const QMultiMap<AKey, AT> &lhs, const QMultiMap<AKey, AT> &rhs);
#else
// CHANGE: non-member equality comparison
template <typename AKey, typename AT>
friend QTypeTraits::compare_eq_result<AKey, AT> operator==(const QMultiMap<AKey, AT> &lhs, const QMultiMap<AKey, AT> &rhs);
template <typename AKey, typename AT>
friend QTypeTraits::compare_eq_result<AKey, AT> operator!=(const QMultiMap<AKey, AT> &lhs, const QMultiMap<AKey, AT> &rhs);
#endif
template <typename AKey = Key, typename AT = T> friend
QTypeTraits::compare_eq_result<AKey, AT> operator==(const QMultiMap &lhs, const QMultiMap &rhs)
{
if (lhs.d == rhs.d)
return true;
if (!lhs.d)
return rhs == lhs;
Q_ASSERT(lhs.d);
return rhs.d ? (lhs.d->m == rhs.d->m) : lhs.d->m.empty();
}
template <typename AKey = Key, typename AT = T> friend
QTypeTraits::compare_eq_result<AKey, AT> operator!=(const QMultiMap &lhs, const QMultiMap &rhs)
{
return !(lhs == rhs);
}
// TODO: add the other comparison operators; std::multimap has them.
size_type size() const { return d ? size_type(d->m.size()) : size_type(0); }
@ -1433,23 +1422,6 @@ public:
Q_DECLARE_ASSOCIATIVE_ITERATOR(MultiMap)
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(MultiMap)
template <typename AKey, typename AT>
QTypeTraits::compare_eq_result<AKey, AT> operator==(const QMultiMap<AKey, AT> &lhs, const QMultiMap<AKey, AT> &rhs)
{
if (lhs.d == rhs.d)
return true;
if (!lhs.d)
return rhs == lhs;
Q_ASSERT(lhs.d);
return rhs.d ? (lhs.d->m == rhs.d->m) : lhs.d->m.empty();
}
template <typename AKey, typename AT>
QTypeTraits::compare_eq_result<AKey, AT> operator!=(const QMultiMap<AKey, AT> &lhs, const QMultiMap<AKey, AT> &rhs)
{
return !(lhs == rhs);
}
template <typename Key, typename T>
QMultiMap<Key, T> operator+(const QMultiMap<Key, T> &lhs, const QMultiMap<Key, T> &rhs)
{

View File

@ -249,8 +249,7 @@
Returns an STL map equivalent to this QMap.
*/
/*! \fn template <class Key, class T> bool operator==(const QMap<Key, T> &lhs, const QMap<Key, T> &rhs)
\relates QMap
/*! \fn template <class Key, class T> bool QMap<Key, T>::operator==(const QMap<Key, T> &lhs, const QMap<Key, T> &rhs)
Returns \c true if \a lhs is equal to \a rhs; otherwise returns
false.
@ -264,8 +263,7 @@
\sa operator!=()
*/
/*! \fn template <class Key, class T> bool operator!=(const QMap<Key, T> &lhs, const QMap<Key, T> &rhs)
\relates QMap
/*! \fn template <class Key, class T> bool QMap<Key, T>::operator!=(const QMap<Key, T> &lhs, const QMap<Key, T> &rhs)
Returns \c true if \a lhs is not equal to \a rhs; otherwise returns
false.

View File

@ -259,8 +259,7 @@
fast and never fails.
*/
/*! \fn template<class Key, class T> bool operator==(const QMultiMap<Key, T> &lhs, const QMultiMap<Key, T> &rhs)
\relates QMultiMap
/*! \fn template<class Key, class T> bool QMultiMap<Key, T>::operator==(const QMultiMap<Key, T> &lhs, const QMultiMap<Key, T> &rhs)
Returns \c true if \a lhs is equal to \a rhs; otherwise returns
false.
@ -274,8 +273,7 @@
\sa operator!=()
*/
/*! \fn template<class Key, class T> bool operator!=(const QMultiMap<Key, T> &lhs, const QMultiMap<Key, T> &rhs)
\relates QMultiMap
/*! \fn template<class Key, class T> bool QMultiMap<Key, T>::operator!=(const QMultiMap<Key, T> &lhs, const QMultiMap<Key, T> &rhs)
Returns \c true if \a lhs is not equal to \a rhs; otherwise
returns \c false.