diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2014-03-09 11:46:26 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-04-03 22:35:57 +0200 |
commit | e3a8e242ada146b1ec796424696e6c3964ab36dc (patch) | |
tree | 2247cea75ee30120b0e16fc007f0608092f87201 | |
parent | ef2a2a9c03b6cce57375bd998633d759a4ec1b3f (diff) |
QWeakPointer: add lock() for std::weak_ptr compatibility
[ChangeLog][QtCore][QWeakPointer] Added lock() method for std::weak_ptr compatibility.
Change-Id: I0851d91c51f5a4f04a855a1d8082234ce38396b4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/tools/qsharedpointer.cpp | 9 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer.h | 1 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 2 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 18 |
4 files changed, 30 insertions, 0 deletions
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index a610fc46e5..19c88fcf1c 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -894,6 +894,15 @@ */ /*! + \fn QSharedPointer<T> QWeakPointer::lock() const + \since 5.4 + + Same as toStrongRef(). + + This function is provided for API compatibility with std::weak_ptr. +*/ + +/*! \fn void QWeakPointer::clear() Clears this QWeakPointer object, dropping the reference that it diff --git a/src/corelib/tools/qsharedpointer.h b/src/corelib/tools/qsharedpointer.h index 34b4bfbb12..ea9b4fbf27 100644 --- a/src/corelib/tools/qsharedpointer.h +++ b/src/corelib/tools/qsharedpointer.h @@ -126,6 +126,7 @@ public: void clear(); QSharedPointer<T> toStrongRef() const; + QSharedPointer<T> lock() const; }; template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2); diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index c077b84492..e59efce7ae 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -639,6 +639,8 @@ public: inline void clear() { *this = QWeakPointer(); } inline QSharedPointer<T> toStrongRef() const { return QSharedPointer<T>(*this); } + // std::weak_ptr compatibility: + inline QSharedPointer<T> lock() const { return toStrongRef(); } #if defined(QWEAKPOINTER_ENABLE_ARROW) inline T *operator->() const { return data(); } diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 8c76e6b440..1d466a4734 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -74,6 +74,7 @@ private slots: void useOfForwardDeclared(); void memoryManagement(); void dropLastReferenceOfForwardDeclared(); + void lock(); void downCast(); void functionCallDownCast(); void upCast(); @@ -494,6 +495,22 @@ void tst_QSharedPointer::dropLastReferenceOfForwardDeclared() QCOMPARE(forwardDeclaredDestructorRunCount, 1); } +void tst_QSharedPointer::lock() +{ + QSharedPointer<int> sp = QSharedPointer<int>::create(); + QVERIFY(sp); + QWeakPointer<int> wp = sp; + QVERIFY(sp == wp); + QVERIFY(sp == wp.lock()); + QVERIFY(sp == wp.toStrongRef()); + + sp.reset(); + QVERIFY(!wp); + QVERIFY(sp != wp); // this is why op(shared_ptr, weak_ptr) is a bad idea (apart from MT races)... + QVERIFY(sp == wp.lock()); + QVERIFY(sp == wp.toStrongRef()); +} + class DerivedData: public Data { public: @@ -866,6 +883,7 @@ void tst_QSharedPointer::objectCast() ptr.clear(); QVERIFY(ptr.isNull()); QVERIFY(weakptr.toStrongRef().isNull()); + QVERIFY(weakptr.lock().isNull()); // verify that the object casts fail without crash QSharedPointer<OtherObject> otherptr = qSharedPointerObjectCast<OtherObject>(weakptr); |