From b4c17476129e07dd3bf52c6aac8a51cf30c2dd3a Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 18 Nov 2020 13:17:31 +0100 Subject: [PATCH] Make QMetaTypeInterface constexpr on Windows This was so far problematic as it gave various link errors. The solution to that seems to be to make the default constructor of QPairVariantInterfaceImpl constexpr to get around one set of problems. The other problem to solve where undefined references to metaobjects. The reason for that is apparently that QMetaTypeInterface contains a direct pointer to the meta object, something the linker doesn't like. Adding a level of indirection by using a function that returns the pointer seems to solve that problem. Fixes: QTBUG-88468 Change-Id: I5612ae807ea3b7e49bc40349d8d1fca1be9bd7ee Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qmetatype.h | 84 +++++++++---------- .../kernel/qmetatype/tst_qmetatype.cpp | 35 +++++--- 2 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 86cd15def8..8ef1bb8faa 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -698,7 +698,7 @@ public: { } - QPairVariantInterfaceImpl() + constexpr QPairVariantInterfaceImpl() : _pair(nullptr) , _getFirst(nullptr) , _getSecond(nullptr) @@ -818,36 +818,44 @@ namespace QtPrivate template struct MetaObjectForType { - static constexpr inline const QMetaObject *value() { return nullptr; } + static constexpr const QMetaObject *value() { return nullptr; } + using MetaObjectFn = const QMetaObject *(*)(const QMetaTypeInterface *); + static constexpr MetaObjectFn metaObjectFunction = nullptr; }; #ifndef QT_NO_QOBJECT template<> struct MetaObjectForType { - static constexpr inline const QMetaObject *value() { return nullptr; } + static constexpr const QMetaObject *value() { return nullptr; } + using MetaObjectFn = const QMetaObject *(*)(const QMetaTypeInterface *); + static constexpr MetaObjectFn metaObjectFunction = nullptr; }; template struct MetaObjectForType::Value>::type> { - static constexpr inline const QMetaObject *value() { return &T::staticMetaObject; } + static constexpr const QMetaObject *value() { return &T::staticMetaObject; } + static constexpr const QMetaObject *metaObjectFunction(const QMetaTypeInterface *) { return &T::staticMetaObject; } }; template struct MetaObjectForType::IsGadgetOrDerivedFrom>::type> { - static constexpr inline const QMetaObject *value() { return &T::staticMetaObject; } + static constexpr const QMetaObject *value() { return &T::staticMetaObject; } + static constexpr const QMetaObject *metaObjectFunction(const QMetaTypeInterface *) { return &T::staticMetaObject; } }; template struct MetaObjectForType::IsGadgetOrDerivedFrom>::type> { - static constexpr inline const QMetaObject *value() + static constexpr const QMetaObject *value() { return &IsPointerToGadgetHelper::BaseType::staticMetaObject; } + static constexpr const QMetaObject *metaObjectFunction(const QMetaTypeInterface *) { return value(); } }; template struct MetaObjectForType::Value>::type > { - static constexpr inline const QMetaObject *value() { return qt_getEnumMetaObject(T()); } + static constexpr const QMetaObject *value() { return qt_getEnumMetaObject(T()); } + static constexpr const QMetaObject *metaObjectFunction(const QMetaTypeInterface *) { return value(); } }; #endif @@ -1590,7 +1598,10 @@ public: uint size; uint flags; mutable QBasicAtomicInt typeId; - const QMetaObject *metaObject; + + using MetaObjectFn = const QMetaObject *(*)(const QMetaTypeInterface *); + const MetaObjectFn metaObjectFn; + const char *name; using DefaultCtrFn = void (*)(const QMetaTypeInterface *, void *); @@ -2224,42 +2235,27 @@ public: template struct QMetaTypeInterfaceWrapper { - static inline constexpr QMetaTypeInterface create() - { - return { - /*.revision=*/ 0, - /*.alignment=*/ alignof(T), - /*.size=*/ sizeof(T), - /*.flags=*/ QMetaTypeTypeFlags::Flags, - /*.typeId=*/ BuiltinMetaType::value, - /*.metaObject=*/ MetaObjectForType::value(), - /*.name=*/ QMetaTypeForType::getName(), - /*.defaultCtr=*/ QMetaTypeForType::getDefaultCtr(), - /*.copyCtr=*/ QMetaTypeForType::getCopyCtr(), - /*.moveCtr=*/ QMetaTypeForType::getMoveCtr(), - /*.dtor=*/ QMetaTypeForType::getDtor(), - /*.equals=*/ QEqualityOperatorForType::equals, - /*.lessThan=*/ QLessThanOperatorForType::lessThan, - /*.debugStream=*/ QDebugStreamOperatorForType::debugStream, - /*.dataStreamOut=*/ QDataStreamOperatorForType::dataStreamOut, - /*.dataStreamIn=*/ QDataStreamOperatorForType::dataStreamIn, - /*.legacyRegisterOp=*/ QMetaTypeForType::getLegacyRegister() - }; - } - -#ifdef Q_OS_WIN - // MSVC produces link errors when the metaType is constexpr - static const QMetaTypeInterface metaType; -#else - static constexpr const QMetaTypeInterface metaType = create(); -#endif + static inline constexpr const QMetaTypeInterface metaType = { + /*.revision=*/ 0, + /*.alignment=*/ alignof(T), + /*.size=*/ sizeof(T), + /*.flags=*/ QMetaTypeTypeFlags::Flags, + /*.typeId=*/ BuiltinMetaType::value, + /*.metaObjectFn=*/ MetaObjectForType::metaObjectFunction, + /*.name=*/ QMetaTypeForType::getName(), + /*.defaultCtr=*/ QMetaTypeForType::getDefaultCtr(), + /*.copyCtr=*/ QMetaTypeForType::getCopyCtr(), + /*.moveCtr=*/ QMetaTypeForType::getMoveCtr(), + /*.dtor=*/ QMetaTypeForType::getDtor(), + /*.equals=*/ QEqualityOperatorForType::equals, + /*.lessThan=*/ QLessThanOperatorForType::lessThan, + /*.debugStream=*/ QDebugStreamOperatorForType::debugStream, + /*.dataStreamOut=*/ QDataStreamOperatorForType::dataStreamOut, + /*.dataStreamIn=*/ QDataStreamOperatorForType::dataStreamIn, + /*.legacyRegisterOp=*/ QMetaTypeForType::getLegacyRegister() + }; }; -#ifdef Q_OS_WIN -template -const QMetaTypeInterface QMetaTypeInterfaceWrapper::metaType - = QMetaTypeInterfaceWrapper::create(); -#endif template<> class QMetaTypeInterfaceWrapper @@ -2272,7 +2268,7 @@ public: /*.size=*/ 0, /*.flags=*/ 0, /*.typeId=*/ BuiltinMetaType::value, - /*.metaObject=*/ nullptr, + /*.metaObjectFn=*/ nullptr, /*.name=*/ "void", /*.defaultCtr=*/ nullptr, /*.copyCtr=*/ nullptr, @@ -2395,7 +2391,7 @@ constexpr QMetaType::TypeFlags QMetaType::flags() const constexpr const QMetaObject *QMetaType::metaObject() const { - return d_ptr ? d_ptr->metaObject : nullptr; + return d_ptr && d_ptr->metaObjectFn ? d_ptr->metaObjectFn(d_ptr) : nullptr; } template diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index f3d95285dc..503e9b0e33 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -413,19 +413,30 @@ void tst_QMetaType::registerGadget(const char *name, const QListd.static_metacall = &GadgetsStaticMetacallFunction; meta->d.superdata = nullptr; const auto flags = QMetaType::IsGadget | QMetaType::NeedsConstruction | QMetaType::NeedsDestruction; - using TypeInfo = QtPrivate::QMetaTypeInterface; + struct TypeInfo : public QtPrivate::QMetaTypeInterface + { + QMetaObject *mo; + }; + auto typeInfo = new TypeInfo { - 0, alignof(GenericGadgetType), sizeof(GenericGadgetType), uint(flags), 0, meta, name, - [](const TypeInfo *self, void *where) { GadgetTypedConstructor(self->typeId, where, nullptr); }, - [](const TypeInfo *self, void *where, const void *copy) { GadgetTypedConstructor(self->typeId, where, copy); }, - [](const TypeInfo *self, void *where, void *copy) { GadgetTypedConstructor(self->typeId, where, copy); }, - [](const TypeInfo *self, void *ptr) { GadgetTypedDestructor(self->typeId, ptr); }, - nullptr, - nullptr, - nullptr, - GadgetSaveOperator, - GadgetLoadOperator, - nullptr + { + 0, alignof(GenericGadgetType), sizeof(GenericGadgetType), uint(flags), 0, + [](const QtPrivate::QMetaTypeInterface *self) -> const QMetaObject * { + return reinterpret_cast(self)->mo; + }, + name, + [](const QtPrivate::QMetaTypeInterface *self, void *where) { GadgetTypedConstructor(self->typeId, where, nullptr); }, + [](const QtPrivate::QMetaTypeInterface *self, void *where, const void *copy) { GadgetTypedConstructor(self->typeId, where, copy); }, + [](const QtPrivate::QMetaTypeInterface *self, void *where, void *copy) { GadgetTypedConstructor(self->typeId, where, copy); }, + [](const QtPrivate::QMetaTypeInterface *self, void *ptr) { GadgetTypedDestructor(self->typeId, ptr); }, + nullptr, + nullptr, + nullptr, + GadgetSaveOperator, + GadgetLoadOperator, + nullptr + }, + meta }; QMetaType gadgetMetaType(typeInfo); dynamicGadgetProperties->m_metatype = gadgetMetaType;