diff options
Diffstat (limited to 'src/corelib/kernel/qmetatype.cpp')
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 256 |
1 files changed, 108 insertions, 148 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 311648c33a..294b1fc07c 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -283,10 +283,11 @@ struct DefinedTypesFilter { \value MovableType An instance of a type having this attribute can be safely moved by memcpy. \omitvalue SharedPointerToQObject \omitvalue IsEnumeration - \omitvalue PointerToQObject + \value PointerToQObject This type is a pointer to a derived of QObject \omitvalue WeakPointerToQObject \omitvalue TrackingPointerToQObject \omitvalue WasDeclaredAsMetaType + \value IsGadget This type is a Q_GADGET and it's corresponding QMetaObject can be accessed with QMetaType::metaObject Since 5.5. */ /*! @@ -363,8 +364,19 @@ struct DefinedTypesFilter { /*! \fn const QMetaObject *QMetaType::metaObject() const - \since 5.0 - \internal + \since 5.5 + + return a QMetaObject relative to this type. + + If the type is a pointer type to a subclass of QObject, flags contains + QMetaType::PointerToQObject and this function returns the corresponding QMetaObject. This can + be used to in combinaison with QMetaObject::construct to create QObject of this type. + + If the type is a Q_GADGET, flags contains QMetaType::IsGadget, and this function returns its + QMetaObject. This can be used to retrieve QMetaMethod and QMetaProperty and use them on a + pointer of this type. (given by QVariant::data for example) + + \sa QMetaType::metaObjectForType(), QMetaType::flags() */ /*! @@ -801,7 +813,7 @@ static inline int qMetaTypeStaticType(const char *typeName, int length) { int i = 0; while (types[i].typeName && ((length != types[i].typeNameLength) - || strcmp(typeName, types[i].typeName))) { + || memcmp(typeName, types[i].typeName, length))) { ++i; } return types[i].type; @@ -821,7 +833,7 @@ static int qMetaTypeCustomType_unlocked(const char *typeName, int length) for (int v = 0; v < ct->count(); ++v) { const QCustomTypeInfo &customInfo = ct->at(v); if ((length == customInfo.typeName.size()) - && !strcmp(typeName, customInfo.typeName.constData())) { + && !memcmp(typeName, customInfo.typeName.constData(), length)) { if (customInfo.alias >= 0) return customInfo.alias; return v + QMetaType::User; @@ -870,24 +882,43 @@ int QMetaType::registerType(const char *typeName, Deleter deleter, /*! - \internal - \since 5.0 + \internal + \since 5.0 + \overload + Don't use, kept for binary compatibility - Registers a user type for marshalling, with \a normalizedTypeName, a \a - deleter, a \a creator, a \a destructor, a \a constructor, and - a \a size. Returns the type's handle, or -1 if the type could - not be registered. Note that normalizedTypeName is not checked for - conformance with Qt's normalized format, so it must already - conform. - */ + ### TODO Qt6: remove me +*/ int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, Deleter deleter, Creator creator, Destructor destructor, Constructor constructor, int size, TypeFlags flags, const QMetaObject *metaObject) { + Q_UNUSED(deleter); + Q_UNUSED(creator); + return registerNormalizedType(normalizedTypeName, destructor, constructor, size, flags, metaObject); +} + + +/*! + \internal + \since 5.5 + + Registers a user type for marshalling, with \a normalizedTypeName, + a \a destructor, a \a constructor, and a \a size. Returns the type's + handle, or -1 if the type could not be registered. + + \note normalizedTypeName is not checked for conformance with + Qt's normalized format, so it must already conform. + */ +int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, + Destructor destructor, + Constructor constructor, + int size, TypeFlags flags, const QMetaObject *metaObject) +{ QVector<QCustomTypeInfo> *ct = customTypes(); - if (!ct || normalizedTypeName.isEmpty() || !deleter || !creator || !destructor || !constructor) + if (!ct || normalizedTypeName.isEmpty() || !destructor || !constructor) return -1; int idx = qMetaTypeStaticType(normalizedTypeName.constData(), @@ -902,8 +933,6 @@ int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, if (idx == UnknownType) { QCustomTypeInfo inf; inf.typeName = normalizedTypeName; - inf.creator = creator; - inf.deleter = deleter; #ifndef QT_NO_DATASTREAM inf.loadOp = 0; inf.saveOp = 0; @@ -1004,8 +1033,6 @@ int QMetaType::registerNormalizedTypedef(const NS(QByteArray) &normalizedTypeNam QCustomTypeInfo inf; inf.typeName = normalizedTypeName; inf.alias = aliasId; - inf.creator = 0; - inf.deleter = 0; ct->append(inf); return aliasId; } @@ -1048,9 +1075,8 @@ bool QMetaType::isRegistered(int type) Implementation of QMetaType::type(). */ template <bool tryNormalizedType> -static inline int qMetaTypeTypeImpl(const char *typeName) +static inline int qMetaTypeTypeImpl(const char *typeName, int length) { - int length = qstrlen(typeName); if (!length) return QMetaType::UnknownType; int type = qMetaTypeStaticType(typeName, length); @@ -1080,7 +1106,7 @@ static inline int qMetaTypeTypeImpl(const char *typeName) */ int QMetaType::type(const char *typeName) { - return qMetaTypeTypeImpl</*tryNormalizedType=*/true>(typeName); + return qMetaTypeTypeImpl</*tryNormalizedType=*/true>(typeName, qstrlen(typeName)); } /*! @@ -1092,7 +1118,21 @@ int QMetaType::type(const char *typeName) */ int qMetaTypeTypeInternal(const char *typeName) { - return qMetaTypeTypeImpl</*tryNormalizedType=*/false>(typeName); + return qMetaTypeTypeImpl</*tryNormalizedType=*/false>(typeName, qstrlen(typeName)); +} + +/*! + \since 5.5 + \overload + + Returns a handle to the type called \a typeName, or 0 if there is + no such type. + + \sa isRegistered(), typeName() +*/ +int QMetaType::type(const QT_PREPEND_NAMESPACE(QByteArray) &typeName) +{ + return qMetaTypeTypeImpl</*tryNormalizedType=*/true>(typeName.constData(), typeName.size()); } #ifndef QT_NO_DATASTREAM @@ -1541,60 +1581,6 @@ bool QMetaType::load(QDataStream &stream, int type, void *data) return true; } #endif // QT_NO_DATASTREAM -namespace { -class TypeCreator { - template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> - struct CreatorImpl { - static void *Create(const int /* type */, const void *copy) - { - // Using QMetaTypeFunctionHelper<T>::Create adds function call cost, even if it is a template (gcc). - // This "copy" check is moved out from the switcher by compiler (at least by gcc) - return copy ? new T(*static_cast<const T*>(copy)) : new T(); - } - }; - template<typename T> - struct CreatorImpl<T, /* IsAcceptedType = */ false> { - static void *Create(const int type, const void *copy) - { - if (QModulesPrivate::QTypeModuleInfo<T>::IsGui) { - if (Q_LIKELY(qMetaTypeGuiHelper)) - return qMetaTypeGuiHelper[type - QMetaType::FirstGuiType].creator(copy); - } - if (QModulesPrivate::QTypeModuleInfo<T>::IsWidget) { - if (Q_LIKELY(qMetaTypeWidgetsHelper)) - return qMetaTypeWidgetsHelper[type - QMetaType::FirstWidgetsType].creator(copy); - } - // This point can be reached only for known types that definition is not available, for example - // in bootstrap mode. We have no other choice then ignore it. - return 0; - } - }; -public: - TypeCreator(const int type) - : m_type(type) - {} - - template<typename T> - void *delegate(const T *copy) { return CreatorImpl<T>::Create(m_type, copy); } - void *delegate(const void*) { return 0; } - void *delegate(const QMetaTypeSwitcher::UnknownType *) { return 0; } - void *delegate(const QMetaTypeSwitcher::NotBuiltinType *copy) - { - QMetaType::Creator creator; - const QVector<QCustomTypeInfo> * const ct = customTypes(); - { - QReadLocker locker(customTypesLock()); - if (Q_UNLIKELY(m_type < QMetaType::User || !ct || ct->count() <= m_type - QMetaType::User)) - return 0; - creator = ct->at(m_type - QMetaType::User).creator; - } - Q_ASSERT_X(creator, "void *QMetaType::create(int type, const void *copy)", "The type was not properly registered"); - return creator(copy); - } -private: - const int m_type; -}; -} // namespace /*! Returns a copy of \a copy, assuming it is of type \a type. If \a @@ -1604,65 +1590,11 @@ private: */ void *QMetaType::create(int type, const void *copy) { - TypeCreator typeCreator(type); - return QMetaTypeSwitcher::switcher<void*>(typeCreator, type, copy); + QMetaType info(type); + int size = info.sizeOf(); + return info.construct(operator new(size), copy); } -namespace { -class TypeDestroyer { - template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> - struct DestroyerImpl { - static void Destroy(const int /* type */, void *where) { QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Delete(where); } - }; - template<typename T> - struct DestroyerImpl<T, /* IsAcceptedType = */ false> { - static void Destroy(const int type, void *where) - { - if (QModulesPrivate::QTypeModuleInfo<T>::IsGui) { - if (Q_LIKELY(qMetaTypeGuiHelper)) - qMetaTypeGuiHelper[type - QMetaType::FirstGuiType].deleter(where); - return; - } - if (QModulesPrivate::QTypeModuleInfo<T>::IsWidget) { - if (Q_LIKELY(qMetaTypeWidgetsHelper)) - qMetaTypeWidgetsHelper[type - QMetaType::FirstWidgetsType].deleter(where); - return; - } - // This point can be reached only for known types that definition is not available, for example - // in bootstrap mode. We have no other choice then ignore it. - } - }; -public: - TypeDestroyer(const int type) - : m_type(type) - {} - - template<typename T> - void delegate(const T *where) { DestroyerImpl<T>::Destroy(m_type, const_cast<T*>(where)); } - void delegate(const void *) {} - void delegate(const QMetaTypeSwitcher::UnknownType*) {} - void delegate(const QMetaTypeSwitcher::NotBuiltinType *where) { customTypeDestroyer(m_type, (void*)where); } - -private: - static void customTypeDestroyer(const int type, void *where) - { - QMetaType::Destructor deleter; - const QVector<QCustomTypeInfo> * const ct = customTypes(); - { - QReadLocker locker(customTypesLock()); - if (Q_UNLIKELY(type < QMetaType::User || !ct || ct->count() <= type - QMetaType::User)) - return; - deleter = ct->at(type - QMetaType::User).deleter; - } - Q_ASSERT_X(deleter, "void QMetaType::destroy(int type, void *data)", "The type was not properly registered"); - deleter(where); - } - - const int m_type; -}; -} // namespace - - /*! Destroys the \a data, assuming it is of the \a type given. @@ -1670,8 +1602,9 @@ private: */ void QMetaType::destroy(int type, void *data) { - TypeDestroyer deleter(type); - QMetaTypeSwitcher::switcher<void>(deleter, type, data); + QMetaType info(type); + info.destruct(data); + operator delete(data); } namespace { @@ -1969,8 +1902,27 @@ public: MetaObject(const int type) : m_type(type) {} + + template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> + struct MetaObjectImpl + { + static const QMetaObject *MetaObject(int /*type*/) + { return QtPrivate::MetaObjectForType<T>::value(); } + }; template<typename T> - const QMetaObject *delegate(const T*) { return QtPrivate::MetaObjectForType<T>::value(); } + struct MetaObjectImpl<T, /* IsAcceptedType = */ false> + { + static const QMetaObject *MetaObject(int type) { + if (QModulesPrivate::QTypeModuleInfo<T>::IsGui) + return Q_LIKELY(qMetaTypeGuiHelper) ? qMetaTypeGuiHelper[type - QMetaType::FirstGuiType].metaObject : 0; + if (QModulesPrivate::QTypeModuleInfo<T>::IsWidget) + return Q_LIKELY(qMetaTypeWidgetsHelper) ? qMetaTypeWidgetsHelper[type - QMetaType::FirstWidgetsType].metaObject : 0; + return 0; + } + }; + + template <typename T> + const QMetaObject *delegate(const T *) { return MetaObjectImpl<T>::MetaObject(m_type); } const QMetaObject *delegate(const void*) { return 0; } const QMetaObject *delegate(const QMetaTypeSwitcher::UnknownType*) { return 0; } const QMetaObject *delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return customMetaObject(m_type); } @@ -1993,7 +1945,9 @@ private: /*! \since 5.0 - Returns QMetaObject of a given \a type, if the \a type is a pointer to type derived from QObject. + returns QMetaType::metaObject for \a type + + \sa metaObject() */ const QMetaObject *QMetaType::metaObjectForType(int type) { @@ -2200,10 +2154,10 @@ QMetaType QMetaType::typeInfo(const int type) { TypeInfo typeInfo(type); QMetaTypeSwitcher::switcher<void>(typeInfo, type, 0); - return typeInfo.info.creator ? QMetaType(QMetaType::NoExtensionFlags + return typeInfo.info.constructor ? QMetaType(static_cast<ExtensionFlag>(QMetaType::CreateEx | QMetaType::DestroyEx) , static_cast<const QMetaTypeInterface *>(0) // typeInfo::info is a temporary variable, we can't return address of it. - , typeInfo.info.creator - , typeInfo.info.deleter + , 0 // unused + , 0 // unused , typeInfo.info.saveOp , typeInfo.info.loadOp , typeInfo.info.constructor @@ -2245,8 +2199,8 @@ QMetaType::QMetaType(const int typeId) Copy constructs a QMetaType object. */ QMetaType::QMetaType(const QMetaType &other) - : m_creator(other.m_creator) - , m_deleter(other.m_deleter) + : m_creator_unused(other.m_creator_unused) + , m_deleter_unused(other.m_deleter_unused) , m_saveOp(other.m_saveOp) , m_loadOp(other.m_loadOp) , m_constructor(other.m_constructor) @@ -2261,8 +2215,8 @@ QMetaType::QMetaType(const QMetaType &other) QMetaType &QMetaType::operator =(const QMetaType &other) { - m_creator = other.m_creator; - m_deleter = other.m_deleter; + m_creator_unused = other.m_creator_unused; + m_deleter_unused = other.m_deleter_unused; m_saveOp = other.m_saveOp; m_loadOp = other.m_loadOp; m_constructor = other.m_constructor; @@ -2311,11 +2265,14 @@ void QMetaType::dtor() Method used for future binary compatible extensions. The function may be called during QMetaType::create to force library call from inlined code. + + ### TODO Qt6 remove the extension */ void *QMetaType::createExtended(const void *copy) const { - Q_UNUSED(copy); - return 0; + if (m_typeId == QMetaType::UnknownType) + return 0; + return m_constructor(operator new(m_size), copy); } /*! @@ -2324,10 +2281,13 @@ void *QMetaType::createExtended(const void *copy) const Method used for future binary compatible extensions. The function may be called during QMetaType::destroy to force library call from inlined code. + + ### TODO Qt6 remove the extension */ void QMetaType::destroyExtended(void *data) const { - Q_UNUSED(data); + m_destructor(data); + operator delete(data); } /*! |