diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-29 11:38:09 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-29 20:25:49 +0100 |
commit | 0cd134ed91711aab662fbfea86faeb326d32a94f (patch) | |
tree | 5be919c7beaf8c21829d88d2ed99e24949eecdc7 | |
parent | e53a3bd85afb0e140aa4b89925345c33dc464bf5 (diff) |
Make QMap and QMultiMap comparisons hidden friends
Task-number: QTBUG-87975
Change-Id: I3c84a188cdbb0d09e0e7c66588c7638c8a8328fd
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r-- | src/corelib/tools/qmap.h | 90 | ||||
-rw-r--r-- | src/corelib/tools/qmap.qdoc | 6 | ||||
-rw-r--r-- | src/corelib/tools/qmultimap.qdoc | 6 |
3 files changed, 35 insertions, 67 deletions
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 0030c076ad..7b59688fa4 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -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) { diff --git a/src/corelib/tools/qmap.qdoc b/src/corelib/tools/qmap.qdoc index 79b3cc155d..ace09e7391 100644 --- a/src/corelib/tools/qmap.qdoc +++ b/src/corelib/tools/qmap.qdoc @@ -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. diff --git a/src/corelib/tools/qmultimap.qdoc b/src/corelib/tools/qmultimap.qdoc index d07383a366..487480a927 100644 --- a/src/corelib/tools/qmultimap.qdoc +++ b/src/corelib/tools/qmultimap.qdoc @@ -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. |