Move QJsonValue and QCborValue conversions to QMetaType

Change-Id: Id4796e0913c0c5b0abce0ea27bed0f7b41bc0cca
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-07-14 00:14:48 +02:00
parent b69a1d8eed
commit 53047c3623
2 changed files with 127 additions and 151 deletions

View File

@ -898,6 +898,8 @@ static const struct : QMetaTypeModuleHelper
using Bool = bool;
using Nullptr = std::nullptr_t;
#define QMETATYPE_CONVERTER_ASSIGN_DOUBLE(To, From) \
QMETATYPE_CONVERTER(To, From, result = double(source); return true;)
#define QMETATYPE_CONVERTER_ASSIGN_NUMBER(To, From) \
QMETATYPE_CONVERTER(To, From, result = To::number(source); return true;)
#ifndef QT_BOOTSTRAPPED
@ -1228,6 +1230,59 @@ static const struct : QMetaTypeModuleHelper
return true;
);
#endif
QMETATYPE_CONVERTER(QCborValue, Nullptr,
Q_UNUSED(source);
result = QCborValue(QCborValue::Null);
return true;
);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, Bool);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, Int);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, UInt);
QMETATYPE_CONVERTER(QCborValue, ULong, result = qlonglong(source); return true;);
QMETATYPE_CONVERTER(QCborValue, Long, result = qlonglong(source); return true;);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, LongLong);
QMETATYPE_CONVERTER(QCborValue, ULongLong, result = qlonglong(source); return true;);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, UShort);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, UChar);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, Char);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, SChar);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, Short);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, Double);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, Float);
QMETATYPE_CONVERTER(QCborValue, QStringList,
result = QCborArray::fromStringList(source);
return true;
);
QMETATYPE_CONVERTER(QCborValue, QDate,
result = QCborValue(source.startOfDay());
return true;
);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, QUrl);
QMETATYPE_CONVERTER(QCborValue, QJsonValue,
result = QCborValue::fromJsonValue(source);
return true;
);
QMETATYPE_CONVERTER(QCborValue, QJsonObject,
result = QCborMap::fromJsonObject(source);
return true;
);
QMETATYPE_CONVERTER(QCborValue, QJsonArray,
result = QCborArray::fromJsonArray(source);
return true;
);
QMETATYPE_CONVERTER(QCborValue, QJsonDocument,
QJsonDocument doc = source;
if (doc.isArray())
result = QCborArray::fromJsonArray(doc.array());
else
result = QCborMap::fromJsonObject(doc.object());
return true;
);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, QCborMap);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, QCborArray);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, QDateTime);
QMETATYPE_CONVERTER(QDateTime, QCborValue,
if (source.isDateTime()) {
result = source.toDateTime();
@ -1236,6 +1291,15 @@ static const struct : QMetaTypeModuleHelper
return false;
);
QMETATYPE_CONVERTER_ASSIGN(QCborValue, QCborSimpleType);
QMETATYPE_CONVERTER(QCborSimpleType, QCborValue,
if (source.isSimpleType()) {
result = source.toSimpleType();
return true;
}
return false;
);
QMETATYPE_CONVERTER(QCborArray, QVariantList, result = QCborArray::fromVariantList(source); return true;);
QMETATYPE_CONVERTER(QVariantList, QCborArray, result = source.toVariantList(); return true;);
QMETATYPE_CONVERTER(QCborArray, QStringList, result = QCborArray::fromStringList(source); return true;);
@ -1356,6 +1420,69 @@ static const struct : QMetaTypeModuleHelper
);
QMETATYPE_CONVERTER(QJsonObject, QCborMap, result = source.toJsonObject(); return true; );
QMETATYPE_CONVERTER(QJsonValue, Nullptr,
Q_UNUSED(source);
result = QJsonValue(QJsonValue::Null);
return true;);
QMETATYPE_CONVERTER(QJsonValue, Bool,
result = QJsonValue(source);
return true;);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Int);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, UInt);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Double);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Float);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, ULong);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Long);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, LongLong);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, ULongLong);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, UShort);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, UChar);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Char);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, SChar);
QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Short);
QMETATYPE_CONVERTER_ASSIGN(QJsonValue, QString);
QMETATYPE_CONVERTER(QJsonValue, QStringList,
result = QJsonValue(QJsonArray::fromStringList(source));
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QVariantList,
result = QJsonValue(QJsonArray::fromVariantList(source));
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QVariantMap,
result = QJsonValue(QJsonObject::fromVariantMap(source));
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QVariantHash,
result = QJsonValue(QJsonObject::fromVariantHash(source));
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QJsonObject,
result = source;
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QJsonArray,
result = source;
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QJsonDocument,
QJsonDocument doc = source;
result = doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object());
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QCborValue,
result = source.toJsonValue();
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QCborMap,
result = source.toJsonObject();
return true;
);
QMETATYPE_CONVERTER(QJsonValue, QCborArray,
result = source.toJsonArray();
return true;
);
#endif
QMETATYPE_CONVERTER(QDate, QDateTime, result = source.date(); return true;);

View File

