From b1d6af35a45860ad9dfbd65979ca1df44abae446 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 9 Mar 2014 11:56:56 +0100 Subject: [PATCH] QWeakPointer: add member-swap [ChangeLog][QtCore][QWeakPointer] Added member-swap function. Change-Id: Ide3672dc74a9d8153e5f930290d938e8c23993b5 Reviewed-by: Olivier Goffart Reviewed-by: Thiago Macieira --- src/corelib/tools/qsharedpointer.cpp | 8 +++++++ src/corelib/tools/qsharedpointer.h | 2 ++ src/corelib/tools/qsharedpointer_impl.h | 6 +++++ .../qsharedpointer/tst_qsharedpointer.cpp | 24 +++++++++++++++++++ 4 files changed, 40 insertions(+) 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 @@ -770,6 +770,14 @@ you will get a compiler error. */ +/*! + \fn void QWeakPointer::swap(QWeakPointer &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 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 operator=(const QObject *other); + void swap(QWeakPointer &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 &o) : d(o.d), value(o.data()) { if (d) d->weakref.ref();} inline QWeakPointer &operator=(const QSharedPointer &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 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()