Finish porting number conversions to QMetaType

Change-Id: I2861c6fb5f8192a627ffb41f1455c703849cf945
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-07-13 19:25:16 +02:00
parent fefb1c1362
commit 2b83244402
2 changed files with 29 additions and 253 deletions

View File

@ -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);

View File

@ -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<QString>().toULongLong(ok);
case QMetaType::QChar:
return d->get<QChar>().unicode();
case QMetaType::QByteArray:
return d->get<QByteArray>().toULongLong(ok);
case QMetaType::Bool:
return qulonglong(d->get<bool>());
#ifndef QT_BOOTSTRAPPED
case QMetaType::QCborValue:
if (d->get<QCborValue>().isDouble())
return qulonglong(qConvertToRealNumber(d, ok));
if (!d->get<QCborValue>().isInteger())
return false;
return qulonglong(qMetaTypeNumber(d));
case QMetaType::QJsonValue:
if (!d->get<QJsonValue>().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<unsigned char>();
case 2:
return d->get<unsigned short>();
case 4:
return d->get<unsigned int>();
case 8:
return d->get<qulonglong>();
}
}
*ok = false;
return Q_UINT64_C(0);
}
template<typename TInput, typename LiteralWrapper>
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<short *>(result) = short(qConvertToNumber(d, &ok));
return ok;
case QMetaType::Long:
*static_cast<long *>(result) = long(qConvertToNumber(d, &ok));
return ok;
case QMetaType::UShort:
*static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, &ok));
return ok;
case QMetaType::ULong:
*static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, &ok));
return ok;
case QMetaType::Int:
*static_cast<int *>(result) = int(qConvertToNumber(d, &ok));
return ok;
case QMetaType::UInt:
*static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, &ok));
return ok;
case QMetaType::LongLong:
*static_cast<qlonglong *>(result) = qConvertToNumber(d, &ok);
return ok;
case QMetaType::ULongLong: {
*static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, &ok);
return ok;
}
case QMetaType::SChar: {
signed char s = qConvertToNumber(d, &ok);
*static_cast<signed char*>(result) = s;
return ok;
}
case QMetaType::UChar: {
*static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, &ok);
return ok;
}
case QMetaType::Bool: {
bool *b = static_cast<bool *>(result);
switch (d->typeId()) {
case QMetaType::QByteArray:
*b = qt_convertToBool<QByteArray, const char*>(d);
break;
case QMetaType::QString:
*b = qt_convertToBool<QString, QLatin1String>(d);
break;
case QMetaType::QChar:
*b = !d->get<QChar>().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<QCborValue>().toBool();
if (!d->get<QCborValue>().isBool())
return false;
break;
case QMetaType::QJsonValue:
*b = d->get<QJsonValue>().toBool(false);
if (!d->get<QJsonValue>().isBool())
return false;
break;
#endif
default:
*b = false;
return false;
}
break;
}
case QMetaType::Double: {
double *f = static_cast<double *>(result);
switch (d->typeId()) {
case QMetaType::QString:
*f = d->get<QString>().toDouble(&ok);
return ok;
case QMetaType::QByteArray:
*f = d->get<QByteArray>().toDouble(&ok);
return ok;
case QMetaType::Bool:
*f = double(d->get<bool>());
break;
case QMetaType::Float:
*f = double(d->get<float>());
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<QCborValue>().toDouble();
if (!d->get<QCborValue>().isDouble())
return false;
break;
case QMetaType::QJsonValue:
*f = d->get<QJsonValue>().toDouble(0.0);
if (!d->get<QJsonValue>().isDouble())
return false;
break;
#endif
default:
*f = 0.0;
return false;
}
break;
}
case QMetaType::Float: {
float *f = static_cast<float *>(result);
switch (d->typeId()) {
case QMetaType::QString:
*f = d->get<QString>().toFloat(&ok);
return ok;
case QMetaType::QByteArray:
*f = d->get<QByteArray>().toFloat(&ok);
return ok;
case QMetaType::Bool:
*f = float(d->get<bool>());
break;
case QMetaType::Double:
*f = float(d->get<double>());
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<QCborValue>().toDouble();
if (!d->get<QCborValue>().isDouble())
return false;
break;
case QMetaType::QJsonValue:
*f = d->get<QJsonValue>().toDouble(0.0);
if (!d->get<QJsonValue>().isDouble())
return false;
break;
#endif
default:
*f = 0.0f;
return false;
}
break;
}
case QMetaType::Char:
{
*static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, &ok));
return ok;
}
case QMetaType::Nullptr:
*static_cast<std::nullptr_t *>(result) = nullptr;
if (QMetaType::typeFlags(t) & (QMetaType::PointerToGadget | QMetaType::PointerToQObject)