@ -255,8 +255,6 @@ static bool convert(const QVariant::Private *d, int t, void *result)
Q_ASSERT(d->typeId() != t);
Q_ASSERT(result);
bool ok = true;
switch (uint(t)) {
case QMetaType::Nullptr:
*static_cast<std::nullptr_t *>(result) = nullptr;
@ -271,155 +269,6 @@ static bool convert(const QVariant::Private *d, int t, void *result)
#endif
return false;
#ifndef QT_BOOTSTRAPPED
case QMetaType::QJsonValue:
switch (d->typeId()) {
case QMetaType::Nullptr:
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonValue::Null);
break;
case QMetaType::Bool:
*static_cast<QJsonValue *>(result) = QJsonValue(d->get<bool>());
break;
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::Double:
case QMetaType::Float:
case QMetaType::ULong:
case QMetaType::Long:
case QMetaType::LongLong:
case QMetaType::ULongLong:
case QMetaType::UShort:
case QMetaType::UChar:
case QMetaType::Char:
case QMetaType::SChar:
case QMetaType::Short:
*static_cast<QJsonValue *>(result) = QJsonValue(qConvertToRealNumber(d, &ok));
Q_ASSERT(ok);
break;
case QMetaType::QString:
*static_cast<QJsonValue *>(result) = QJsonValue(d->get<QString>());
break;
case QMetaType::QStringList:
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonArray::fromStringList(d->get<QStringList>()));
break;
case QMetaType::QVariantList:
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonArray::fromVariantList(d->get<QVariantList>()));
break;
case QMetaType::QVariantMap:
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonObject::fromVariantMap(d->get<QVariantMap>()));
break;
case QMetaType::QVariantHash:
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonObject::fromVariantHash(d->get<QVariantHash>()));
break;
case QMetaType::QJsonObject:
*static_cast<QJsonValue *>(result) = d->get<QJsonObject>();
break;
case QMetaType::QJsonArray:
*static_cast<QJsonValue *>(result) = d->get<QJsonArray>();
break;
case QMetaType::QJsonDocument: {
QJsonDocument doc = d->get<QJsonDocument>();
*static_cast<QJsonValue *>(result) = doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object());
break;
}
case QMetaType::QCborValue:
*static_cast<QJsonValue *>(result) = d->get<QCborValue>().toJsonValue();
break;
case QMetaType::QCborMap:
*static_cast<QJsonValue *>(result) = d->get<QCborMap>().toJsonObject();
break;
case QMetaType::QCborArray:
*static_cast<QJsonValue *>(result) = d->get<QCborArray>().toJsonArray();
break;
default:
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonValue::Undefined);
return false;
}
break;
case QMetaType::QCborSimpleType:
if (d->typeId() == QMetaType::QCborValue && d->get<QCborValue>().isSimpleType()) {
*static_cast<QCborSimpleType *>(result) = d->get<QCborValue>().toSimpleType();
break;
}
return false;
case QMetaType::QCborValue:
switch (d->typeId()) {
case QMetaType::Nullptr:
*static_cast<QCborValue *>(result) = QCborValue(QCborValue::Null);
break;
case QMetaType::Bool:
*static_cast<QCborValue *>(result) = QCborValue(d->get<bool>());
break;
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::ULong:
case QMetaType::Long:
case QMetaType::LongLong:
case QMetaType::ULongLong:
case QMetaType::UShort:
case QMetaType::UChar:
case QMetaType::Char:
case QMetaType::SChar:
case QMetaType::Short:
*static_cast<QCborValue *>(result) = QCborValue(qConvertToNumber(d, &ok));
Q_ASSERT(ok);
break;
case QMetaType::Double:
case QMetaType::Float:
*static_cast<QCborValue *>(result) = QCborValue(qConvertToRealNumber(d, &ok));
Q_ASSERT(ok);
break;
case QMetaType::QString:
*static_cast<QCborValue *>(result) = d->get<QString>();
break;
case QMetaType::QStringList:
*static_cast<QCborValue *>(result) = QCborArray::fromStringList(d->get<QStringList>());
break;
case QMetaType::QByteArray:
*static_cast<QCborValue *>(result) = d->get<QByteArray>();
break;
case QMetaType::QDate:
*static_cast<QCborValue *>(result) = QCborValue(d->get<QDate>().startOfDay());
break;
case QMetaType::QDateTime:
*static_cast<QCborValue *>(result) = QCborValue(d->get<QDateTime>());
break;
case QMetaType::QUrl:
*static_cast<QCborValue *>(result) = QCborValue(d->get<QUrl>());
break;
case QMetaType::QJsonValue:
*static_cast<QCborValue *>(result) = QCborValue::fromJsonValue(d->get<QJsonValue>());
break;
case QMetaType::QJsonObject:
*static_cast<QCborValue *>(result) = QCborMap::fromJsonObject(d->get<QJsonObject>());
break;
case QMetaType::QJsonArray:
*static_cast<QCborValue *>(result) = QCborArray::fromJsonArray(d->get<QJsonArray>());
break;
case QMetaType::QJsonDocument: {
QJsonDocument doc = d->get<QJsonDocument>();
if (doc.isArray())
*static_cast<QCborValue *>(result) = QCborArray::fromJsonArray(doc.array());
else
*static_cast<QCborValue *>(result) = QCborMap::fromJsonObject(doc.object());
break;
}
case QMetaType::QCborSimpleType:
*static_cast<QCborValue *>(result) = d->get<QCborSimpleType>();
break;
case QMetaType::QCborMap:
*static_cast<QCborValue *>(result) = d->get<QCborMap>();
break;
case QMetaType::QCborArray:
*static_cast<QCborValue *>(result) = d->get<QCborArray>();
break;
default:
*static_cast<QCborValue *>(result) = {};
return false;
}
break;
#endif
default:
return false;
}