From cc7239da8d1ab95e68e12a64df3ca3051419cb34 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sun, 10 Feb 2013 10:57:43 +0100 Subject: [PATCH] Make it possible to use QPointer This is possible with QWeakPointer, so allow it for migrating code too. In the process, replace the QPointerBase with a member variable for simplicity. The functionality of the QPointerBase is replaced by a TypeSelector template. Change-Id: I3b4c77bdeda2b863cc33e84a3da8a25bae928c8c Reviewed-by: Thiago Macieira Reviewed-by: Olivier Goffart --- dist/changes-5.1.0 | 3 + src/corelib/kernel/qpointer.h | 56 ++++++++----------- src/corelib/tools/qsharedpointer_impl.h | 2 +- .../corelib/kernel/qpointer/tst_qpointer.cpp | 7 +++ 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/dist/changes-5.1.0 b/dist/changes-5.1.0 index 8ee691055f9..f7984265ff4 100644 --- a/dist/changes-5.1.0 +++ b/dist/changes-5.1.0 @@ -64,6 +64,9 @@ QtCore inside the pattern string, as well as the numerical index of each named capturing group. + - QPointer + * It is now possible to create a QPointer with a const templated type. + - QtGui diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h index 385bc2814b0..230b6b66eb7 100644 --- a/src/corelib/kernel/qpointer.h +++ b/src/corelib/kernel/qpointer.h @@ -50,56 +50,44 @@ QT_BEGIN_NAMESPACE class QVariant; -class QPointerBase -{ - QWeakPointer wp; - -protected: - inline QPointerBase() : wp() { } - inline QPointerBase(QObject *p) : wp(p, true) { } - // compiler-generated copy/move ctor/assignment operators are fine! (even though public) - inline ~QPointerBase() { } - - inline QObject* data() const - { return wp.data(); } - - inline void assign(QObject *p) - { wp.assign(p); } - - inline bool isNull() const - { return wp.isNull(); } - - inline void clear() - { wp.clear(); } -}; - template -class QPointer : private QPointerBase +class QPointer { + template + struct TypeSelector + { + typedef QObject Type; + }; + template + struct TypeSelector + { + typedef const QObject Type; + }; + typedef typename TypeSelector::Type QObjectType; + QWeakPointer wp; public: inline QPointer() { } - inline QPointer(T *p) : QPointerBase(p) { } + inline QPointer(T *p) : wp(p, true) { } // compiler-generated copy/move ctor/assignment operators are fine! inline ~QPointer() { } inline QPointer &operator=(T* p) - { QPointerBase::assign(p); return *this; } + { wp.assign(static_cast(p)); return *this; } inline T* data() const - { return static_cast(QPointerBase::data()); } + { return static_cast( wp.data()); } inline T* operator->() const { return data(); } inline T& operator*() const { return *data(); } inline operator T*() const { return data(); } -#ifdef Q_QDOC - inline bool isNull() const; - inline void clear(); -#else - using QPointerBase::isNull; - using QPointerBase::clear; -#endif + + inline bool isNull() const + { return wp.isNull(); } + + inline void clear() + { wp.clear(); } }; template Q_DECLARE_TYPEINFO_BODY(QPointer, Q_MOVABLE_TYPE); diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index f6ef7cd55d4..3121bcdf40f 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -639,7 +639,7 @@ private: public: #else template friend class QSharedPointer; - friend class QPointerBase; + template friend class QPointer; #endif template diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp index 7c6549364fd..609b4b7dce6 100644 --- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp +++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp @@ -65,6 +65,7 @@ private slots: void threadSafety(); void qvariantCast(); + void constPointer(); }; void tst_QPointer::constructors() @@ -384,6 +385,12 @@ void tst_QPointer::qvariantCast() // QPointer sop = qPointerFromVariant(v); } +void tst_QPointer::constPointer() +{ + // Compile-time test that QPointer works. + QPointer fp = new QFile; + delete fp.data(); +} QTEST_MAIN(tst_QPointer)