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())
return reset(object);
v = QVariant(t, nullptr);
} else if (!v.convert(t.id())) {
} else if (!v.convert(t)) {
return false;
}
}

View File

@ -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<qulonglong *>(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<qulonglong>(), &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<qlonglong *>(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<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
QMetaEnum en = metaEnumFromType(fromType);
if (en.isValid()) {
const char *key = en.valueToKey(ll);
if (toTypeId == QMetaType::QString)
if (toType.id() == QMetaType::QString)
*static_cast<QString *>(to) = QString::fromUtf8(key);
else
*static_cast<QByteArray *>(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<const qlonglong *>(from);
ok = true;
} 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 =
customTypesConversionRegistry()->function(qMakePair(fromTypeId,
customTypesConversionRegistry()->function(qMakePair(fromType.id(),
qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>()));
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<QtMetaTypePrivate::QAssociativeIterableImpl>()));
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<QtMetaTypePrivate::QAssociativeIterableImpl>()));
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<QtMetaTypePrivate::QPairVariantInterfaceImpl>()));
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<QSequentialIterable *>(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<QtMetaTypePrivate::QSequentialIterableImpl>())) {
if (QMetaType::convert(fromType, from, QMetaType::fromType<QtMetaTypePrivate::QSequentialIterableImpl>(), &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<QAssociativeIterable *>(to);
if (fromTypeId == QMetaType::QVariantMap) {
if (fromType.id() == QMetaType::QVariantMap) {
i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast<const QVariantMap *>(from)));
return true;
}
if (fromTypeId == QMetaType::QVariantHash) {
if (fromType.id() == QMetaType::QVariantHash) {
i = QAssociativeIterable(QAssociativeIterableImpl(reinterpret_cast<const QVariantHash *>(from)));
return true;
}
QAssociativeIterableImpl impl;
if (QMetaType::convert(from, fromTypeId, &impl, qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
if (QMetaType::convert(fromType, from, QMetaType::fromType<QtMetaTypePrivate::QAssociativeIterableImpl>(), &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<std::nullptr_t *>(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<QSequentialIterable>())
return convertToSequentialIterable(from, fromTypeId, to);
return convertToSequentialIterable(fromType, from, to);
if (toTypeId == qMetaTypeId<QAssociativeIterable>())
return convertToAssociativeIterable(from, fromTypeId, to);
return convertToAssociativeIterable(fromType, from, to);
#ifndef QT_BOOTSTRAPPED
// handle QObject conversion

View File

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

View File

@ -1765,15 +1765,15 @@ QBitArray QVariant::toBitArray() const
template <typename T>
inline T qNumVariantToHelper(const QVariant::Private &d, bool *ok, const T& val)
{
const uint t = qMetaTypeId<T>();
QMetaType t = QMetaType::fromType<T>();
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<bool>())
auto boolType = QMetaType::fromType<bool>();
if (d.type() == boolType)
return d.get<bool>();
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);
}

View File

@ -698,7 +698,7 @@ template<typename T> inline T qvariant_cast(const QVariant &v)
return v.d.get<T>();
T t{};
QMetaType::convert(v.constData(), v.userType(), &t, qMetaTypeId<T>());
QMetaType::convert(v.metaType(), v.constData(), targetType, &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)
{
if (v.type == Value::Identifier || v.type == Value::String) {
v.variant.convert(QMetaType::QColor);
v.variant.convert(QMetaType::fromType<QColor>());
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<double>());
break;
case PERCENTAGE:
value->type = Value::Percentage;

View File

@ -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<int>(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 {