diff options
-rw-r--r-- | src/corelib/tools/qsharedpointer.cpp | 8 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 6 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 24 |
4 files changed, 40 insertions, 0 deletions
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index 19c88fcf1c..2ced3d6a7c 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -771,6 +771,14 @@ */ /*! + \fn void QWeakPointer::swap(QWeakPointer<T> &other) + \since 5.4 + + Swaps this weak pointer instance with \a other. This function is + very fast and never fails. +*/ + +/*! \fn bool QWeakPointer::isNull() const Returns \c true if this object is holding a reference to a null diff --git a/src/corelib/tools/qsharedpointer.h b/src/corelib/tools/qsharedpointer.h index ea9b4fbf27..d9de48b7f4 100644 --- a/src/corelib/tools/qsharedpointer.h +++ b/src/corelib/tools/qsharedpointer.h @@ -122,6 +122,8 @@ public: QWeakPointer(const QObject *other); QWeakPointer<T> operator=(const QObject *other); + void swap(QWeakPointer<T> &other); + T *data() const; void clear(); diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index e59efce7ae..f70e398cfe 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -587,6 +587,12 @@ public: return *this; } + inline void swap(QWeakPointer &other) + { + qSwap(this->d, other.d); + qSwap(this->value, other.value); + } + inline QWeakPointer(const QSharedPointer<T> &o) : d(o.d), value(o.data()) { if (d) d->weakref.ref();} inline QWeakPointer &operator=(const QSharedPointer<T> &o) diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 1d466a4734..9bcce60e93 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -392,6 +392,30 @@ void tst_QSharedPointer::swap() QVERIFY(p2 != control); QVERIFY(p2.isNull()); QVERIFY(*p1 == 42); + + QWeakPointer<int> w1, w2 = control; + + QVERIFY(w1.isNull()); + QVERIFY(!w2.isNull()); + QVERIFY(w2.lock() == control); + QVERIFY(!w1.lock()); + + w1.swap(w2); + QVERIFY(w2.isNull()); + QVERIFY(!w1.isNull()); + QVERIFY(w1.lock() == control); + QVERIFY(!w2.lock()); + + qSwap(w1, w2); + QVERIFY(w1.isNull()); + QVERIFY(w2.lock() == control); + + p1.reset(); + p2.reset(); + control.reset(); + + QVERIFY(w1.isNull()); + QVERIFY(w2.isNull()); } void tst_QSharedPointer::useOfForwardDeclared() |