From 369cb1470d06b01934e616757ad98db2bb9ebb20 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 27 Jul 2020 17:31:27 +0200 Subject: Clean up int based convert() API Pass QMetaType instances instead. Change-Id: I07366cea566fdebf5bb793aa8087f8109216ec0c Reviewed-by: Thiago Macieira --- src/corelib/kernel/qmetaobject.cpp | 2 +- src/corelib/kernel/qmetatype.cpp | 92 +++++++++++++++++++++---------------- src/corelib/kernel/qmetatype.h | 5 +- src/corelib/kernel/qvariant.cpp | 15 +++--- src/corelib/kernel/qvariant.h | 2 +- src/gui/text/qcssparser.cpp | 4 +- src/gui/util/qshadernodesloader.cpp | 10 ++-- 7 files changed, 73 insertions(+), 57 deletions(-) diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index d97373511e..8d4d4d943f 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -3185,7 +3185,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const if (isResettable()) return reset(object); v = QVariant(t, nullptr); - } else if (!v.convert(t.id())) { + } else if (!v.convert(t)) { return false; } } diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index e37629ffa3..42c5c7cb32 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -1714,7 +1714,7 @@ static QMetaEnum metaEnumFromType(QMetaType t) } #endif -static bool convertFromEnum(const void *from, const QMetaType &fromType, void *to, int toTypeId) +static bool convertFromEnum(const QMetaType &fromType, const void *from, QMetaType toType, void *to) { qlonglong ll; if (fromType.flags() & QMetaType::IsUnsignedEnumeration) { @@ -1735,12 +1735,12 @@ static bool convertFromEnum(const void *from, const QMetaType &fromType, void *t default: Q_UNREACHABLE(); } - if (toTypeId == QMetaType::ULongLong) { + if (toType.id() == QMetaType::ULongLong) { *static_cast(to) = ull; return true; } - if (toTypeId != QMetaType::QString && toTypeId != QMetaType::QByteArray) - return QMetaType::convert(&ull, QMetaType::ULongLong, to, toTypeId); + if (toType.id() != QMetaType::QString && toType.id() != QMetaType::QByteArray) + return QMetaType::convert(QMetaType::fromType(), &ull, toType, to); ll = qlonglong(ull); } else { switch (fromType.sizeOf()) { @@ -1759,19 +1759,19 @@ static bool convertFromEnum(const void *from, const QMetaType &fromType, void *t default: Q_UNREACHABLE(); } - if (toTypeId == QMetaType::LongLong) { + if (toType.id() == QMetaType::LongLong) { *static_cast(to) = ll; return true; } - if (toTypeId != QMetaType::QString && toTypeId != QMetaType::QByteArray) - return QMetaType::convert(&ll, QMetaType::LongLong, to, toTypeId); + if (toType.id() != QMetaType::QString && toType.id() != QMetaType::QByteArray) + return QMetaType::convert(QMetaType::fromType(), &ll, toType, to); } - Q_ASSERT(toTypeId == QMetaType::QString || toTypeId == QMetaType::QByteArray); + Q_ASSERT(toType.id() == QMetaType::QString || toType.id() == QMetaType::QByteArray); #ifndef QT_NO_QOBJECT QMetaEnum en = metaEnumFromType(fromType); if (en.isValid()) { const char *key = en.valueToKey(ll); - if (toTypeId == QMetaType::QString) + if (toType.id() == QMetaType::QString) *static_cast(to) = QString::fromUtf8(key); else *static_cast(to) = key; @@ -1781,8 +1781,9 @@ static bool convertFromEnum(const void *from, const QMetaType &fromType, void *t return false; } -static bool convertToEnum(const void *from, int fromTypeId, void *to, const QMetaType &toType) +static bool convertToEnum(QMetaType fromType, const void *from, QMetaType toType, void *to) { + int fromTypeId = fromType.id(); qlonglong value; bool ok = false; #ifndef QT_NO_QOBJECT @@ -1801,7 +1802,7 @@ static bool convertToEnum(const void *from, int fromTypeId, void *to, const QMet value = *static_cast(from); ok = true; } else { - ok = QMetaType::convert(from, fromTypeId, &value, QMetaType::LongLong); + ok = QMetaType::convert(fromType, from, QMetaType::fromType(), &value); } } @@ -1827,10 +1828,10 @@ static bool convertToEnum(const void *from, int fromTypeId, void *to, const QMet } } -static bool convertIterableToVariantList(const void *from, int fromTypeId, void *to) +static bool convertIterableToVariantList(QMetaType fromType, const void *from, void *to) { const QMetaType::ConverterFunction * const f = - customTypesConversionRegistry()->function(qMakePair(fromTypeId, + customTypesConversionRegistry()->function(qMakePair(fromType.id(), qMetaTypeId())); if (!f) return false; @@ -1847,10 +1848,10 @@ static bool convertIterableToVariantList(const void *from, int fromTypeId, void return true; } -static bool convertIterableToVariantMap(const void *from, int fromTypeId, void *to) +static bool convertIterableToVariantMap(QMetaType fromType, const void *from, void *to) { const QMetaType::ConverterFunction * const f = - customTypesConversionRegistry()->function(qMakePair(fromTypeId, + customTypesConversionRegistry()->function(qMakePair(fromType.id(), qMetaTypeId())); if (!f) return false; @@ -1866,10 +1867,10 @@ static bool convertIterableToVariantMap(const void *from, int fromTypeId, void * return true; } -static bool convertIterableToVariantHash(const void *from, int fromTypeId, void *to) +static bool convertIterableToVariantHash(QMetaType fromType, const void *from, void *to) { const QMetaType::ConverterFunction * const f = - customTypesConversionRegistry()->function(qMakePair(fromTypeId, + customTypesConversionRegistry()->function(qMakePair(fromType.id(), qMetaTypeId())); if (!f) return false; @@ -1886,10 +1887,10 @@ static bool convertIterableToVariantHash(const void *from, int fromTypeId, void return true; } -static bool convertIterableToVariantPair(const void *from, int fromTypeId, void *to) +static bool convertIterableToVariantPair(QMetaType fromType, const void *from, void *to) { const QMetaType::ConverterFunction * const f = - customTypesConversionRegistry()->function(qMakePair(fromTypeId, + customTypesConversionRegistry()->function(qMakePair(fromType.id(), qMetaTypeId())); if (!f) return false; @@ -1916,9 +1917,10 @@ static bool convertIterableToVariantPair(const void *from, int fromTypeId, void return true; } -static bool convertToSequentialIterable(const void *from, int fromTypeId, void *to) +static bool convertToSequentialIterable(QMetaType fromType, const void *from, void *to) { using namespace QtMetaTypePrivate; + int fromTypeId = fromType.id(); QSequentialIterable &i = *static_cast(to); if (fromTypeId == QMetaType::QVariantList) { @@ -1934,28 +1936,28 @@ static bool convertToSequentialIterable(const void *from, int fromTypeId, void * return true; } QSequentialIterableImpl impl; - if (QMetaType::convert(from, fromTypeId, &impl, qMetaTypeId())) { + if (QMetaType::convert(fromType, from, QMetaType::fromType(), &impl)) { i = QSequentialIterable(impl); return true; } return false; } -static bool convertToAssociativeIterable(const void *from, int fromTypeId, void *to) +static bool convertToAssociativeIterable(QMetaType fromType, const void *from, void *to) { using namespace QtMetaTypePrivate; QAssociativeIterable &i = *static_cast(to); - if (fromTypeId == QMetaType::QVariantMap) { + if (fromType.id() == QMetaType::QVariantMap) { i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast(from))); return true; } - if (fromTypeId == QMetaType::QVariantHash) { + if (fromType.id() == QMetaType::QVariantHash) { i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast(from))); return true; } QAssociativeIterableImpl impl; - if (QMetaType::convert(from, fromTypeId, &impl, qMetaTypeId())) { + if (QMetaType::convert(fromType, from, QMetaType::fromType(), &impl)) { i = QAssociativeIterable(impl); return true; } @@ -1975,22 +1977,34 @@ static bool canConvertMetaObject(const QMetaType &fromType, const QMetaType &toT #endif /*! + \fn bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId) + \obsolete + Converts the object at \a from from \a fromTypeId to the preallocated space at \a to typed \a toTypeId. Returns \c true, if the conversion succeeded, otherwise false. \since 5.2 */ -bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId) + +/*! + Converts the object at \a from from \a fromType to the preallocated space at \a to + typed \a toType. Returns \c true, if the conversion succeeded, otherwise false. + \since 5.2 +*/ +bool QMetaType::convert(QMetaType fromType, const void *from, QMetaType toType, void *to) { - if (fromTypeId == UnknownType || toTypeId == UnknownType) + if (!fromType.isValid() || !toType.isValid()) return false; - if (fromTypeId == toTypeId) { + if (fromType == toType) { // just make a copy - QMetaType(fromTypeId).destruct(to); - QMetaType(fromTypeId).construct(to, from); + fromType.destruct(to); + fromType.construct(to, from); return true; } + int fromTypeId = fromType.id(); + int toTypeId = toType.id(); + if (auto moduleHelper = qModuleHelperForType(qMax(fromTypeId, toTypeId))) { if (moduleHelper->convert(from, fromTypeId, to, toTypeId)) return true; @@ -2000,12 +2014,10 @@ bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId if (f) return (*f)(from, to); - QMetaType fromType(fromTypeId); if (fromType.flags() & QMetaType::IsEnumeration) - return convertFromEnum(from, fromType, to, toTypeId); - QMetaType toType(toTypeId); + return convertFromEnum(fromType, from, toType, to); if (toType.flags() & QMetaType::IsEnumeration) - return convertToEnum(from, fromTypeId, to, toType); + return convertToEnum(fromType, from, toType, to); if (toTypeId == Nullptr) { *static_cast(to) = nullptr; if (fromType.flags() & QMetaType::IsPointer) { @@ -2015,23 +2027,23 @@ bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId } // handle iterables - if (toTypeId == QVariantList && convertIterableToVariantList(from, fromTypeId, to)) + if (toTypeId == QVariantList && convertIterableToVariantList(fromType, from, to)) return true; - if (toTypeId == QVariantMap && convertIterableToVariantMap(from, fromTypeId, to)) + if (toTypeId == QVariantMap && convertIterableToVariantMap(fromType, from, to)) return true; - if (toTypeId == QVariantHash && convertIterableToVariantHash(from, fromTypeId, to)) + if (toTypeId == QVariantHash && convertIterableToVariantHash(fromType, from, to)) return true; - if (toTypeId == QVariantPair && convertIterableToVariantPair(from, fromTypeId, to)) + if (toTypeId == QVariantPair && convertIterableToVariantPair(fromType, from, to)) return true; if (toTypeId == qMetaTypeId()) - return convertToSequentialIterable(from, fromTypeId, to); + return convertToSequentialIterable(fromType, from, to); if (toTypeId == qMetaTypeId()) - return convertToAssociativeIterable(from, fromTypeId, to); + return convertToAssociativeIterable(fromType, from, to); #ifndef QT_BOOTSTRAPPED // handle QObject conversion diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index b2f8e640c2..b72b2f69be 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -537,9 +537,12 @@ public: } #endif - static bool convert(const void *from, int fromTypeId, void *to, int toTypeId); + static bool convert(QMetaType fromType, const void *from, QMetaType toType, void *to); static bool canConvert(const QMetaType &fromType, const QMetaType &toType); #if QT_DEPRECATED_SINCE(6, 0) + QT_DEPRECATED_VERSION_6_0 + static bool convert(const void *from, int fromTypeId, void *to, int toTypeId) + { return convert(QMetaType(fromTypeId), from, QMetaType(toTypeId), to); } QT_DEPRECATED_VERSION_6_0 static bool compare(const void *lhs, const void *rhs, int typeId, int *result) { diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 7f75e1884f..b21ded3186 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -1765,15 +1765,15 @@ QBitArray QVariant::toBitArray() const template inline T qNumVariantToHelper(const QVariant::Private &d, bool *ok, const T& val) { - const uint t = qMetaTypeId(); + QMetaType t = QMetaType::fromType(); if (ok) *ok = true; - if (d.typeId() == t) + if (d.type() == t) return val; T ret = 0; - bool success = QMetaType::convert(d.storage(), d.typeId(), &ret, t); + bool success = QMetaType::convert(d.type(), d.storage(), t, &ret); if (ok) *ok = success; return ret; @@ -1871,11 +1871,12 @@ qulonglong QVariant::toULongLong(bool *ok) const */ bool QVariant::toBool() const { - if (d.type() == QMetaType::fromType()) + auto boolType = QMetaType::fromType(); + if (d.type() == boolType) return d.get(); bool res = false; - QMetaType::convert(constData(), d.typeId(), &res, QMetaType::Bool); + QMetaType::convert(d.type(), constData(), boolType, &res); return res; } @@ -2027,7 +2028,7 @@ bool QVariant::convert(QMetaType targetType) if (oldValue.d.is_null && oldValue.d.typeId() != QMetaType::Nullptr) return false; - bool ok = QMetaType::convert(oldValue.constData(), oldValue.d.typeId(), data(), targetType.id()); + bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data()); d.is_null = !ok; return ok; } @@ -2039,7 +2040,7 @@ bool QVariant::convert(QMetaType targetType) */ bool QVariant::convert(const int type, void *ptr) const { - return QMetaType::convert(constData(), d.typeId(), ptr, type); + return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr); } diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 48ea3a9a03..e9509e637a 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -698,7 +698,7 @@ template inline T qvariant_cast(const QVariant &v) return v.d.get(); T t{}; - QMetaType::convert(v.constData(), v.userType(), &t, qMetaTypeId()); + QMetaType::convert(v.metaType(), v.constData(), targetType, &t); return t; } diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index 9b1c67c8d5..b57f933c37 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -710,7 +710,7 @@ static Qt::Alignment parseAlignment(const QCss::Value *values, int count) static ColorData parseColorValue(QCss::Value v) { if (v.type == Value::Identifier || v.type == Value::String) { - v.variant.convert(QMetaType::QColor); + v.variant.convert(QMetaType::fromType()); v.type = Value::Color; } @@ -2759,7 +2759,7 @@ bool Parser::parseTerm(Value *value) switch (lookup()) { case NUMBER: value->type = Value::Number; - value->variant.convert(QMetaType::Double); + value->variant.convert(QMetaType::fromType()); break; case PERCENTAGE: value->type = Value::Percentage; diff --git a/src/gui/util/qshadernodesloader.cpp b/src/gui/util/qshadernodesloader.cpp index a29b0925e1..e6dcc8799b 100644 --- a/src/gui/util/qshadernodesloader.cpp +++ b/src/gui/util/qshadernodesloader.cpp @@ -160,21 +160,21 @@ void QShaderNodesLoader::load(const QJsonObject &prototypesObject) if (parameterValue.isObject()) { const QJsonObject parameterObject = parameterValue.toObject(); const QString type = parameterObject.value(QStringLiteral("type")).toString(); - const int typeId = QMetaType::fromName(type.toUtf8()).id(); + const auto metaType = QMetaType::fromName(type.toUtf8()); const QString value = parameterObject.value(QStringLiteral("value")).toString(); auto variant = QVariant(value); - if (QMetaType(typeId).flags() & QMetaType::IsEnumeration) { - const QMetaObject *metaObject = QMetaType(typeId).metaObject(); + if (metaType.flags() & QMetaType::IsEnumeration) { + const QMetaObject *metaObject = metaType.metaObject(); const char *className = metaObject->className(); const QByteArray enumName = type.mid(static_cast(qstrlen(className)) + 2).toUtf8(); const QMetaEnum metaEnum = metaObject->enumerator(metaObject->indexOfEnumerator(enumName)); const int enumValue = metaEnum.keyToValue(value.toUtf8()); variant = QVariant(enumValue); - variant.convert(typeId); + variant.convert(metaType); } else { - variant.convert(typeId); + variant.convert(metaType); } node.setParameter(parameterName, variant); } else { -- cgit v1.2.3