From 2b8324440276d4255fce1fb0cff882ae06ab9f76 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 13 Jul 2020 19:25:16 +0200 Subject: [PATCH] Finish porting number conversions to QMetaType Change-Id: I2861c6fb5f8192a627ffb41f1455c703849cf945 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qmetatype.cpp | 40 +++-- src/corelib/kernel/qvariant.cpp | 242 ------------------------------- 2 files changed, 29 insertions(+), 253 deletions(-) diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 4bbc228a9e..843274df3a 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -949,6 +949,8 @@ static const struct : QMetaTypeModuleHelper QMETATYPE_CONVERTER_ASSIGN(To, ULong); \ QMETATYPE_CONVERTER_ASSIGN(To, LongLong); \ QMETATYPE_CONVERTER_ASSIGN(To, ULongLong); \ + QMETATYPE_CONVERTER(To, Float, result = qRound64(source); return true;); \ + QMETATYPE_CONVERTER(To, Double, result = qRound64(source); return true;); \ QMETATYPE_CONVERTER(To, QChar, result = source.unicode(); return true;); \ QMETATYPE_CONVERTER(To, QString, \ bool ok = false; \ @@ -972,18 +974,32 @@ static const struct : QMetaTypeModuleHelper ); \ CONVERT_CBOR_AND_JSON(To) - QMETATYPE_CONVERTER(To, QCborValue, \ - if (!source.isInteger() && !source.isDouble()) \ - return false; \ - result = source.toInteger(); \ - return false; \ +#define FLOAT_CONVERTER(To) \ + QMETATYPE_CONVERTER_ASSIGN(To, Bool); \ + QMETATYPE_CONVERTER_ASSIGN(To, Char); \ + QMETATYPE_CONVERTER_ASSIGN(To, UChar); \ + QMETATYPE_CONVERTER_ASSIGN(To, SChar); \ + QMETATYPE_CONVERTER_ASSIGN(To, Short); \ + QMETATYPE_CONVERTER_ASSIGN(To, UShort); \ + QMETATYPE_CONVERTER_ASSIGN(To, Int); \ + QMETATYPE_CONVERTER_ASSIGN(To, UInt); \ + QMETATYPE_CONVERTER_ASSIGN(To, Long); \ + QMETATYPE_CONVERTER_ASSIGN(To, ULong); \ + QMETATYPE_CONVERTER_ASSIGN(To, LongLong); \ + QMETATYPE_CONVERTER_ASSIGN(To, ULongLong); \ + QMETATYPE_CONVERTER_ASSIGN(To, Float); \ + QMETATYPE_CONVERTER_ASSIGN(To, Double); \ + QMETATYPE_CONVERTER(To, QString, \ + bool ok = false; \ + result = source.toDouble(&ok); \ + return ok; \ ); \ - QMETATYPE_CONVERTER(To, QJsonValue, \ - if (source.isDouble()) \ - return false; \ - result = source.toInteger(); \ - return false; \ - ) + QMETATYPE_CONVERTER(To, QByteArray, \ + bool ok = false; \ + result = source.toDouble(&ok); \ + return ok; \ + ); \ + CONVERT_CBOR_AND_JSON(To) switch (makePair(toTypeId, fromTypeId)) { @@ -1000,6 +1016,8 @@ static const struct : QMetaTypeModuleHelper INTEGRAL_CONVERTER(ULong); INTEGRAL_CONVERTER(LongLong); INTEGRAL_CONVERTER(ULongLong); + FLOAT_CONVERTER(Float); + FLOAT_CONVERTER(Double); #ifndef QT_BOOTSTRAPPED QMETATYPE_CONVERTER_ASSIGN(QUrl, QString); diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 1877832db3..f45aec5d60 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -245,66 +245,6 @@ static qreal qConvertToRealNumber(const QVariant::Private *d, bool *ok) } } -static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok) -{ - *ok = true; - - switch (uint(d->typeId())) { - case QMetaType::QString: - return d->get().toULongLong(ok); - case QMetaType::QChar: - return d->get().unicode(); - case QMetaType::QByteArray: - return d->get().toULongLong(ok); - case QMetaType::Bool: - return qulonglong(d->get()); -#ifndef QT_BOOTSTRAPPED - case QMetaType::QCborValue: - if (d->get().isDouble()) - return qulonglong(qConvertToRealNumber(d, ok)); - if (!d->get().isInteger()) - return false; - return qulonglong(qMetaTypeNumber(d)); - case QMetaType::QJsonValue: - if (!d->get().isDouble()) - break; - Q_FALLTHROUGH(); -#endif - case QMetaType::Double: - case QMetaType::Int: - case QMetaType::Char: - case QMetaType::SChar: - case QMetaType::Short: - case QMetaType::Long: - case QMetaType::Float: - case QMetaType::LongLong: - return qulonglong(qMetaTypeNumber(d)); - case QMetaType::ULongLong: - case QMetaType::UInt: - case QMetaType::UChar: - case QMetaType::UShort: - case QMetaType::ULong: - return qMetaTypeUNumber(d); - } - - QMetaType typeInfo = d->type(); - if (typeInfo.flags() & QMetaType::IsEnumeration) { - switch (typeInfo.sizeOf()) { - case 1: - return d->get(); - case 2: - return d->get(); - case 4: - return d->get(); - case 8: - return d->get(); - } - } - - *ok = false; - return Q_UINT64_C(0); -} - template inline bool qt_convertToBool(const QVariant::Private *const d) { @@ -330,188 +270,6 @@ static bool convert(const QVariant::Private *d, int t, void *result) bool ok = true; switch (uint(t)) { - case QMetaType::Short: - *static_cast(result) = short(qConvertToNumber(d, &ok)); - return ok; - case QMetaType::Long: - *static_cast(result) = long(qConvertToNumber(d, &ok)); - return ok; - case QMetaType::UShort: - *static_cast(result) = ushort(qConvertToUnsignedNumber(d, &ok)); - return ok; - case QMetaType::ULong: - *static_cast(result) = ulong(qConvertToUnsignedNumber(d, &ok)); - return ok; - case QMetaType::Int: - *static_cast(result) = int(qConvertToNumber(d, &ok)); - return ok; - case QMetaType::UInt: - *static_cast(result) = uint(qConvertToUnsignedNumber(d, &ok)); - return ok; - case QMetaType::LongLong: - *static_cast(result) = qConvertToNumber(d, &ok); - return ok; - case QMetaType::ULongLong: { - *static_cast(result) = qConvertToUnsignedNumber(d, &ok); - return ok; - } - case QMetaType::SChar: { - signed char s = qConvertToNumber(d, &ok); - *static_cast(result) = s; - return ok; - } - case QMetaType::UChar: { - *static_cast(result) = qConvertToUnsignedNumber(d, &ok); - return ok; - } - case QMetaType::Bool: { - bool *b = static_cast(result); - switch (d->typeId()) { - case QMetaType::QByteArray: - *b = qt_convertToBool(d); - break; - case QMetaType::QString: - *b = qt_convertToBool(d); - break; - case QMetaType::QChar: - *b = !d->get().isNull(); - break; - case QMetaType::Double: - case QMetaType::Int: - case QMetaType::LongLong: - case QMetaType::Char: - case QMetaType::SChar: - case QMetaType::Short: - case QMetaType::Long: - case QMetaType::Float: - *b = qMetaTypeNumber(d) != Q_INT64_C(0); - break; - case QMetaType::UInt: - case QMetaType::ULongLong: - case QMetaType::UChar: - case QMetaType::UShort: - case QMetaType::ULong: - *b = qMetaTypeUNumber(d) != Q_UINT64_C(0); - break; -#ifndef QT_BOOTSTRAPPED - case QMetaType::QCborValue: - *b = d->get().toBool(); - if (!d->get().isBool()) - return false; - break; - case QMetaType::QJsonValue: - *b = d->get().toBool(false); - if (!d->get().isBool()) - return false; - break; -#endif - default: - *b = false; - return false; - } - break; - } - case QMetaType::Double: { - double *f = static_cast(result); - switch (d->typeId()) { - case QMetaType::QString: - *f = d->get().toDouble(&ok); - return ok; - case QMetaType::QByteArray: - *f = d->get().toDouble(&ok); - return ok; - case QMetaType::Bool: - *f = double(d->get()); - break; - case QMetaType::Float: - *f = double(d->get()); - break; - case QMetaType::LongLong: - case QMetaType::Int: - case QMetaType::Char: - case QMetaType::SChar: - case QMetaType::Short: - case QMetaType::Long: - *f = double(qMetaTypeNumber(d)); - break; - case QMetaType::UInt: - case QMetaType::ULongLong: - case QMetaType::UChar: - case QMetaType::UShort: - case QMetaType::ULong: - *f = double(qMetaTypeUNumber(d)); - break; -#ifndef QT_BOOTSTRAPPED - case QMetaType::QCborValue: - *f = d->get().toDouble(); - if (!d->get().isDouble()) - return false; - break; - case QMetaType::QJsonValue: - *f = d->get().toDouble(0.0); - if (!d->get().isDouble()) - return false; - break; -#endif - default: - *f = 0.0; - return false; - } - break; - } - case QMetaType::Float: { - float *f = static_cast(result); - switch (d->typeId()) { - case QMetaType::QString: - *f = d->get().toFloat(&ok); - return ok; - case QMetaType::QByteArray: - *f = d->get().toFloat(&ok); - return ok; - case QMetaType::Bool: - *f = float(d->get()); - break; - case QMetaType::Double: - *f = float(d->get()); - break; - case QMetaType::LongLong: - case QMetaType::Int: - case QMetaType::Char: - case QMetaType::SChar: - case QMetaType::Short: - case QMetaType::Long: - *f = float(qMetaTypeNumber(d)); - break; - case QMetaType::UInt: - case QMetaType::ULongLong: - case QMetaType::UChar: - case QMetaType::UShort: - case QMetaType::ULong: - *f = float(qMetaTypeUNumber(d)); - break; -#ifndef QT_BOOTSTRAPPED - case QMetaType::QCborValue: - *f = d->get().toDouble(); - if (!d->get().isDouble()) - return false; - break; - case QMetaType::QJsonValue: - *f = d->get().toDouble(0.0); - if (!d->get().isDouble()) - return false; - break; -#endif - default: - *f = 0.0f; - return false; - } - break; - } - case QMetaType::Char: - { - *static_cast(result) = qint8(qConvertToNumber(d, &ok)); - return ok; - } case QMetaType::Nullptr: *static_cast(result) = nullptr; if (QMetaType::typeFlags(t) & (QMetaType::PointerToGadget | QMetaType::PointerToQObject)