Add a metatype helper class for Qt Core

This helps get better symmetry with the other modules,
and to unify the code paths for both conversion and
retrieving the interface for static types.

Change-Id: Icbd20de2563f36e3de20d826323acd057734abfb
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-07-13 10:59:48 +02:00
parent f44d2ea1cc
commit 1d7a9fc9b4
4 changed files with 31 additions and 19 deletions

View File

@ -852,11 +852,29 @@ static const struct { const char * typeName; int typeNameLength; int type; } typ
{nullptr, 0, QMetaType::UnknownType}
};
static const struct : QMetaTypeModuleHelper
{
QtPrivate::QMetaTypeInterface *interfaceForType(int type) const override {
switch (type) {
QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_CORE_CLASS(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_CORE_POINTER(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_CORE_TEMPLATE(QT_METATYPE_CONVERT_ID_TO_TYPE)
default:
return nullptr;
}
}
} metatypeHelper;
static const QMetaTypeModuleHelper *qMetaTypeCoreHelper = &metatypeHelper;
Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeGuiHelper = nullptr;
Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeWidgetsHelper = nullptr;
static const QMetaTypeModuleHelper *qModuleHelperForType(int type)
{
if (type <= QMetaType::LastCoreType)
return qMetaTypeCoreHelper;
if (type >= QMetaType::FirstGuiType && type <= QMetaType::LastGuiType)
return qMetaTypeGuiHelper;
else if (type >= QMetaType::FirstWidgetsType && type <= QMetaType::LastWidgetsType)
@ -1589,24 +1607,19 @@ const QMetaObject *QMetaType::metaObjectForType(int type)
static QtPrivate::QMetaTypeInterface *interfaceForType(int typeId)
{
QtPrivate::QMetaTypeInterface *iface = nullptr;
if (typeId >= QMetaType::User) {
if (auto reg = customTypeRegistry())
return reg->getCustomType(typeId);
iface = reg->getCustomType(typeId);
} else {
if (auto moduleHelper = qModuleHelperForType(typeId))
iface = moduleHelper->interfaceForType(typeId);
}
if (auto moduleHelper = qModuleHelperForType(typeId))
return moduleHelper->interfaceForType(typeId);
switch (typeId) {
QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_CORE_CLASS(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_CORE_POINTER(QT_METATYPE_CONVERT_ID_TO_TYPE)
QT_FOR_EACH_STATIC_CORE_TEMPLATE(QT_METATYPE_CONVERT_ID_TO_TYPE)
default:
if (typeId != QMetaType::UnknownType)
qWarning("Trying to construct an instance of an invalid type, type id: %i", typeId);
return nullptr;
}
if (!iface && typeId != QMetaType::UnknownType)
qWarning("Trying to construct an instance of an invalid type, type id: %i", typeId);
return iface;
}
/*!

View File

@ -155,6 +155,10 @@ public:
virtual bool convert(const void *, int, void *, int) const { return false; }
};
extern Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeGuiHelper;
extern Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeWidgetsHelper;
namespace QtMetaTypePrivate {
template<typename T>
struct TypeDefinition {

View File

@ -178,8 +178,6 @@ static const struct : QMetaTypeModuleHelper
} // namespace used to hide QVariant handler
extern Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeGuiHelper;
void qRegisterGuiVariant()
{
qMetaTypeGuiHelper = &qVariantGuiHelper;

View File

@ -64,11 +64,8 @@ static const struct : QMetaTypeModuleHelper
} // namespace
extern Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeWidgetsHelper;
void qRegisterWidgetsVariant()
{
qRegisterMetaType<QWidget*>();
qMetaTypeWidgetsHelper = &qVariantWidgetsHelper;
}
Q_CONSTRUCTOR_FUNCTION(qRegisterWidgetsVariant)