Use QMetaType in QMetaCallEvent

And don't use int based type mapping anymore.

Change-Id: I456e76d1933ef646a7bd39ce565886b89e938a44
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-07-27 19:16:17 +02:00
parent 700e6341e5
commit 0161f00e50
9 changed files with 34 additions and 34 deletions

View File

@ -2309,24 +2309,24 @@ bool QMetaMethod::invoke(QObject *object,
}
QScopedPointer<QMetaCallEvent> event(new QMetaCallEvent(idx_offset, idx_relative, callFunction, nullptr, -1, paramCount));
int *types = event->types();
QMetaType *types = event->types();
void **args = event->args();
int argIndex = 0;
for (int i = 1; i < paramCount; ++i) {
types[i] = QMetaType::fromName(typeNames[i]).id();
if (types[i] == QMetaType::UnknownType && param[i]) {
types[i] = QMetaType::fromName(typeNames[i]);
if (!types[i].isValid() && param[i]) {
// Try to register the type and try again before reporting an error.
void *argv[] = { &types[i], &argIndex };
QMetaObject::metacall(object, QMetaObject::RegisterMethodArgumentMetaType,
idx_relative + idx_offset, argv);
if (types[i] == -1) {
if (!types[i].isValid()) {
qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'",
typeNames[i]);
return false;
}
}
if (types[i] != QMetaType::UnknownType) {
if (types[i].isValid()) {
args[i] = QMetaType(types[i]).create(param[i]);
++argIndex;
}

View File

@ -506,7 +506,7 @@ inline void QMetaCallEvent::allocArgs()
if (!d.nargs_)
return;
constexpr size_t each = sizeof(void*) + sizeof(int);
constexpr size_t each = sizeof(void*) + sizeof(QMetaType);
void *const memory = d.nargs_ * each > sizeof(prealloc_) ?
calloc(d.nargs_, each) : prealloc_;
@ -588,10 +588,10 @@ QMetaCallEvent::QMetaCallEvent(QtPrivate::QSlotObjectBase *slotO,
QMetaCallEvent::~QMetaCallEvent()
{
if (d.nargs_) {
int *typeIDs = types();
QMetaType *t = types();
for (int i = 0; i < d.nargs_; ++i) {
if (typeIDs[i] && d.args_[i])
QMetaType(typeIDs[i]).destroy(d.args_[i]);
if (t[i].isValid() && d.args_[i])
t[i].destroy(d.args_[i]);
}
if (reinterpret_cast<void*>(d.args_) != reinterpret_cast<void*>(prealloc_))
free(d.args_);
@ -3644,17 +3644,17 @@ static void queued_activate(QObject *sender, int signal, QObjectPrivate::Connect
new QMetaCallEvent(c->method_offset, c->method_relative, c->callFunction, sender, signal, nargs);
void **args = ev->args();
int *types = ev->types();
QMetaType *types = ev->types();
types[0] = 0; // return type
types[0] = QMetaType(); // return type
args[0] = nullptr; // return value
if (nargs > 1) {
for (int n = 1; n < nargs; ++n)
types[n] = argumentTypes[n-1];
types[n] = QMetaType(argumentTypes[n-1]);
for (int n = 1; n < nargs; ++n)
args[n] = QMetaType(types[n]).create(argv[n]);
args[n] = types[n].create(argv[n]);
}
locker.relock();

View File

@ -548,8 +548,8 @@ public:
inline int id() const { return d.method_offset_ + d.method_relative_; }
inline const void * const* args() const { return d.args_; }
inline void ** args() { return d.args_; }
inline const int *types() const { return reinterpret_cast<int*>(d.args_ + d.nargs_); }
inline int *types() { return reinterpret_cast<int*>(d.args_ + d.nargs_); }
inline const QMetaType *types() const { return reinterpret_cast<QMetaType *>(d.args_ + d.nargs_); }
inline QMetaType *types() { return reinterpret_cast<QMetaType *>(d.args_ + d.nargs_); }
virtual void placeMetaCall(QObject *object) override;
@ -565,7 +565,7 @@ private:
ushort method_relative_;
} d;
// preallocate enough space for three arguments
char prealloc_[3*(sizeof(void*) + sizeof(int))];
alignas(void *) char prealloc_[3*sizeof(void*) + 3*sizeof(QMetaType)];
};
class QBoolBlocker

View File

@ -1139,14 +1139,14 @@ static bool setFontSizeFromValue(QCss::Value value, QFont *font, int *fontSizeAd
if (s.endsWith(QLatin1String("pt"), Qt::CaseInsensitive)) {
s.chop(2);
value.variant = s;
if (value.variant.convert((QVariant::Type)qMetaTypeId<qreal>())) {
if (value.variant.convert(QMetaType::fromType<qreal>())) {
font->setPointSizeF(value.variant.toReal());
valid = true;
}
} else if (s.endsWith(QLatin1String("px"), Qt::CaseInsensitive)) {
s.chop(2);
value.variant = s;
if (value.variant.convert(QMetaType::Int)) {
if (value.variant.convert(QMetaType::fromType<int>())) {
font->setPixelSize(value.variant.toInt());
valid = true;
}

View File

@ -187,21 +187,21 @@ void QShaderGraphLoader::load()
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 {

View File

@ -159,11 +159,11 @@ void QHostInfoResult::postResultsReady(const QHostInfo &info)
auto metaCallEvent = new QMetaCallEvent(slotObj, nullptr, signal_index, nargs);
Q_CHECK_PTR(metaCallEvent);
void **args = metaCallEvent->args();
int *types = metaCallEvent->types();
QMetaType *types = metaCallEvent->types();
auto voidType = QMetaType::fromType<void>();
auto hostInfoType = QMetaType::fromType<QHostInfo>();
types[0] = voidType.id();
types[1] = hostInfoType.id();
types[0] = voidType;
types[1] = hostInfoType;
args[0] = nullptr;
args[1] = hostInfoType.create(&info);
Q_CHECK_PTR(args[1]);

View File

@ -1016,7 +1016,7 @@ void Generator::generateMetacall()
fprintf(out, " if (_id < %d)\n", int(methodList.size()));
if (methodsWithAutomaticTypesHelper(methodList).isEmpty())
fprintf(out, " *reinterpret_cast<int*>(_a[0]) = -1;\n");
fprintf(out, " *reinterpret_cast<QMetaType *>(_a[0]) = QMetaType();\n");
else
fprintf(out, " qt_static_metacall(this, _c, _id, _a);\n");
fprintf(out, " _id -= %d;\n }", int(methodList.size()));
@ -1177,13 +1177,13 @@ void Generator::generateStaticMetacall()
if (!methodsWithAutomaticTypes.isEmpty()) {
fprintf(out, " else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {\n");
fprintf(out, " switch (_id) {\n");
fprintf(out, " default: *reinterpret_cast<int*>(_a[0]) = -1; break;\n");
fprintf(out, " default: *reinterpret_cast<QMetaType *>(_a[0]) = QMetaType(); break;\n");
QMap<int, QMultiMap<QByteArray, int> >::const_iterator it = methodsWithAutomaticTypes.constBegin();
const QMap<int, QMultiMap<QByteArray, int> >::const_iterator end = methodsWithAutomaticTypes.constEnd();
for ( ; it != end; ++it) {
fprintf(out, " case %d:\n", it.key());
fprintf(out, " switch (*reinterpret_cast<int*>(_a[1])) {\n");
fprintf(out, " default: *reinterpret_cast<int*>(_a[0]) = -1; break;\n");
fprintf(out, " default: *reinterpret_cast<QMetaType *>(_a[0]) = QMetaType(); break;\n");
auto jt = it->begin();
const auto jend = it->end();
while (jt != jend) {
@ -1191,7 +1191,7 @@ void Generator::generateStaticMetacall()
const QByteArray &lastKey = jt.key();
++jt;
if (jt == jend || jt.key() != lastKey)
fprintf(out, " *reinterpret_cast<int*>(_a[0]) = qRegisterMetaType< %s >(); break;\n", lastKey.constData());
fprintf(out, " *reinterpret_cast<QMetaType *>(_a[0]) = QMetaType::fromType< %s >(); break;\n", lastKey.constData());
}
fprintf(out, " }\n");
fprintf(out, " break;\n");

View File

@ -104,7 +104,7 @@ static QString addFunction(const FunctionDef &mm, bool isSignal = false) {
isSignal ? "signal" : "method", mm.name.constData());
// check the return type first
int typeId = QMetaType::type(mm.normalizedType.constData());
int typeId = QMetaType::fromName(mm.normalizedType).id();
if (typeId != QMetaType::Void) {
if (typeId) {
const char *typeName = QDBusMetaType::typeToSignature(typeId);
@ -158,7 +158,7 @@ static QString addFunction(const FunctionDef &mm, bool isSignal = false) {
// do we need to describe this argument?
if (QDBusMetaType::signatureToType(signature) == QMetaType::UnknownType) {
const char *typeName = QMetaType::typeName(types.at(j));
const char *typeName = QMetaType(types.at(j)).name();
xml += QString::fromLatin1(" <annotation name=\"org.qtproject.QtDBus.QtTypeName.%1%2\" value=\"%3\"/>\n")
.arg(isOutput ? QLatin1String("Out") : QLatin1String("In"))
.arg(isOutput && !isSignal ? j - inputCount : j - 1)
@ -208,7 +208,7 @@ static QString generateInterfaceXml(const ClassDef *mo)
if (!mp.write.isEmpty())
access |= 2;
int typeId = QMetaType::type(mp.type.constData());
int typeId = QMetaType::fromName(mp.type).id();
if (!typeId) {
fprintf(stderr, PROGRAMNAME ": unregistered type: '%s', ignoring\n",
mp.type.constData());

View File

@ -246,7 +246,7 @@ static QByteArray qtTypeName(const QString &signature, const QDBusIntrospection:
return std::move(qttype).toLatin1();
}
return QVariant::typeToName(QVariant::Type(type));
return QMetaType(type).name();
}
static QString nonConstRefArg(const QByteArray &arg)