From 85e4e5bacf0030573359be8de2ea984d541fdbd5 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 22 Oct 2020 09:13:24 +0200 Subject: [PATCH] Make QSharedPointer comparisons hidden friends Doesn't touch qsharedpointer.h which already seems outdated and needs a general overhaul. Change-Id: I051cdeb1fe03a7ef16e333a483bb68e2fada3c25 Reviewed-by: Volker Hilsheimer --- src/corelib/tools/qshareddata.cpp | 34 +++-- src/corelib/tools/qsharedpointer_impl.h | 158 +++++++----------------- 2 files changed, 70 insertions(+), 122 deletions(-) diff --git a/src/corelib/tools/qshareddata.cpp b/src/corelib/tools/qshareddata.cpp index 6db2a9adb9..16c10a58ac 100644 --- a/src/corelib/tools/qshareddata.cpp +++ b/src/corelib/tools/qshareddata.cpp @@ -357,13 +357,23 @@ QT_BEGIN_NAMESPACE \since 5.2 */ -/*! \fn template bool QSharedDataPointer::operator==(const QSharedDataPointer& other) const - Returns \c true if \a other and \e this have the same \e{d pointer}. +/*! \fn template bool QSharedDataPointer::operator==(const QSharedDataPointer& lhs, const QSharedDataPointer& rhs) + Returns \c true if \a lhs and \a rhs have the same \e{d pointer}. This function does \e not call detach(). */ -/*! \fn template bool QSharedDataPointer::operator!=(const QSharedDataPointer& other) const - Returns \c true if \a other and \e this do \e not have the same +/*! \fn template bool QSharedDataPointer::operator!=(const QSharedDataPointer& lhs, const QSharedDataPointer& rhs) + Returns \c true if \a lhs and \a rhs do \e not have the same + \e{d pointer}. This function does \e not call detach(). +*/ + +/*! \fn template bool QSharedDataPointer::operator==(const T *ptr, const QSharedDataPointer& rhs) + Returns \c true if the \e{d pointer} of \a rhs is \a ptr. + This function does \e not call detach(). +*/ + +/*! \fn template bool QSharedDataPointer::operator!=(const T *ptr, const QSharedDataPointer& rhs) + Returns \c true if the \e{d pointer} of \a rhs is \e not \a ptr. \e{d pointer}. This function does \e not call detach(). */ @@ -530,8 +540,8 @@ QT_BEGIN_NAMESPACE the explicitly shared data pointer in \a other. */ -/*! \fn template bool QExplicitlySharedDataPointer::operator==(const QExplicitlySharedDataPointer& other) const - Returns \c true if \a other and \e this have the same \e{d pointer}. +/*! \fn template bool QExplicitlySharedDataPointer::operator==(const QExplicitlySharedDataPointer& lhs, const QExplicitlySharedDataPointer& rhs) + Returns \c true if \a lhs and \a rhs have the same \e{d pointer}. */ /*! @@ -542,17 +552,17 @@ QT_BEGIN_NAMESPACE \since 5.2 */ -/*! \fn template bool QExplicitlySharedDataPointer::operator==(const T* ptr) const - Returns \c true if the \e{d pointer} of \e this is \a ptr. +/*! \fn template bool QExplicitlySharedDataPointer::operator==(const T* ptr, const QExplicitlySharedDataPointer& rhs) + Returns \c true if the \e{d pointer} of \a rhs is \a ptr. */ -/*! \fn template bool QExplicitlySharedDataPointer::operator!=(const QExplicitlySharedDataPointer& other) const - Returns \c true if \a other and \e this do \e not have the same +/*! \fn template bool QExplicitlySharedDataPointer::operator!=(const QExplicitlySharedDataPointer& lhs, const QExplicitlySharedDataPointer& rhs) + Returns \c true if \a lhs and \a rhs do \e not have the same \e{d pointer}. */ -/*! \fn template bool QExplicitlySharedDataPointer::operator!=(const T* ptr) const - Returns \c true if the \e{d pointer} of \e this is \e not \a ptr. +/*! \fn template bool QExplicitlySharedDataPointer::operator!=(const T* ptr, const QExplicitlySharedDataPointer& rhs) + Returns \c true if the \e{d pointer} of \a rhs is \e not \a ptr. */ /*! \fn template QExplicitlySharedDataPointer::QExplicitlySharedDataPointer() diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 2e7ec8103c..affd85d88d 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -437,6 +437,27 @@ public: return result; } +#define DECLARE_COMPARE_SET(T1, A1, T2, A2) \ + friend bool operator==(T1, T2) noexcept \ + { return A1 == A2; } \ + friend bool operator!=(T1, T2) noexcept \ + { return A1 != A2; } + +#define DECLARE_TEMPLATE_COMPARE_SET(T1, A1, T2, A2) \ + template \ + friend bool operator==(T1, T2) noexcept \ + { return A1 == A2; } \ + template \ + friend bool operator!=(T1, T2) noexcept \ + { return A1 != A2; } + + DECLARE_TEMPLATE_COMPARE_SET(const QSharedPointer &p1, p1.data(), const QSharedPointer &p2, p2.data()) + DECLARE_TEMPLATE_COMPARE_SET(const QSharedPointer &p1, p1.data(), X *ptr, ptr) + DECLARE_TEMPLATE_COMPARE_SET(X *ptr, ptr, const QSharedPointer &p2, p2.data()) + DECLARE_COMPARE_SET(const QSharedPointer &p1, p1.data(), std::nullptr_t, nullptr) + DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QSharedPointer &p2, p2.data()) +#undef DECLARE_TEMPLATE_COMPARE_SET + private: explicit QSharedPointer(Qt::Initialization) {} @@ -593,14 +614,6 @@ public: return *this; } - template - bool operator==(const QWeakPointer &o) const noexcept - { return d == o.d && value == static_cast(o.value); } - - template - bool operator!=(const QWeakPointer &o) const noexcept - { return !(*this == o); } - template = true> inline QWeakPointer(const QSharedPointer &o) : d(nullptr), value(nullptr) { *this = o; } @@ -612,14 +625,6 @@ public: return *this; } - template - bool operator==(const QSharedPointer &o) const noexcept - { return d == o.d; } - - template - bool operator!=(const QSharedPointer &o) const noexcept - { return !(*this == o); } - inline void clear() { *this = QWeakPointer(); } inline QSharedPointer toStrongRef() const { return QSharedPointer(*this); } @@ -630,6 +635,33 @@ public: inline T *operator->() const { return data(); } #endif + template + bool operator==(const QWeakPointer &o) const noexcept + { return d == o.d && value == static_cast(o.value); } + + template + bool operator!=(const QWeakPointer &o) const noexcept + { return !(*this == o); } + + template + bool operator==(const QSharedPointer &o) const noexcept + { return d == o.d; } + + template + bool operator!=(const QSharedPointer &o) const noexcept + { return !(*this == o); } + + template + friend bool operator==(const QSharedPointer &p1, const QWeakPointer &p2) noexcept + { return p2 == p1; } + template + friend bool operator!=(const QSharedPointer &p1, const QWeakPointer &p2) noexcept + { return p2 != p1; } + + DECLARE_COMPARE_SET(const QWeakPointer &p1, p1.d, std::nullptr_t, nullptr) + DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QWeakPointer &p2, p2.data()) +#undef DECLARE_COMPARE_SET + private: friend struct QtPrivate::EnableInternalData; #if defined(Q_NO_TEMPLATE_FRIENDS) @@ -709,100 +741,6 @@ public: mutable QWeakPointer weakPointer; }; -// -// operator== and operator!= -// -template -bool operator==(const QSharedPointer &ptr1, const QSharedPointer &ptr2) noexcept -{ - return ptr1.data() == ptr2.data(); -} -template -bool operator!=(const QSharedPointer &ptr1, const QSharedPointer &ptr2) noexcept -{ - return ptr1.data() != ptr2.data(); -} - -template -bool operator==(const QSharedPointer &ptr1, const X *ptr2) noexcept -{ - return ptr1.data() == ptr2; -} -template -bool operator==(const T *ptr1, const QSharedPointer &ptr2) noexcept -{ - return ptr1 == ptr2.data(); -} -template -bool operator!=(const QSharedPointer &ptr1, const X *ptr2) noexcept -{ - return !(ptr1 == ptr2); -} -template -bool operator!=(const T *ptr1, const QSharedPointer &ptr2) noexcept -{ - return !(ptr2 == ptr1); -} - -template -bool operator==(const QSharedPointer &ptr1, const QWeakPointer &ptr2) noexcept -{ - return ptr2 == ptr1; -} -template -bool operator!=(const QSharedPointer &ptr1, const QWeakPointer &ptr2) noexcept -{ - return ptr2 != ptr1; -} - -template -inline bool operator==(const QSharedPointer &lhs, std::nullptr_t) noexcept -{ - return lhs.isNull(); -} - -template -inline bool operator!=(const QSharedPointer &lhs, std::nullptr_t) noexcept -{ - return !lhs.isNull(); -} - -template -inline bool operator==(std::nullptr_t, const QSharedPointer &rhs) noexcept -{ - return rhs.isNull(); -} - -template -inline bool operator!=(std::nullptr_t, const QSharedPointer &rhs) noexcept -{ - return !rhs.isNull(); -} - -template -inline bool operator==(const QWeakPointer &lhs, std::nullptr_t) noexcept -{ - return lhs.isNull(); -} - -template -inline bool operator!=(const QWeakPointer &lhs, std::nullptr_t) noexcept -{ - return !lhs.isNull(); -} - -template -inline bool operator==(std::nullptr_t, const QWeakPointer &rhs) noexcept -{ - return rhs.isNull(); -} - -template -inline bool operator!=(std::nullptr_t, const QWeakPointer &rhs) noexcept -{ - return !rhs.isNull(); -} - // // operator- //