From 242bc539ab6d9e76e1f67f2a51918c79970c2788 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 6 Jun 2019 12:52:15 +0200 Subject: QWeakPointer: use an alternative work-round for internalData() users MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous work-around fails, probably because of cross-dependencies. E.g. we have this in QtScXml: In file included from /home/qt/work/install/include/QtCore/qsharedpointer.h:48:0, from ../../src/scxml/qscxmltabledata_p.h:55, from ../../src/scxml/qscxmltabledata.cpp:40: /home/qt/work/install/include/QtCore/qsharedpointer_impl.h:687:12: error: ‘QPointer’ does not name a type; did you mean ‘pointer’? friend QPointer ^~~~~~~~ pointer /home/qt/work/install/include/QtCore/qsharedpointer_impl.h:689:23: error: ‘QSmartPointerConvertFunctor’ in namespace ‘QtPrivate’ does not name a template type friend QtPrivate::QSmartPointerConvertFunctor; ^~~~~~~~~~~~~~~~~~~~~~~~~~~ To fix, grand friendship only to a non-template class with a templated static method that returns internalData(). This fixes most users, except in qmetatype.h, which does not include qsharedpointer.h. In order to use the non-template class in there, we need to delay its name lookup to instantiation time. We do this by artificially making it a dependent name, by using a class template that inherits from our befrieded class. Change-Id: I12b427f1fe9503df819ea5436d780972d6402e68 Reviewed-by: Ville Voutilainen --- src/corelib/kernel/qmetatype.h | 7 ++++++- src/corelib/kernel/qpointer.h | 3 ++- src/corelib/tools/qsharedpointer_impl.h | 28 +++++++++++++++++----------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 154ccf62bb..fef25a32c4 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -1715,12 +1715,17 @@ namespace QtPrivate { } }; + // hack to delay name lookup to instantiation time by making + // EnableInternalData a dependent name: + template + struct EnableInternalDataWrap; + template struct QSmartPointerConvertFunctor > { QObject* operator()(const QWeakPointer &p) const { - return p.internalData(); + return QtPrivate::EnableInternalDataWrap::internalData(p); } }; } diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h index 80faef2990..7052bcf0d4 100644 --- a/src/corelib/kernel/qpointer.h +++ b/src/corelib/kernel/qpointer.h @@ -143,7 +143,8 @@ template QPointer qPointerFromVariant(const QVariant &variant) { - return QPointer(qobject_cast(QtSharedPointer::weakPointerFromVariant_internal(variant).internalData())); + const auto wp = QtSharedPointer::weakPointerFromVariant_internal(variant); + return QPointer{qobject_cast(QtPrivate::EnableInternalData::internalData(wp))}; } template diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 55f0f17c52..f352e433c5 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -107,6 +107,10 @@ template QSharedPointer qSharedPointerObjectCast(const QSharedPointer &ptr); #endif +namespace QtPrivate { +struct EnableInternalData; +} + namespace QtSharedPointer { template class ExternalRefCount; @@ -672,21 +676,12 @@ public: #endif private: - + friend struct QtPrivate::EnableInternalData; #if defined(Q_NO_TEMPLATE_FRIENDS) public: #else template friend class QSharedPointer; template friend class QPointer; -# ifndef QT_NO_QOBJECT - template - friend QWeakPointer::Value, X>::type> - qWeakPointerFromVariant(const QVariant &variant); -# endif - template - friend QPointer - qPointerFromVariant(const QVariant &variant); - friend QtPrivate::QSmartPointerConvertFunctor; #endif template @@ -721,6 +716,17 @@ public: T *value; }; +namespace QtPrivate { +struct EnableInternalData { + template + static T *internalData(const QWeakPointer &p) noexcept { return p.internalData(); } +}; +// hack to delay name lookup to instantiation time by making +// EnableInternalData a dependent name: +template +struct EnableInternalDataWrap : EnableInternalData {}; +} + template class QEnableSharedFromThis { @@ -996,7 +1002,7 @@ template QWeakPointer::Value, T>::type> qWeakPointerFromVariant(const QVariant &variant) { - return QWeakPointer(qobject_cast(QtSharedPointer::weakPointerFromVariant_internal(variant).internalData())); + return QWeakPointer(qobject_cast(QtPrivate::EnableInternalData::internalData(QtSharedPointer::weakPointerFromVariant_internal(variant)))); } template QSharedPointer::Value, T>::type> -- cgit v1.2.3