diff options
Diffstat (limited to 'src/dbus/qdbusmetaobject.cpp')
-rw-r--r-- | src/dbus/qdbusmetaobject.cpp | 223 |
1 files changed, 89 insertions, 134 deletions
diff --git a/src/dbus/qdbusmetaobject.cpp b/src/dbus/qdbusmetaobject.cpp index bf42736eab..543b185df9 100644 --- a/src/dbus/qdbusmetaobject.cpp +++ b/src/dbus/qdbusmetaobject.cpp @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtDBus module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qdbusmetaobject_p.h" @@ -59,6 +23,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + class QDBusMetaObjectGenerator { public: @@ -75,22 +41,23 @@ private: QVarLengthArray<int, 4> inputTypes; QVarLengthArray<int, 4> outputTypes; QByteArray rawReturnType; - int flags; + quint32 flags; }; struct Property { QByteArray typeName; QByteArray signature; int type; - int flags; + quint32 flags; }; struct Type { int id; QByteArray name; }; - QMap<QByteArray, Method> signals_; - QMap<QByteArray, Method> methods; + using MethodMap = QMap<QByteArray, Method>; + MethodMap signals_; + MethodMap methods; QMap<QByteArray, Property> properties; const QDBusIntrospection::Interface *data; @@ -104,11 +71,11 @@ private: void parseSignals(); void parseProperties(); - static int aggregateParameterCount(const QMap<QByteArray, Method> &map); + static qsizetype aggregateParameterCount(const MethodMap &map); }; -static const int intsPerProperty = 2; -static const int intsPerMethod = 2; +static const qsizetype intsPerProperty = 2; +static const qsizetype intsPerMethod = 2; struct QDBusMetaObjectPrivate : public QMetaObjectPrivate { @@ -144,8 +111,8 @@ static int registerComplexDBusType(const QByteArray &typeName) {} }; - static QBasicMutex mutex; - static struct Hash : QHash<QByteArray, QMetaType> + Q_CONSTINIT static QBasicMutex mutex; + Q_CONSTINIT static struct Hash : QHash<QByteArray, QMetaType> { ~Hash() { @@ -177,11 +144,12 @@ QDBusMetaObjectGenerator::findType(const QByteArray &signature, QString annotationName = QString::fromLatin1("org.qtproject.QtDBus.QtTypeName"); if (id >= 0) annotationName += QString::fromLatin1(".%1%2") - .arg(QLatin1String(direction)) + .arg(QLatin1StringView(direction)) .arg(id); // extract from annotations: - QByteArray typeName = annotations.value(annotationName).toLatin1(); + auto annotation = annotations.value(annotationName); + QByteArray typeName = annotation.value.toLatin1(); // verify that it's a valid one if (typeName.isEmpty()) { @@ -189,9 +157,10 @@ QDBusMetaObjectGenerator::findType(const QByteArray &signature, annotationName = QString::fromLatin1("com.trolltech.QtDBus.QtTypeName"); if (id >= 0) annotationName += QString::fromLatin1(".%1%2") - .arg(QLatin1String(direction)) + .arg(QLatin1StringView(direction)) .arg(id); - typeName = annotations.value(annotationName).toLatin1(); + annotation = annotations.value(annotationName); + typeName = annotation.value.toLatin1(); } if (!typeName.isEmpty()) { @@ -242,10 +211,7 @@ void QDBusMetaObjectGenerator::parseMethods() // Add cloned methods when the remote object has return types // - QDBusIntrospection::Methods::ConstIterator method_it = data->methods.constBegin(); - QDBusIntrospection::Methods::ConstIterator method_end = data->methods.constEnd(); - for ( ; method_it != method_end; ++method_it) { - const QDBusIntrospection::Method &m = *method_it; + for (const QDBusIntrospection::Method &m : std::as_const(data->methods)) { Method mm; mm.name = m.name.toLatin1(); @@ -255,7 +221,7 @@ void QDBusMetaObjectGenerator::parseMethods() bool ok = true; // build the input argument list - for (int i = 0; i < m.inputArgs.count(); ++i) { + for (qsizetype i = 0; i < m.inputArgs.size(); ++i) { const QDBusIntrospection::Argument &arg = m.inputArgs.at(i); Type type = findType(arg.type.toLatin1(), m.annotations, "In", i); @@ -274,7 +240,7 @@ void QDBusMetaObjectGenerator::parseMethods() if (!ok) continue; // build the output argument list: - for (int i = 0; i < m.outputArgs.count(); ++i) { + for (qsizetype i = 0; i < m.outputArgs.size(); ++i) { const QDBusIntrospection::Argument &arg = m.outputArgs.at(i); Type type = findType(arg.type.toLatin1(), m.annotations, "Out", i); @@ -300,12 +266,12 @@ void QDBusMetaObjectGenerator::parseMethods() // convert the last commas: if (!mm.parameterNames.isEmpty()) - prototype[prototype.length() - 1] = ')'; + prototype[prototype.size() - 1] = ')'; else prototype.append(')'); // check the async tag - if (m.annotations.value(QLatin1String(ANNOTATION_NO_WAIT)) == QLatin1String("true")) + if (m.annotations.value(ANNOTATION_NO_WAIT ""_L1).value == "true"_L1) mm.tag = "Q_NOREPLY"; // meta method flags @@ -318,10 +284,7 @@ void QDBusMetaObjectGenerator::parseMethods() void QDBusMetaObjectGenerator::parseSignals() { - QDBusIntrospection::Signals::ConstIterator signal_it = data->signals_.constBegin(); - QDBusIntrospection::Signals::ConstIterator signal_end = data->signals_.constEnd(); - for ( ; signal_it != signal_end; ++signal_it) { - const QDBusIntrospection::Signal &s = *signal_it; + for (const QDBusIntrospection::Signal &s : std::as_const(data->signals_)) { Method mm; mm.name = s.name.toLatin1(); @@ -331,7 +294,7 @@ void QDBusMetaObjectGenerator::parseSignals() bool ok = true; // build the output argument list - for (int i = 0; i < s.outputArgs.count(); ++i) { + for (qsizetype i = 0; i < s.outputArgs.size(); ++i) { const QDBusIntrospection::Argument &arg = s.outputArgs.at(i); Type type = findType(arg.type.toLatin1(), s.annotations, "Out", i); @@ -351,7 +314,7 @@ void QDBusMetaObjectGenerator::parseSignals() // convert the last commas: if (!mm.parameterNames.isEmpty()) - prototype[prototype.length() - 1] = ')'; + prototype[prototype.size() - 1] = ')'; else prototype.append(')'); @@ -365,10 +328,7 @@ void QDBusMetaObjectGenerator::parseSignals() void QDBusMetaObjectGenerator::parseProperties() { - QDBusIntrospection::Properties::ConstIterator prop_it = data->properties.constBegin(); - QDBusIntrospection::Properties::ConstIterator prop_end = data->properties.constEnd(); - for ( ; prop_it != prop_end; ++prop_it) { - const QDBusIntrospection::Property &p = *prop_it; + for (const QDBusIntrospection::Property &p : std::as_const(data->properties)) { Property mp; Type type = findType(p.type.toLatin1(), p.annotations); if (type.id == QMetaType::UnknownType) @@ -394,14 +354,11 @@ void QDBusMetaObjectGenerator::parseProperties() // Returns the sum of all parameters (including return type) for the given // \a map of methods. This is needed for calculating the size of the methods' // parameter type/name meta-data. -int QDBusMetaObjectGenerator::aggregateParameterCount(const QMap<QByteArray, Method> &map) +qsizetype QDBusMetaObjectGenerator::aggregateParameterCount(const MethodMap &map) { - int sum = 0; - QMap<QByteArray, Method>::const_iterator it; - for (it = map.constBegin(); it != map.constEnd(); ++it) { - const Method &m = it.value(); + qsizetype sum = 0; + for (const Method &m : map) sum += m.inputTypes.size() + qMax(qsizetype(1), m.outputTypes.size()); - } return sum; } @@ -411,61 +368,67 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) // with a few modifications to make it cleaner QString className = interface; - className.replace(QLatin1Char('.'), QLatin1String("::")); + className.replace(u'.', "::"_L1); if (className.isEmpty()) - className = QLatin1String("QDBusInterface"); + className = "QDBusInterface"_L1; - QVarLengthArray<int> idata; - idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(int)); + QVarLengthArray<uint> idata; + idata.resize(sizeof(QDBusMetaObjectPrivate) / sizeof(uint)); - int methodParametersDataSize = + qsizetype methodParametersDataSize = ((aggregateParameterCount(signals_) + aggregateParameterCount(methods)) * 2) // types and parameter names - - signals_.count() // return "parameters" don't have names - - methods.count(); // ditto + - signals_.size() // return "parameters" don't have names + - methods.size(); // ditto QDBusMetaObjectPrivate *header = reinterpret_cast<QDBusMetaObjectPrivate *>(idata.data()); - static_assert(QMetaObjectPrivate::OutputRevision == 10, "QtDBus meta-object generator should generate the same version as moc"); + static_assert(QMetaObjectPrivate::OutputRevision == 12, "QtDBus meta-object generator should generate the same version as moc"); header->revision = QMetaObjectPrivate::OutputRevision; header->className = 0; header->classInfoCount = 0; header->classInfoData = 0; - header->methodCount = signals_.count() + methods.count(); - header->methodData = idata.size(); - header->propertyCount = properties.count(); - header->propertyData = header->methodData + header->methodCount * QMetaObjectPrivate::IntsPerMethod + methodParametersDataSize; + header->methodCount = int(signals_.size() + methods.size()); + header->methodData = int(idata.size()); + header->propertyCount = int(properties.size()); + header->propertyData = int(header->methodData + header->methodCount * + QMetaObjectPrivate::IntsPerMethod + methodParametersDataSize); header->enumeratorCount = 0; header->enumeratorData = 0; header->constructorCount = 0; header->constructorData = 0; header->flags = RequiresVariantMetaObject; - header->signalCount = signals_.count(); + header->signalCount = signals_.size(); // These are specific to QDBusMetaObject: - header->propertyDBusData = header->propertyData + header->propertyCount * QMetaObjectPrivate::IntsPerProperty; - header->methodDBusData = header->propertyDBusData + header->propertyCount * intsPerProperty; + header->propertyDBusData = int(header->propertyData + header->propertyCount + * QMetaObjectPrivate::IntsPerProperty); + header->methodDBusData = int(header->propertyDBusData + header->propertyCount * intsPerProperty); - int data_size = idata.size() + + qsizetype data_size = idata.size() + (header->methodCount * (QMetaObjectPrivate::IntsPerMethod+intsPerMethod)) + methodParametersDataSize + (header->propertyCount * (QMetaObjectPrivate::IntsPerProperty+intsPerProperty)); - for (const Method &mm : qAsConst(signals_)) - data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); - for (const Method &mm : qAsConst(methods)) - data_size += 2 + mm.inputTypes.count() + mm.outputTypes.count(); + + // Signals must be added before other methods, to match moc. + std::array<std::reference_wrapper<const MethodMap>, 2> methodMaps = { signals_, methods }; + + for (const auto &methodMap : methodMaps) { + for (const Method &mm : methodMap.get()) + data_size += 2 + mm.inputTypes.size() + mm.outputTypes.size(); + } idata.resize(data_size + 1); QMetaStringTable strings(className.toLatin1()); - int offset = header->methodData; - int parametersOffset = offset + header->methodCount * QMetaObjectPrivate::IntsPerMethod; - int signatureOffset = header->methodDBusData; - int typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod; + qsizetype offset = header->methodData; + qsizetype parametersOffset = offset + header->methodCount * QMetaObjectPrivate::IntsPerMethod; + qsizetype signatureOffset = header->methodDBusData; + qsizetype typeidOffset = header->methodDBusData + header->methodCount * intsPerMethod; idata[typeidOffset++] = 0; // eod - int totalMetaTypeCount = properties.count(); + qsizetype totalMetaTypeCount = properties.size(); ++totalMetaTypeCount; // + 1 for metatype of dynamic metaobject - for (const auto& methodContainer: {signals_, methods}) { - for (const auto& method: methodContainer) { - int argc = method.inputTypes.size() + qMax(qsizetype(0), method.outputTypes.size() - 1); + for (const auto &methodMap : methodMaps) { + for (const Method &mm : methodMap.get()) { + qsizetype argc = mm.inputTypes.size() + qMax(qsizetype(0), mm.outputTypes.size() - 1); totalMetaTypeCount += argc + 1; } } @@ -473,15 +436,11 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) int propertyId = 0; // add each method: - int currentMethodMetaTypeOffset = properties.count() + 1; - for (int x = 0; x < 2; ++x) { - // Signals must be added before other methods, to match moc. - QMap<QByteArray, Method> &map = (x == 0) ? signals_ : methods; - for (QMap<QByteArray, Method>::ConstIterator it = map.constBegin(); - it != map.constEnd(); ++it) { - const Method &mm = it.value(); + qsizetype currentMethodMetaTypeOffset = properties.size() + 1; - int argc = mm.inputTypes.size() + qMax(qsizetype(0), mm.outputTypes.size() - 1); + for (const auto &methodMap : methodMaps) { + for (const Method &mm : methodMap.get()) { + qsizetype argc = mm.inputTypes.size() + qMax(qsizetype(0), mm.outputTypes.size() - 1); idata[offset++] = strings.enter(mm.name); idata[offset++] = argc; @@ -491,7 +450,7 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) idata[offset++] = currentMethodMetaTypeOffset; // Parameter types - for (int i = -1; i < argc; ++i) { + for (qsizetype i = -1; i < argc; ++i) { int type; QByteArray typeName; if (i < 0) { // Return type @@ -511,29 +470,29 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) // Output parameters are references; type id not available typeName = QMetaType(type).name(); typeName.append('&'); + type = QMetaType::UnknownType; } - Q_ASSERT(type != QMetaType::UnknownType); int typeInfo; if (!typeName.isEmpty()) typeInfo = IsUnresolvedType | strings.enter(typeName); else typeInfo = type; - metaTypes[currentMethodMetaTypeOffset++] = QMetaType (type); + metaTypes[currentMethodMetaTypeOffset++] = QMetaType(type); idata[parametersOffset++] = typeInfo; } // Parameter names - for (int i = 0; i < argc; ++i) + for (qsizetype i = 0; i < argc; ++i) idata[parametersOffset++] = strings.enter(mm.parameterNames.at(i)); idata[signatureOffset++] = typeidOffset; - idata[typeidOffset++] = mm.inputTypes.count(); - memcpy(idata.data() + typeidOffset, mm.inputTypes.data(), mm.inputTypes.count() * sizeof(int)); - typeidOffset += mm.inputTypes.count(); + idata[typeidOffset++] = mm.inputTypes.size(); + memcpy(idata.data() + typeidOffset, mm.inputTypes.data(), mm.inputTypes.size() * sizeof(uint)); + typeidOffset += mm.inputTypes.size(); idata[signatureOffset++] = typeidOffset; - idata[typeidOffset++] = mm.outputTypes.count(); - memcpy(idata.data() + typeidOffset, mm.outputTypes.data(), mm.outputTypes.count() * sizeof(int)); - typeidOffset += mm.outputTypes.count(); + idata[typeidOffset++] = mm.outputTypes.size(); + memcpy(idata.data() + typeidOffset, mm.outputTypes.data(), mm.outputTypes.size() * sizeof(uint)); + typeidOffset += mm.outputTypes.size(); } } @@ -546,12 +505,9 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) // add each property signatureOffset = header->propertyDBusData; - for (QMap<QByteArray, Property>::ConstIterator it = properties.constBegin(); - it != properties.constEnd(); ++it) { - const Property &mp = it.value(); - + for (const auto &[name, mp] : std::as_const(properties).asKeyValueRange()) { // form is name, typeinfo, flags - idata[offset++] = strings.enter(it.key()); // name + idata[offset++] = strings.enter(name); Q_ASSERT(mp.type != QMetaType::UnknownType); idata[offset++] = mp.type; idata[offset++] = mp.flags; @@ -572,7 +528,7 @@ void QDBusMetaObjectGenerator::write(QDBusMetaObject *obj) strings.writeBlob(string_data); uint *uint_data = new uint[idata.size()]; - memcpy(uint_data, idata.data(), idata.size() * sizeof(int)); + memcpy(uint_data, idata.data(), idata.size() * sizeof(uint)); // put the metaobject together obj->d.data = uint_data; @@ -589,7 +545,7 @@ void QDBusMetaObjectGenerator::writeWithoutXml(const QString &interface) { // no XML definition QString tmp(interface); - tmp.replace(QLatin1Char('.'), QLatin1String("::")); + tmp.replace(u'.', "::"_L1); QByteArray name(tmp.toLatin1()); QDBusMetaObjectPrivate *header = new QDBusMetaObjectPrivate; @@ -628,13 +584,13 @@ QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, con bool us = it.key() == interface; QDBusMetaObject *obj = cache.value(it.key(), 0); - if ( !obj && ( us || !interface.startsWith( QLatin1String("local.") ) ) ) { + if (!obj && (us || !interface.startsWith("local."_L1 ))) { // not in cache; create obj = new QDBusMetaObject; QDBusMetaObjectGenerator generator(it.key(), it.value().constData()); generator.write(obj); - if ( (obj->cached = !it.key().startsWith( QLatin1String("local.") )) ) + if ((obj->cached = !it.key().startsWith("local."_L1))) // cache it cache.insert(it.key(), obj); else if (!us) @@ -670,7 +626,7 @@ QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, con merged.properties.insert(it.value()->properties); } - merged.name = QLatin1String("local.Merged"); + merged.name = "local.Merged"_L1; merged.introspection.clear(); we = new QDBusMetaObject; @@ -682,8 +638,7 @@ QDBusMetaObject *QDBusMetaObject::createMetaObject(const QString &interface, con // mark as an error error = QDBusError(QDBusError::UnknownInterface, - QLatin1String("Interface '%1' was not found") - .arg(interface)); + "Interface '%1' was not found"_L1.arg(interface)); return nullptr; } |