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 <marc.mutz@kdab.com>
bb10
Giuseppe D'Angelo 2016-05-03 23:19:49 +02:00
parent 997572d859
commit 982ef5b494
7 changed files with 231 additions and 1 deletions

View File

@ -192,6 +192,48 @@ QT_BEGIN_NAMESPACE
Otherwise returns \c false.
*/
/*!
\fn bool operator==(const QScopedPointer<T, Cleanup> &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<T, Cleanup> &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<T, Cleanup> &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<T, Cleanup> &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

View File

@ -187,6 +187,30 @@ inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPoint
return lhs.data() != rhs.data();
}
template <class T, class Cleanup>
inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) Q_DECL_NOTHROW
{
return lhs.isNull();
}
template <class T, class Cleanup>
inline bool operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) Q_DECL_NOTHROW
{
return rhs.isNull();
}
template <class T, class Cleanup>
inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) Q_DECL_NOTHROW
{
return !lhs.isNull();
}
template <class T, class Cleanup>
inline bool operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) Q_DECL_NOTHROW
{
return !rhs.isNull();
}
template <class T, class Cleanup>
inline void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) Q_DECL_NOTHROW
{ p1.swap(p2); }

View File

@ -1147,6 +1147,90 @@
\a ptr1's, you will get a compiler error.
*/
/*!
\fn bool operator==(const QSharedPointer<T> &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<T> &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<T> &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<T> &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<T> &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<T> &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<T> &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<T> &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<T> &ptr1, const QSharedPointer<X> &ptr2)
\relates QWeakPointer

View File

@ -149,6 +149,14 @@ template<class T, class X> bool operator==(const QWeakPointer<T> &ptr1, const QS
template<class T, class X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
template<class T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t);
template<class T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t);
template<class T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs);
template<class T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs);
template<class T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t);
template<class T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t);
template<class T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs);
template<class T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs);
template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other);
template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other);

View File

@ -822,6 +822,54 @@ bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2) Q_DE
return ptr2 != ptr1;
}
template<class T>
inline bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW
{
return lhs.isNull();
}
template<class T>
inline bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW
{
return !lhs.isNull();
}
template<class T>
inline bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs) Q_DECL_NOTHROW
{
return rhs.isNull();
}
template<class T>
inline bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs) Q_DECL_NOTHROW
{
return !rhs.isNull();
}
template<class T>
inline bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW
{
return lhs.isNull();
}
template<class T>
inline bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t) Q_DECL_NOTHROW
{
return !lhs.isNull();
}
template<class T>
inline bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs) Q_DECL_NOTHROW
{
return rhs.isNull();
}
template<class T>
inline bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs) Q_DECL_NOTHROW
{
return !rhs.isNull();
}
//
// operator-
//

View File

@ -278,12 +278,16 @@ void tst_QScopedPointer::isNull()
{
QScopedPointer<int> p;
QVERIFY(p.isNull());
QVERIFY(p == nullptr);
QVERIFY(nullptr == p);
}
/* Invoke on a set value. */
{
QScopedPointer<int> p(new int(69));
QVERIFY(!p.isNull());
QVERIFY(p != nullptr);
QVERIFY(nullptr != p);
}
}

View File

@ -371,12 +371,22 @@ void tst_QSharedPointer::nullptrOps()
QSharedPointer<char> null;
QVERIFY(p1 == null);
QVERIFY(p2 == null);
QVERIFY(p1 == nullptr);
QVERIFY(nullptr == p1);
QVERIFY(!p1);
QVERIFY(!p1.data());
QVERIFY(p2 == null);
QVERIFY(p2 == nullptr);
QVERIFY(nullptr == p2);
QVERIFY(!p2);
QVERIFY(!p2.data());
QVERIFY(p1 == p2);
QSharedPointer<char> p3 = p1;
QVERIFY(p3 == p1);
QVERIFY(p3 == null);
QVERIFY(p3 == nullptr);
QVERIFY(nullptr == p3);
QVERIFY(!p3.data());
p3 = nullptr;
@ -386,6 +396,16 @@ void tst_QSharedPointer::nullptrOps()
QSharedPointer<char> p2_zero = 0;
p3 = 0;
QSharedPointer<char> p4(new char);
QVERIFY(p4);
QVERIFY(p4.data());
QVERIFY(p4 != nullptr);
QVERIFY(nullptr != p4);
QVERIFY(p4 != p1);
QVERIFY(p4 != p2);
QVERIFY(p4 != null);
QVERIFY(p4 != p3);
}
void tst_QSharedPointer::swap()