diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2009-06-25 15:06:51 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2009-06-26 09:20:08 (GMT) |
commit | 937e92fe83436c0d02e1a3c1783fc896ffae67ad (patch) | |
tree | 4425bf3e58113b220f11ab9d6a5e0ffee90f9429 /src | |
parent | 4209fba25612d93070976a597d2fa01325631e3b (diff) | |
download | Qt-937e92fe83436c0d02e1a3c1783fc896ffae67ad.zip Qt-937e92fe83436c0d02e1a3c1783fc896ffae67ad.tar.gz Qt-937e92fe83436c0d02e1a3c1783fc896ffae67ad.tar.bz2 |
Clean up QSharedPointer cast implementations.
There's no need to have separate cast-checker functions, plus the cast-
and centralise the function to create the object.
Reviewed-by: TrustMe
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 76 |
1 files changed, 19 insertions, 57 deletions
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 0471a1b..2797622 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -87,9 +87,8 @@ namespace QtSharedPointer { template <class T> class InternalRefCount; template <class T> class ExternalRefCount; - template <class X, class T> QSharedPointer<X> qStrongRefFromWeakHelper(const QWeakPointer<T> &, X*); - template <class X, class T> QSharedPointer<X> qSharedPointerCastHelper(const QSharedPointer<T> &src, X *); - template <class X, class T> QSharedPointer<X> qSharedPointerConstCastHelper(const QSharedPointer<T> &src, X *); + template <class X, class T> QSharedPointer<X> strongRefFromWeakHelper(const QWeakPointer<T> &, X*); + template <class X, class Y> QSharedPointer<X> copyAndSetPointer(X * ptr, const QSharedPointer<Y> &src); // used in debug mode to verify the reuse of pointers Q_CORE_EXPORT void internalSafetyCheckAdd(const volatile void *); @@ -239,10 +238,8 @@ namespace QtSharedPointer { #else template <class X> friend class ExternalRefCount; template <class X> friend class QWeakPointer; - template <class X, class Y> friend QSharedPointer<X> qSharedPointerCastHelper(const QSharedPointer<Y> &src, X *); - template <class X, class Y> friend QSharedPointer<X> qSharedPointerDynamicCastHelper(const QSharedPointer<Y> &src, X *); - template <class X, class Y> friend QSharedPointer<X> qSharedPointerConstCastHelper(const QSharedPointer<Y> &src, X *); - template <class X, class Y> friend QSharedPointer<X> QtSharedPointer::qStrongRefFromWeakHelper(const QWeakPointer<Y> &src, X *); + template <class X, class Y> friend QSharedPointer<X> copyAndSetPointer(X * ptr, const QSharedPointer<Y> &src); + template <class X, class Y> friend QSharedPointer<X> QtSharedPointer::strongRefFromWeakHelper(const QWeakPointer<Y> &src, X *); #endif inline void internalSet(Data *o, T *actual) @@ -292,9 +289,9 @@ public: } inline QSharedPointer(const QWeakPointer<T> &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast<T*>(0)); } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast<T*>(0)); } inline QSharedPointer<T> &operator=(const QWeakPointer<T> &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast<T*>(0)); return *this; } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast<T*>(0)); return *this; } template <class X> inline QSharedPointer(const QSharedPointer<X> &other) { *this = other; } @@ -309,11 +306,11 @@ public: template <class X> inline QSharedPointer(const QWeakPointer<X> &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast<T *>(0)); } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast<T *>(0)); } template <class X> inline QSharedPointer<T> &operator=(const QWeakPointer<X> &other) - { *this = qStrongRefFromWeakHelper(other, static_cast<T *>(0)); return *this; } + { *this = strongRefFromWeakHelper(other, static_cast<T *>(0)); return *this; } template <class X> QSharedPointer<X> staticCast() const @@ -417,7 +414,7 @@ private: #if defined(Q_NO_TEMPLATE_FRIENDS) public: #else - template <class X, class Y> friend QSharedPointer<X> QtSharedPointer::qStrongRefFromWeakHelper(const QWeakPointer<Y> &src, X *); + template <class X, class Y> friend QSharedPointer<X> QtSharedPointer::strongRefFromWeakHelper(const QWeakPointer<Y> &src, X *); #endif inline void internalSet(Data *o, T *actual) @@ -488,47 +485,14 @@ Q_INLINE_TEMPLATE QWeakPointer<T> QSharedPointer<T>::toWeakRef() const namespace QtSharedPointer { // helper functions: template <class X, class T> - Q_INLINE_TEMPLATE X *qVerifyStaticCast(T *src, X *) - { - return static_cast<X *>(src); // if you get an error in this line, the cast is invalid - } - template <class X, class T> - Q_INLINE_TEMPLATE X *qVerifyDynamicCast(T *src, X *) - { - return dynamic_cast<X *>(src); // if you get an error in this line, the cast is invalid - } - template <class X, class T> - Q_INLINE_TEMPLATE X *qVerifyConstCast(T *src, X *) - { - return const_cast<X *>(src); // if you get an error in this line, the cast is invalid - } - - template <class X, class T> - Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCastHelper(const QSharedPointer<T> &src, X *) - { - QSharedPointer<X> result; - register T *ptr = src.data(); - result.internalSet(src.d, static_cast<X *>(ptr)); - return result; - } - template <class X, class T> - Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCastHelper(const QSharedPointer<T> &src, X *) - { - QSharedPointer<X> result; - register T *ptr = src.data(); - result.internalSet(src.d, dynamic_cast<X *>(ptr)); - return result; - } - template <class X, class T> - Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerConstCastHelper(const QSharedPointer<T> &src, X *) + Q_INLINE_TEMPLATE QSharedPointer<X> copyAndSetPointer(X *ptr, const QSharedPointer<T> &src) { QSharedPointer<X> result; - register T *ptr = src.data(); - result.internalSet(src.d, const_cast<X *>(ptr)); + result.internalSet(src.d, ptr); return result; } template <class X, class T> - Q_INLINE_TEMPLATE QSharedPointer<X> qStrongRefFromWeakHelper + Q_INLINE_TEMPLATE QSharedPointer<X> strongRefFromWeakHelper (const QT_PREPEND_NAMESPACE(QWeakPointer<T>) &src, X *) { QSharedPointer<X> result; @@ -541,9 +505,8 @@ namespace QtSharedPointer { template <class X, class T> Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &src) { - X *x = 0; - QtSharedPointer::qVerifyStaticCast(src.data(), x); - return QtSharedPointer::qSharedPointerCastHelper(src, x); + register X *ptr = static_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template <class X, class T> Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &src) @@ -554,8 +517,8 @@ Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &sr template <class X, class T> Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerDynamicCastHelper(src, x); + register X *ptr = dynamic_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template <class X, class T> Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src) @@ -566,14 +529,13 @@ Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer template <class X, class T> Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerConstCastHelper(src, x); + register X *ptr = const_cast<X *>(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template <class X, class T> Q_INLINE_TEMPLATE QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerConstCastHelper(src, x); + return qSharedPointerConstCast<X, T>(src.toStrongRef()); } template <class X, class T> |