diff options
Diffstat (limited to 'src/dbus/qdbusmetatype.cpp')
-rw-r--r-- | src/dbus/qdbusmetatype.cpp | 130 |
1 files changed, 59 insertions, 71 deletions
diff --git a/src/dbus/qdbusmetatype.cpp b/src/dbus/qdbusmetatype.cpp index 2d0324d9cb..3ae7589480 100644 --- a/src/dbus/qdbusmetatype.cpp +++ b/src/dbus/qdbusmetatype.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** 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. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qdbusmetatype.h" #include "qdbusmetatype_p.h" @@ -84,19 +48,19 @@ public: void QDBusMetaTypeId::init() { - static QBasicAtomicInt initialized = Q_BASIC_ATOMIC_INITIALIZER(false); + Q_CONSTINIT static QBasicAtomicInt initialized = Q_BASIC_ATOMIC_INITIALIZER(false); // reentrancy is not a problem since everything else is locked on their own // set the guard variable at the end if (!initialized.loadRelaxed()) { - // register our types with Qt Core (calling qMetaTypeId<T>() does this implicitly) - (void)message(); - (void)argument(); - (void)variant(); - (void)objectpath(); - (void)signature(); - (void)error(); - (void)unixfd(); + // register our types with Qt Core + message().registerType(); + argument().registerType(); + variant().registerType(); + objectpath().registerType(); + signature().registerType(); + error().registerType(); + unixfd().registerType(); #ifndef QDBUS_NO_SPECIALTYPES // and register Qt Core's with us @@ -123,6 +87,9 @@ void QDBusMetaTypeId::init() qDBusRegisterMetaType<QList<qlonglong> >(); qDBusRegisterMetaType<QList<qulonglong> >(); qDBusRegisterMetaType<QList<double> >(); + + // plus lists of our own types + qDBusRegisterMetaType<QList<QDBusVariant> >(); qDBusRegisterMetaType<QList<QDBusObjectPath> >(); qDBusRegisterMetaType<QList<QDBusSignature> >(); qDBusRegisterMetaType<QList<QDBusUnixFileDescriptor> >(); @@ -132,9 +99,13 @@ void QDBusMetaTypeId::init() } } -using QDBusCustomTypeHash = QHash<int, QDBusCustomTypeInfo>; -Q_GLOBAL_STATIC(QDBusCustomTypeHash, customTypes) -Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock) +struct QDBusCustomTypes +{ + QReadWriteLock lock; + QHash<int, QDBusCustomTypeInfo> hash; +}; + +Q_GLOBAL_STATIC(QDBusCustomTypes, customTypes) /*! \class QDBusMetaType @@ -158,7 +129,7 @@ Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock) */ /*! - \fn int qDBusRegisterMetaType() + \fn template<typename T> QMetaType qDBusRegisterMetaType() \relates QDBusArgument \threadsafe \since 4.2 @@ -215,12 +186,15 @@ void QDBusMetaType::registerMarshallOperators(QMetaType metaType, MarshallFuncti DemarshallFunction df) { int id = metaType.id(); - auto *ct = customTypes(); - if (id < 0 || !mf || !df || !ct) + if (id < 0 || !mf || !df) return; // error! - QWriteLocker locker(customTypesLock()); - QDBusCustomTypeInfo &info = (*ct)[id]; + auto *ct = customTypes(); + if (!ct) + return; + + QWriteLocker locker(&ct->lock); + QDBusCustomTypeInfo &info = ct->hash[id]; info.marshall = mf; info.demarshall = df; } @@ -233,15 +207,19 @@ void QDBusMetaType::registerMarshallOperators(QMetaType metaType, MarshallFuncti */ bool QDBusMetaType::marshall(QDBusArgument &arg, QMetaType metaType, const void *data) { + auto *ct = customTypes(); + if (!ct) + return false; + int id = metaType.id(); QDBusMetaTypeId::init(); MarshallFunction mf; { - QReadLocker locker(customTypesLock()); - auto *ct = customTypes(); - auto it = ct->constFind(id); - if (it == ct->cend()) + QReadLocker locker(&ct->lock); + + auto it = ct->hash.constFind(id); + if (it == ct->hash.cend()) return false; // non-existent const QDBusCustomTypeInfo &info = *it; @@ -264,15 +242,19 @@ bool QDBusMetaType::marshall(QDBusArgument &arg, QMetaType metaType, const void */ bool QDBusMetaType::demarshall(const QDBusArgument &arg, QMetaType metaType, void *data) { + auto *ct = customTypes(); + if (!ct) + return false; + int id = metaType.id(); QDBusMetaTypeId::init(); DemarshallFunction df; { - QReadLocker locker(customTypesLock()); - auto *ct = customTypes(); - auto it = ct->constFind(id); - if (it == ct->cend()) + QReadLocker locker(&ct->lock); + + auto it = ct->hash.constFind(id); + if (it == ct->hash.cend()) return false; // non-existent const QDBusCustomTypeInfo &info = *it; @@ -390,8 +372,11 @@ QMetaType QDBusMetaType::signatureToMetaType(const char *signature) void QDBusMetaType::registerCustomType(QMetaType type, const QByteArray &signature) { auto *ct = customTypes(); - QWriteLocker locker(customTypesLock()); - auto &info = (*ct)[type.id()]; + if (!ct) + return; + + QWriteLocker locker(&ct->lock); + auto &info = ct->hash[type.id()]; info.signature = signature; // note how marshall/demarshall are not set, the type is never used at runtime } @@ -463,10 +448,13 @@ const char *QDBusMetaType::typeToSignature(QMetaType type) // try the database auto *ct = customTypes(); + if (!ct) + return nullptr; + { - QReadLocker locker(customTypesLock()); - auto it = ct->constFind(type.id()); - if (it == ct->end()) + QReadLocker locker(&ct->lock); + auto it = ct->hash.constFind(type.id()); + if (it == ct->hash.end()) return nullptr; const QDBusCustomTypeInfo &info = *it; @@ -483,11 +471,11 @@ const char *QDBusMetaType::typeToSignature(QMetaType type) { // createSignature will never return a null QByteArray // if there was an error, it'll return "" - QByteArray signature = QDBusArgumentPrivate::createSignature(type.id()); + QByteArray signature = QDBusArgumentPrivate::createSignature(type); // re-acquire lock - QWriteLocker locker(customTypesLock()); - info = &(*ct)[type.id()]; + QWriteLocker locker(&ct->lock); + info = &ct->hash[type.id()]; info->signature = signature; } return info->signature; |