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 <volker.hilsheimer@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2020-10-22 09:13:24 +02:00 committed by Volker Hilsheimer
parent 074f71cfda
commit 85e4e5bacf
2 changed files with 70 additions and 122 deletions

View File

@ -357,13 +357,23 @@ QT_BEGIN_NAMESPACE
\since 5.2
*/
/*! \fn template <class T> bool QSharedDataPointer<T>::operator==(const QSharedDataPointer<T>& other) const
Returns \c true if \a other and \e this have the same \e{d pointer}.
/*! \fn template <class T> bool QSharedDataPointer<T>::operator==(const QSharedDataPointer<T>& lhs, const QSharedDataPointer<T>& 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 <class T> bool QSharedDataPointer<T>::operator!=(const QSharedDataPointer<T>& other) const
Returns \c true if \a other and \e this do \e not have the same
/*! \fn template <class T> bool QSharedDataPointer<T>::operator!=(const QSharedDataPointer<T>& lhs, const QSharedDataPointer<T>& 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 <class T> bool QSharedDataPointer<T>::operator==(const T *ptr, const QSharedDataPointer<T>& rhs)
Returns \c true if the \e{d pointer} of \a rhs is \a ptr.
This function does \e not call detach().
*/
/*! \fn template <class T> bool QSharedDataPointer<T>::operator!=(const T *ptr, const QSharedDataPointer<T>& 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 <class T> bool QExplicitlySharedDataPointer<T>::operator==(const QExplicitlySharedDataPointer<T>& other) const
Returns \c true if \a other and \e this have the same \e{d pointer}.
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator==(const QExplicitlySharedDataPointer<T>& lhs, const QExplicitlySharedDataPointer<T>& 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 <class T> bool QExplicitlySharedDataPointer<T>::operator==(const T* ptr) const
Returns \c true if the \e{d pointer} of \e this is \a ptr.
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator==(const T* ptr, const QExplicitlySharedDataPointer<T>& rhs)
Returns \c true if the \e{d pointer} of \a rhs is \a ptr.
*/
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!=(const QExplicitlySharedDataPointer<T>& other) const
Returns \c true if \a other and \e this do \e not have the same
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!=(const QExplicitlySharedDataPointer<T>& lhs, const QExplicitlySharedDataPointer<T>& rhs)
Returns \c true if \a lhs and \a rhs do \e not have the same
\e{d pointer}.
*/
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!=(const T* ptr) const
Returns \c true if the \e{d pointer} of \e this is \e not \a ptr.
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!=(const T* ptr, const QExplicitlySharedDataPointer<T>& rhs)
Returns \c true if the \e{d pointer} of \a rhs is \e not \a ptr.
*/
/*! \fn template <class T> QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer()

View File

@ -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 <typename X> \
friend bool operator==(T1, T2) noexcept \
{ return A1 == A2; } \
template <typename X> \
friend bool operator!=(T1, T2) noexcept \
{ return A1 != A2; }
DECLARE_TEMPLATE_COMPARE_SET(const QSharedPointer &p1, p1.data(), const QSharedPointer<X> &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 <class X>
bool operator==(const QWeakPointer<X> &o) const noexcept
{ return d == o.d && value == static_cast<const T *>(o.value); }
template <class X>
bool operator!=(const QWeakPointer<X> &o) const noexcept
{ return !(*this == o); }
template <class X, IfCompatible<X> = true>
inline QWeakPointer(const QSharedPointer<X> &o) : d(nullptr), value(nullptr)
{ *this = o; }
@ -612,14 +625,6 @@ public:
return *this;
}
template <class X>
bool operator==(const QSharedPointer<X> &o) const noexcept
{ return d == o.d; }
template <class X>
bool operator!=(const QSharedPointer<X> &o) const noexcept
{ return !(*this == o); }
inline void clear() { *this = QWeakPointer(); }
inline QSharedPointer<T> toStrongRef() const { return QSharedPointer<T>(*this); }
@ -630,6 +635,33 @@ public:
inline T *operator->() const { return data(); }
#endif
template <class X>
bool operator==(const QWeakPointer<X> &o) const noexcept
{ return d == o.d && value == static_cast<const T *>(o.value); }
template <class X>
bool operator!=(const QWeakPointer<X> &o) const noexcept
{ return !(*this == o); }
template <class X>
bool operator==(const QSharedPointer<X> &o) const noexcept
{ return d == o.d; }
template <class X>
bool operator!=(const QSharedPointer<X> &o) const noexcept
{ return !(*this == o); }
template <typename X>
friend bool operator==(const QSharedPointer<X> &p1, const QWeakPointer &p2) noexcept
{ return p2 == p1; }
template <typename X>
friend bool operator!=(const QSharedPointer<X> &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<T> weakPointer;
};
//
// operator== and operator!=
//
template <class T, class X>
bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) noexcept
{
return ptr1.data() == ptr2.data();
}
template <class T, class X>
bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2) noexcept
{
return ptr1.data() != ptr2.data();
}
template <class T, class X>
bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2) noexcept
{
return ptr1.data() == ptr2;
}
template <class T, class X>
bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2) noexcept
{
return ptr1 == ptr2.data();
}
template <class T, class X>
bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2) noexcept
{
return !(ptr1 == ptr2);
}
template <class T, class X>
bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2) noexcept
{
return !(ptr2 == ptr1);
}
template <class T, class X>
bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) noexcept
{
return ptr2 == ptr1;
}
template <class T, class X>
bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) noexcept
{
return ptr2 != ptr1;
}
template<class T>
inline bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t) noexcept
{
return lhs.isNull();
}
template<class T>
inline bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t) noexcept
{
return !lhs.isNull();
}
template<class T>
inline bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs) noexcept
{
return rhs.isNull();
}
template<class T>
inline bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs) noexcept
{
return !rhs.isNull();
}
template<class T>
inline bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t) noexcept
{
return lhs.isNull();
}
template<class T>
inline bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t) noexcept
{
return !lhs.isNull();
}
template<class T>
inline bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs) noexcept
{
return rhs.isNull();
}
template<class T>
inline bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs) noexcept
{
return !rhs.isNull();
}
//
// operator-
//