From 982ef5b494319bfaaa585cf0620a821dc3b7c642 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 3 May 2016 23:19:49 +0200 Subject: QSharedPointer/QWeakPointer/QScopedPointer: add comparison against nullptr Some constructors were added, but the comparison operators were missing. The STL has them, so we ought have them too. Change-Id: I030c14a3b355988f509716b4b1b1a835b3ab9481 Reviewed-by: Marc Mutz --- src/corelib/tools/qscopedpointer.cpp | 42 +++++++++++++++++ src/corelib/tools/qscopedpointer.h | 24 ++++++++++ src/corelib/tools/qsharedpointer.cpp | 84 +++++++++++++++++++++++++++++++++ src/corelib/tools/qsharedpointer.h | 8 ++++ src/corelib/tools/qsharedpointer_impl.h | 48 +++++++++++++++++++ 5 files changed, 206 insertions(+) (limited to 'src/corelib') diff --git a/src/corelib/tools/qscopedpointer.cpp b/src/corelib/tools/qscopedpointer.cpp index 1a37e0bc9c..67d11660e1 100644 --- a/src/corelib/tools/qscopedpointer.cpp +++ b/src/corelib/tools/qscopedpointer.cpp @@ -192,6 +192,48 @@ QT_BEGIN_NAMESPACE Otherwise returns \c false. */ +/*! + \fn bool operator==(const QScopedPointer &lhs, std::nullptr_t) + \relates QScopedPointer + \since 5.8 + + Returns \c true if the scoped pointer \a lhs is a null pointer. + + \sa QScopedPointer::isNull() +*/ + +/*! + \fn bool operator==(std::nullptr_t, const QScopedPointer &rhs) + \relates QScopedPointer + \since 5.8 + + Returns \c true if the scoped pointer \a rhs is a null pointer. + + \sa QScopedPointer::isNull() +*/ + +/*! + \fn bool operator!=(const QScopedPointer &lhs, std::nullptr_t) + \relates QScopedPointer + \since 5.8 + + Returns \c true if the scoped pointer \a lhs is a valid (i.e. a non-null) + pointer. + + \sa QScopedPointer::isNull() +*/ + +/*! + \fn bool operator!=(std::nullptr_t, const QScopedPointer &rhs) + \relates QScopedPointer + \since 5.8 + + Returns \c true if the scoped pointer \a rhs is a valid (i.e. a non-null) + pointer. + + \sa QScopedPointer::isNull() +*/ + /*! \fn bool QScopedPointer::isNull() const diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index 18fbe2c808..92d7df6e5d 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -187,6 +187,30 @@ inline bool operator!=(const QScopedPointer &lhs, const QScopedPoint return lhs.data() != rhs.data(); } +template +inline bool operator==(const QScopedPointer &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return lhs.isNull(); +} + +template +inline bool operator==(std::nullptr_t, const QScopedPointer &rhs) Q_DECL_NOTHROW +{ + return rhs.isNull(); +} + +template +inline bool operator!=(const QScopedPointer &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return !lhs.isNull(); +} + +template +inline bool operator!=(std::nullptr_t, const QScopedPointer &rhs) Q_DECL_NOTHROW +{ + return !rhs.isNull(); +} + template inline void swap(QScopedPointer &p1, QScopedPointer &p2) Q_DECL_NOTHROW { p1.swap(p2); } diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index 939a1bdffd..af09ef6f40 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -1147,6 +1147,90 @@ \a ptr1's, you will get a compiler error. */ +/*! + \fn bool operator==(const QSharedPointer &lhs, std::nullptr_t) + \relates QSharedPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a lhs is a null pointer. + + \sa QSharedPointer::isNull() +*/ + +/*! + \fn bool operator==(std::nullptr_t, const QSharedPointer &rhs) + \relates QSharedPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a rhs is a null pointer. + + \sa QSharedPointer::isNull() +*/ + +/*! + \fn bool operator!=(const QSharedPointer &lhs, std::nullptr_t) + \relates QSharedPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a lhs is a valid (i.e. + non-null) pointer. + + \sa QSharedPointer::isNull() +*/ + +/*! + \fn bool operator!=(std::nullptr_t, const QSharedPointer &rhs) + \relates QSharedPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a rhs is a valid (i.e. + non-null) pointer. + + \sa QSharedPointer::isNull() +*/ + +/*! + \fn bool operator==(const QWeakPointer &lhs, std::nullptr_t) + \relates QWeakPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a lhs is a null pointer. + + \sa QWeakPointer::isNull() +*/ + +/*! + \fn bool operator==(std::nullptr_t, const QWeakPointer &rhs) + \relates QWeakPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a rhs is a null pointer. + + \sa QWeakPointer::isNull() +*/ + +/*! + \fn bool operator!=(const QWeakPointer &lhs, std::nullptr_t) + \relates QWeakPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a lhs is a valid (i.e. + non-null) pointer. + + \sa QWeakPointer::isNull() +*/ + +/*! + \fn bool operator!=(std::nullptr_t, const QWeakPointer &rhs) + \relates QWeakPointer + \since 5.8 + + Returns \c true if the pointer referenced by \a rhs is a valid (i.e. + non-null) pointer. + + \sa QWeakPointer::isNull() +*/ + /*! \fn bool operator!=(const QWeakPointer &ptr1, const QSharedPointer &ptr2) \relates QWeakPointer diff --git a/src/corelib/tools/qsharedpointer.h b/src/corelib/tools/qsharedpointer.h index 6b38f0e80c..3b86eb238b 100644 --- a/src/corelib/tools/qsharedpointer.h +++ b/src/corelib/tools/qsharedpointer.h @@ -149,6 +149,14 @@ template bool operator==(const QWeakPointer &ptr1, const QS template bool operator!=(const QWeakPointer &ptr1, const QSharedPointer &ptr2); template bool operator==(const QSharedPointer &ptr1, const QWeakPointer &ptr2); template bool operator!=(const QSharedPointer &ptr1, const QWeakPointer &ptr2); +template bool operator==(const QSharedPointer &lhs, std::nullptr_t); +template bool operator!=(const QSharedPointer &lhs, std::nullptr_t); +template bool operator==(std::nullptr_t, const QSharedPointer &rhs); +template bool operator!=(std::nullptr_t, const QSharedPointer &rhs); +template bool operator==(const QWeakPointer &lhs, std::nullptr_t); +template bool operator!=(const QWeakPointer &lhs, std::nullptr_t); +template bool operator==(std::nullptr_t, const QWeakPointer &rhs); +template bool operator!=(std::nullptr_t, const QWeakPointer &rhs); template QSharedPointer qSharedPointerCast(const QSharedPointer &other); template QSharedPointer qSharedPointerCast(const QWeakPointer &other); diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index dc118d7a68..0f769ffa86 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -822,6 +822,54 @@ bool operator!=(const QSharedPointer &ptr1, const QWeakPointer &ptr2) Q_DE return ptr2 != ptr1; } +template +inline bool operator==(const QSharedPointer &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return lhs.isNull(); +} + +template +inline bool operator!=(const QSharedPointer &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return !lhs.isNull(); +} + +template +inline bool operator==(std::nullptr_t, const QSharedPointer &rhs) Q_DECL_NOTHROW +{ + return rhs.isNull(); +} + +template +inline bool operator!=(std::nullptr_t, const QSharedPointer &rhs) Q_DECL_NOTHROW +{ + return !rhs.isNull(); +} + +template +inline bool operator==(const QWeakPointer &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return lhs.isNull(); +} + +template +inline bool operator!=(const QWeakPointer &lhs, std::nullptr_t) Q_DECL_NOTHROW +{ + return !lhs.isNull(); +} + +template +inline bool operator==(std::nullptr_t, const QWeakPointer &rhs) Q_DECL_NOTHROW +{ + return rhs.isNull(); +} + +template +inline bool operator!=(std::nullptr_t, const QWeakPointer &rhs) Q_DECL_NOTHROW +{ + return !rhs.isNull(); +} + // // operator- // -- cgit v1.2.3