QWeakPointer: add member-swap

[ChangeLog][QtCore][QWeakPointer] Added member-swap function.

Change-Id: Ide3672dc74a9d8153e5f930290d938e8c23993b5
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2014-03-09 11:56:56 +01:00 committed by The Qt Project
parent e3a8e242ad
commit b1d6af35a4
4 changed files with 40 additions and 0 deletions

View File

@ -770,6 +770,14 @@
you will get a compiler error.
*/
/*!
\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

View File

@ -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();

View File

@ -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)

View File

@ -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()