Clean up int based convert() API

Pass QMetaType instances instead.

Change-Id: I07366cea566fdebf5bb793aa8087f8109216ec0c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-07-27 17:31:27 +02:00
parent e7e7540aec
commit 369cb1470d
7 changed files with 73 additions and 57 deletions

View File

@ -3185,7 +3185,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
if (isResettable()) if (isResettable())
return reset(object); return reset(object);
v = QVariant(t, nullptr); v = QVariant(t, nullptr);
} else if (!v.convert(t.id())) { } else if (!v.convert(t)) {
return false; return false;
} }
} }

View File

@ -1714,7 +1714,7 @@ static QMetaEnum metaEnumFromType(QMetaType t)
} }
#endif #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; qlonglong ll;
if (fromType.flags() & QMetaType::IsUnsignedEnumeration) { if (fromType.flags() & QMetaType::IsUnsignedEnumeration) {
@ -1735,12 +1735,12 @@ static bool convertFromEnum(const void *from, const QMetaType &fromType, void *t
default: default:
Q_UNREACHABLE(); Q_UNREACHABLE();
} }
if (toTypeId == QMetaType::ULongLong) { if (toType.id() == QMetaType::ULongLong) {
*static_cast<qulonglong *>(to) = ull; *static_cast<qulonglong *>(to) = ull;
return true; return true;
} }
if (toTypeId != QMetaType::QString && toTypeId != QMetaType::QByteArray) if (toType.id() != QMetaType::QString && toType.id() != QMetaType::QByteArray)
return QMetaType::convert(&ull, QMetaType::ULongLong, to, toTypeId); return QMetaType::convert(QMetaType::fromType<qulonglong>(), &ull, toType, to);
ll = qlonglong(ull); ll = qlonglong(ull);
} else { } else {
switch (fromType.sizeOf()) { switch (fromType.sizeOf()) {
@ -1759,19 +1759,19 @@ static bool convertFromEnum(const void *from, const QMetaType &fromType, void *t
default: default:
Q_UNREACHABLE(); Q_UNREACHABLE();
} }
if (toTypeId == QMetaType::LongLong) { if (toType.id() == QMetaType::LongLong) {
*static_cast<qlonglong *>(to) = ll; *static_cast<qlonglong *>(to) = ll;
return true; return true;
} }
if (toTypeId != QMetaType::QString && toTypeId != QMetaType::QByteArray) if (toType.id() != QMetaType::QString && toType.id() != QMetaType::QByteArray)
return QMetaType::convert(&ll, QMetaType::LongLong, to, toTypeId); return QMetaType::convert(QMetaType::fromType<qlonglong>(), &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 #ifndef QT_NO_QOBJECT
QMetaEnum en = metaEnumFromType(fromType); QMetaEnum en = metaEnumFromType(fromType);
if (en.isValid()) { if (en.isValid()) {
const char *key = en.valueToKey(ll); const char *key = en.valueToKey(ll);
if (toTypeId == QMetaType::QString) if (toType.id() == QMetaType::QString)
*static_cast<QString *>(to) = QString::fromUtf8(key); *static_cast<QString *>(to) = QString::fromUtf8(key);
else else
*static_cast<QByteArray *>(to) = key; *static_cast<QByteArray *>(to) = key;
@ -1781,8 +1781,9 @@ static bool convertFromEnum(const void *from, const QMetaType &fromType, void *t
return false; 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; qlonglong value;
bool ok = false; bool ok = false;
#ifndef QT_NO_QOBJECT #ifndef QT_NO_QOBJECT
@ -1801,7 +1802,7 @@ static bool convertToEnum(const void *from, int fromTypeId, void *to, const QMet
value = *static_cast<const qlonglong *>(from); value = *static_cast<const qlonglong *>(from);
ok = true; ok = true;
} else { } else {
ok = QMetaType::convert(from, fromTypeId, &value, QMetaType::LongLong); ok = QMetaType::convert(fromType, from, QMetaType::fromType<qlonglong>(), &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 = const QMetaType::ConverterFunction * const f =
customTypesConversionRegistry()->function(qMakePair(fromTypeId, customTypesConversionRegistry()->function(qMakePair(fromType.id(),
qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())); qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>()));
if (!f) if (!f)
return false; return false;
@ -1847,10 +1848,10 @@ static bool convertIterableToVariantList(const void *from, int fromTypeId, void
return true; 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 = const QMetaType::ConverterFunction * const f =
customTypesConversionRegistry()->function(qMakePair(fromTypeId, customTypesConversionRegistry()->function(qMakePair(fromType.id(),
qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())); qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>()));
if (!f) if (!f)
return false; return false;
@ -1866,10 +1867,10 @@ static bool convertIterableToVariantMap(const void *from, int fromTypeId, void *
return true; 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 = const QMetaType::ConverterFunction * const f =
customTypesConversionRegistry()->function(qMakePair(fromTypeId, customTypesConversionRegistry()->function(qMakePair(fromType.id(),
qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())); qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>()));
if (!f) if (!f)
return false; return false;
@ -1886,10 +1887,10 @@ static bool convertIterableToVariantHash(const void *from, int fromTypeId, void
return true; 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 = const QMetaType::ConverterFunction * const f =
customTypesConversionRegistry()->function(qMakePair(fromTypeId, customTypesConversionRegistry()->function(qMakePair(fromType.id(),
qMetaTypeId<QtMetaTypePrivate::QPairVariantInterfaceImpl>())); qMetaTypeId<QtMetaTypePrivate::QPairVariantInterfaceImpl>()));
if (!f) if (!f)
return false; return false;
@ -1916,9 +1917,10 @@ static bool convertIterableToVariantPair(const void *from, int fromTypeId, void
return true; 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; using namespace QtMetaTypePrivate;
int fromTypeId = fromType.id();
QSequentialIterable &i = *static_cast<QSequentialIterable *>(to); QSequentialIterable &i = *static_cast<QSequentialIterable *>(to);
if (fromTypeId == QMetaType::QVariantList) { if (fromTypeId == QMetaType::QVariantList) {
@ -1934,28 +1936,28 @@ static bool convertToSequentialIterable(const void *from, int fromTypeId, void *
return true; return true;
} }
QSequentialIterableImpl impl; QSequentialIterableImpl impl;
if (QMetaType::convert(from, fromTypeId, &impl, qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) { if (QMetaType::convert(fromType, from, QMetaType::fromType<QtMetaTypePrivate::QSequentialIterableImpl>(), &impl)) {
i = QSequentialIterable(impl); i = QSequentialIterable(impl);
return true; return true;
} }
return false; 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; using namespace QtMetaTypePrivate;
QAssociativeIterable &i = *static_cast<QAssociativeIterable *>(to); QAssociativeIterable &i = *static_cast<QAssociativeIterable *>(to);
if (fromTypeId == QMetaType::QVariantMap) { if (fromType.id() == QMetaType::QVariantMap) {
i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast<const QVariantMap *>(from))); i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast<const QVariantMap *>(from)));
return true; return true;
} }
if (fromTypeId == QMetaType::QVariantHash) { if (fromType.id() == QMetaType::QVariantHash) {
i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast<const QVariantHash *>(from))); i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast<const QVariantHash *>(from)));
return true; return true;
} }
QAssociativeIterableImpl impl; QAssociativeIterableImpl impl;
if (QMetaType::convert(from, fromTypeId, &impl, qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) { if (QMetaType::convert(fromType, from, QMetaType::fromType<QtMetaTypePrivate::QAssociativeIterableImpl>(), &impl)) {
i = QAssociativeIterable(impl); i = QAssociativeIterable(impl);
return true; return true;
} }
@ -1975,22 +1977,34 @@ static bool canConvertMetaObject(const QMetaType &fromType, const QMetaType &toT
#endif #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 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. typed \a toTypeId. Returns \c true, if the conversion succeeded, otherwise false.
\since 5.2 \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; return false;
if (fromTypeId == toTypeId) { if (fromType == toType) {
// just make a copy // just make a copy
QMetaType(fromTypeId).destruct(to); fromType.destruct(to);
QMetaType(fromTypeId).construct(to, from); fromType.construct(to, from);
return true; return true;
} }
int fromTypeId = fromType.id();
int toTypeId = toType.id();
if (auto moduleHelper = qModuleHelperForType(qMax(fromTypeId, toTypeId))) { if (auto moduleHelper = qModuleHelperForType(qMax(fromTypeId, toTypeId))) {
if (moduleHelper->convert(from, fromTypeId, to, toTypeId)) if (moduleHelper->convert(from, fromTypeId, to, toTypeId))
return true; return true;
@ -2000,12 +2014,10 @@ bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId
if (f) if (f)
return (*f)(from, to); return (*f)(from, to);
QMetaType fromType(fromTypeId);
if (fromType.flags() & QMetaType::IsEnumeration) if (fromType.flags() & QMetaType::IsEnumeration)
return convertFromEnum(from, fromType, to, toTypeId); return convertFromEnum(fromType, from, toType, to);
QMetaType toType(toTypeId);
if (toType.flags() & QMetaType::IsEnumeration) if (toType.flags() & QMetaType::IsEnumeration)
return convertToEnum(from, fromTypeId, to, toType); return convertToEnum(fromType, from, toType, to);
if (toTypeId == Nullptr) { if (toTypeId == Nullptr) {
*static_cast<std::nullptr_t *>(to) = nullptr; *static_cast<std::nullptr_t *>(to) = nullptr;
if (fromType.flags() & QMetaType::IsPointer) { if (fromType.flags() & QMetaType::IsPointer) {
@ -2015,23 +2027,23 @@ bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId
} }
// handle iterables // handle iterables
if (toTypeId == QVariantList && convertIterableToVariantList(from, fromTypeId, to)) if (toTypeId == QVariantList && convertIterableToVariantList(fromType, from, to))
return true; return true;
if (toTypeId == QVariantMap && convertIterableToVariantMap(from, fromTypeId, to)) if (toTypeId == QVariantMap && convertIterableToVariantMap(fromType, from, to))
return true; return true;
if (toTypeId == QVariantHash && convertIterableToVariantHash(from, fromTypeId, to)) if (toTypeId == QVariantHash && convertIterableToVariantHash(fromType, from, to))
return true; return true;
if (toTypeId == QVariantPair && convertIterableToVariantPair(from, fromTypeId, to)) if (toTypeId == QVariantPair && convertIterableToVariantPair(fromType, from, to))
return true; return true;
if (toTypeId == qMetaTypeId<QSequentialIterable>()) if (toTypeId == qMetaTypeId<QSequentialIterable>())
return convertToSequentialIterable(from, fromTypeId, to); return convertToSequentialIterable(fromType, from, to);
if (toTypeId == qMetaTypeId<QAssociativeIterable>()) if (toTypeId == qMetaTypeId<QAssociativeIterable>())
return convertToAssociativeIterable(from, fromTypeId, to); return convertToAssociativeIterable(fromType, from, to);
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
// handle QObject conversion // handle QObject conversion

View File

@ -537,9 +537,12 @@ public:
} }
#endif #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); static bool canConvert(const QMetaType &fromType, const QMetaType &toType);
#if QT_DEPRECATED_SINCE(6, 0) #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 QT_DEPRECATED_VERSION_6_0
static bool compare(const void *lhs, const void *rhs, int typeId, int *result) static bool compare(const void *lhs, const void *rhs, int typeId, int *result)
{ {

View File

@ -1765,15 +1765,15 @@ QBitArray QVariant::toBitArray() const
template <typename T> template <typename T>
inline T qNumVariantToHelper(const QVariant::Private &d, bool *ok, const T& val) inline T qNumVariantToHelper(const QVariant::Private &d, bool *ok, const T& val)
{ {
const uint t = qMetaTypeId<T>(); QMetaType t = QMetaType::fromType<T>();
if (ok) if (ok)
*ok = true; *ok = true;
if (d.typeId() == t) if (d.type() == t)
return val; return val;
T ret = 0; 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) if (ok)
*ok = success; *ok = success;
return ret; return ret;
@ -1871,11 +1871,12 @@ qulonglong QVariant::toULongLong(bool *ok) const
*/ */
bool QVariant::toBool() const bool QVariant::toBool() const
{ {
if (d.type() == QMetaType::fromType<bool>()) auto boolType = QMetaType::fromType<bool>();
if (d.type() == boolType)
return d.get<bool>(); return d.get<bool>();
bool res = false; bool res = false;
QMetaType::convert(constData(), d.typeId(), &res, QMetaType::Bool); QMetaType::convert(d.type(), constData(), boolType, &res);
return res; return res;
} }
@ -2027,7 +2028,7 @@ bool QVariant::convert(QMetaType targetType)
if (oldValue.d.is_null && oldValue.d.typeId() != QMetaType::Nullptr) if (oldValue.d.is_null && oldValue.d.typeId() != QMetaType::Nullptr)
return false; 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; d.is_null = !ok;
return ok; return ok;
} }
@ -2039,7 +2040,7 @@ bool QVariant::convert(QMetaType targetType)
*/ */
bool QVariant::convert(const int type, void *ptr) const 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);
} }

View File

@ -698,7 +698,7 @@ template<typename T> inline T qvariant_cast(const QVariant &v)
return v.d.get<T>(); return v.d.get<T>();
T t{}; T t{};
QMetaType::convert(v.constData(), v.userType(), &t, qMetaTypeId<T>()); QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
return t; return t;
} }

View File

@ -710,7 +710,7 @@ static Qt::Alignment parseAlignment(const QCss::Value *values, int count)
static ColorData parseColorValue(QCss::Value v) static ColorData parseColorValue(QCss::Value v)
{ {
if (v.type == Value::Identifier || v.type == Value::String) { if (v.type == Value::Identifier || v.type == Value::String) {
v.variant.convert(QMetaType::QColor); v.variant.convert(QMetaType::fromType<QColor>());
v.type = Value::Color; v.type = Value::Color;
} }
@ -2759,7 +2759,7 @@ bool Parser::parseTerm(Value *value)
switch (lookup()) { switch (lookup()) {
case NUMBER: case NUMBER:
value->type = Value::Number; value->type = Value::Number;
value->variant.convert(QMetaType::Double); value->variant.convert(QMetaType::fromType<double>());
break; break;
case PERCENTAGE: case PERCENTAGE:
value->type = Value::Percentage; value->type = Value::Percentage;

View File

@ -160,21 +160,21 @@ void QShaderNodesLoader::load(const QJsonObject &prototypesObject)
if (parameterValue.isObject()) { if (parameterValue.isObject()) {
const QJsonObject parameterObject = parameterValue.toObject(); const QJsonObject parameterObject = parameterValue.toObject();
const QString type = parameterObject.value(QStringLiteral("type")).toString(); 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(); const QString value = parameterObject.value(QStringLiteral("value")).toString();
auto variant = QVariant(value); auto variant = QVariant(value);
if (QMetaType(typeId).flags() & QMetaType::IsEnumeration) { if (metaType.flags() & QMetaType::IsEnumeration) {
const QMetaObject *metaObject = QMetaType(typeId).metaObject(); const QMetaObject *metaObject = metaType.metaObject();
const char *className = metaObject->className(); const char *className = metaObject->className();
const QByteArray enumName = type.mid(static_cast<int>(qstrlen(className)) + 2).toUtf8(); const QByteArray enumName = type.mid(static_cast<int>(qstrlen(className)) + 2).toUtf8();
const QMetaEnum metaEnum = metaObject->enumerator(metaObject->indexOfEnumerator(enumName)); const QMetaEnum metaEnum = metaObject->enumerator(metaObject->indexOfEnumerator(enumName));
const int enumValue = metaEnum.keyToValue(value.toUtf8()); const int enumValue = metaEnum.keyToValue(value.toUtf8());
variant = QVariant(enumValue); variant = QVariant(enumValue);
variant.convert(typeId); variant.convert(metaType);
} else { } else {
variant.convert(typeId); variant.convert(metaType);
} }
node.setParameter(parameterName, variant); node.setParameter(parameterName, variant);
} else { } else {