From 937e92fe83436c0d02e1a3c1783fc896ffae67ad Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 25 Jun 2009 17:06:51 +0200 Subject: 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 --- src/corelib/tools/qsharedpointer_impl.h | 76 +++++++++------------------------ 1 file 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 InternalRefCount; template class ExternalRefCount; - template QSharedPointer qStrongRefFromWeakHelper(const QWeakPointer &, X*); - template QSharedPointer qSharedPointerCastHelper(const QSharedPointer &src, X *); - template QSharedPointer qSharedPointerConstCastHelper(const QSharedPointer &src, X *); + template QSharedPointer strongRefFromWeakHelper(const QWeakPointer &, X*); + template QSharedPointer copyAndSetPointer(X * ptr, const QSharedPointer &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 friend class ExternalRefCount; template friend class QWeakPointer; - template friend QSharedPointer qSharedPointerCastHelper(const QSharedPointer &src, X *); - template friend QSharedPointer qSharedPointerDynamicCastHelper(const QSharedPointer &src, X *); - template friend QSharedPointer qSharedPointerConstCastHelper(const QSharedPointer &src, X *); - template friend QSharedPointer QtSharedPointer::qStrongRefFromWeakHelper(const QWeakPointer &src, X *); + template friend QSharedPointer copyAndSetPointer(X * ptr, const QSharedPointer &src); + template friend QSharedPointer QtSharedPointer::strongRefFromWeakHelper(const QWeakPointer &src, X *); #endif inline void internalSet(Data *o, T *actual) @@ -292,9 +289,9 @@ public: } inline QSharedPointer(const QWeakPointer &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast(0)); } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast(0)); } inline QSharedPointer &operator=(const QWeakPointer &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast(0)); return *this; } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast(0)); return *this; } template inline QSharedPointer(const QSharedPointer &other) { *this = other; } @@ -309,11 +306,11 @@ public: template inline QSharedPointer(const QWeakPointer &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast(0)); } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast(0)); } template inline QSharedPointer &operator=(const QWeakPointer &other) - { *this = qStrongRefFromWeakHelper(other, static_cast(0)); return *this; } + { *this = strongRefFromWeakHelper(other, static_cast(0)); return *this; } template QSharedPointer staticCast() const @@ -417,7 +414,7 @@ private: #if defined(Q_NO_TEMPLATE_FRIENDS) public: #else - template friend QSharedPointer QtSharedPointer::qStrongRefFromWeakHelper(const QWeakPointer &src, X *); + template friend QSharedPointer QtSharedPointer::strongRefFromWeakHelper(const QWeakPointer &src, X *); #endif inline void internalSet(Data *o, T *actual) @@ -488,47 +485,14 @@ Q_INLINE_TEMPLATE QWeakPointer QSharedPointer::toWeakRef() const namespace QtSharedPointer { // helper functions: template - Q_INLINE_TEMPLATE X *qVerifyStaticCast(T *src, X *) - { - return static_cast(src); // if you get an error in this line, the cast is invalid - } - template - Q_INLINE_TEMPLATE X *qVerifyDynamicCast(T *src, X *) - { - return dynamic_cast(src); // if you get an error in this line, the cast is invalid - } - template - Q_INLINE_TEMPLATE X *qVerifyConstCast(T *src, X *) - { - return const_cast(src); // if you get an error in this line, the cast is invalid - } - - template - Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCastHelper(const QSharedPointer &src, X *) - { - QSharedPointer result; - register T *ptr = src.data(); - result.internalSet(src.d, static_cast(ptr)); - return result; - } - template - Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCastHelper(const QSharedPointer &src, X *) - { - QSharedPointer result; - register T *ptr = src.data(); - result.internalSet(src.d, dynamic_cast(ptr)); - return result; - } - template - Q_INLINE_TEMPLATE QSharedPointer qSharedPointerConstCastHelper(const QSharedPointer &src, X *) + Q_INLINE_TEMPLATE QSharedPointer copyAndSetPointer(X *ptr, const QSharedPointer &src) { QSharedPointer result; - register T *ptr = src.data(); - result.internalSet(src.d, const_cast(ptr)); + result.internalSet(src.d, ptr); return result; } template - Q_INLINE_TEMPLATE QSharedPointer qStrongRefFromWeakHelper + Q_INLINE_TEMPLATE QSharedPointer strongRefFromWeakHelper (const QT_PREPEND_NAMESPACE(QWeakPointer) &src, X *) { QSharedPointer result; @@ -541,9 +505,8 @@ namespace QtSharedPointer { template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCast(const QSharedPointer &src) { - X *x = 0; - QtSharedPointer::qVerifyStaticCast(src.data(), x); - return QtSharedPointer::qSharedPointerCastHelper(src, x); + register X *ptr = static_cast(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCast(const QWeakPointer &src) @@ -554,8 +517,8 @@ Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCast(const QWeakPointer &sr template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCast(const QSharedPointer &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerDynamicCastHelper(src, x); + register X *ptr = dynamic_cast(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCast(const QWeakPointer &src) @@ -566,14 +529,13 @@ Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCast(const QWeakPointer template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerConstCast(const QSharedPointer &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerConstCastHelper(src, x); + register X *ptr = const_cast(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerConstCast(const QWeakPointer &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerConstCastHelper(src, x); + return qSharedPointerConstCast(src.toStrongRef()); } template -- cgit v0.12