diff options
Diffstat (limited to 'src/dbus/qdbusargument.h')
-rw-r--r-- | src/dbus/qdbusargument.h | 217 |
1 files changed, 60 insertions, 157 deletions
diff --git a/src/dbus/qdbusargument.h b/src/dbus/qdbusargument.h index a6d4e9cd25..70f6703d2f 100644 --- a/src/dbus/qdbusargument.h +++ b/src/dbus/qdbusargument.h @@ -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 #ifndef QDBUSARGUMENT_H #define QDBUSARGUMENT_H @@ -76,16 +40,14 @@ public: QDBusArgument(); QDBusArgument(const QDBusArgument &other); -#ifdef Q_COMPILER_RVALUE_REFS - QDBusArgument(QDBusArgument &&other) Q_DECL_NOTHROW : d(other.d) { other.d = nullptr; } - QDBusArgument &operator=(QDBusArgument &&other) Q_DECL_NOTHROW { swap(other); return *this; } -#endif + QDBusArgument(QDBusArgument &&other) noexcept : d(other.d) { other.d = nullptr; } + QDBusArgument &operator=(QDBusArgument &&other) noexcept { swap(other); return *this; } QDBusArgument &operator=(const QDBusArgument &other); ~QDBusArgument(); - void swap(QDBusArgument &other) Q_DECL_NOTHROW { qSwap(d, other.d); } + void swap(QDBusArgument &other) noexcept { qt_ptr_swap(d, other.d); } - // used for marshalling (Qt -> D-BUS) + // used for marshalling (Qt -> D-Bus) QDBusArgument &operator<<(uchar arg); QDBusArgument &operator<<(bool arg); QDBusArgument &operator<<(short arg); @@ -105,16 +67,20 @@ public: void beginStructure(); void endStructure(); - void beginArray(int elementMetaTypeId); + void beginArray(int elementMetaTypeId) + { beginArray(QMetaType(elementMetaTypeId)); } + void beginArray(QMetaType elementMetaType); void endArray(); - void beginMap(int keyMetaTypeId, int valueMetaTypeId); + void beginMap(int keyMetaTypeId, int valueMetaTypeId) + { beginMap(QMetaType(keyMetaTypeId), QMetaType(valueMetaTypeId)); } + void beginMap(QMetaType keyMetaType, QMetaType valueMetaType); void endMap(); void beginMapEntry(); void endMapEntry(); void appendVariant(const QVariant &v); - // used for de-marshalling (D-BUS -> Qt) + // used for de-marshalling (D-Bus -> Qt) QString currentSignature() const; ElementType currentType() const; @@ -152,44 +118,35 @@ protected: friend class QDBusArgumentPrivate; mutable QDBusArgumentPrivate *d; }; -Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QDBusArgument) +Q_DECLARE_SHARED(QDBusArgument) QT_END_NAMESPACE -Q_DECLARE_METATYPE(QDBusArgument) +QT_DECL_METATYPE_EXTERN(QDBusArgument, Q_DBUS_EXPORT) QT_BEGIN_NAMESPACE -template<typename T> inline T qdbus_cast(const QDBusArgument &arg -#ifndef Q_QDOC -, T * = nullptr -#endif - ) +template<typename T> inline T qdbus_cast(const QDBusArgument &arg) { T item; arg >> item; return item; } -template<typename T> inline T qdbus_cast(const QVariant &v -#ifndef Q_QDOC -, T * = nullptr -#endif - ) +template<typename T> inline T qdbus_cast(const QVariant &v) { - int id = v.userType(); - if (id == qMetaTypeId<QDBusArgument>()) + if (v.metaType() == QMetaType::fromType<QDBusArgument>()) return qdbus_cast<T>(qvariant_cast<QDBusArgument>(v)); else return qvariant_cast<T>(v); } // specialize for QVariant, allowing it to be used in place of QDBusVariant -template<> inline QVariant qdbus_cast<QVariant>(const QDBusArgument &arg, QVariant *) +template<> inline QVariant qdbus_cast<QVariant>(const QDBusArgument &arg) { QDBusVariant item; arg >> item; return item.variant(); } -template<> inline QVariant qdbus_cast<QVariant>(const QVariant &v, QVariant *) +template<> inline QVariant qdbus_cast<QVariant>(const QVariant &v) { return qdbus_cast<QDBusVariant>(v).variant(); } @@ -233,11 +190,11 @@ Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QLineF &li Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QLineF &line); #endif -template<template <typename> class Container, typename T> +template<template <typename> class Container, typename T, + typename = typename Container<T>::iterator> inline QDBusArgument &operator<<(QDBusArgument &arg, const Container<T> &list) { - int id = qMetaTypeId<T>(); - arg.beginArray(id); + arg.beginArray(QMetaType::fromType<T>()); typename Container<T>::const_iterator it = list.begin(); typename Container<T>::const_iterator end = list.end(); for ( ; it != end; ++it) @@ -246,7 +203,8 @@ inline QDBusArgument &operator<<(QDBusArgument &arg, const Container<T> &list) return arg; } -template<template <typename> class Container, typename T> +template<template <typename> class Container, typename T, + typename = typename Container<T>::iterator> inline const QDBusArgument &operator>>(const QDBusArgument &arg, Container<T> &list) { arg.beginArray(); @@ -261,67 +219,51 @@ inline const QDBusArgument &operator>>(const QDBusArgument &arg, Container<T> &l return arg; } -// QList specializations -template<typename T> -inline QDBusArgument &operator<<(QDBusArgument &arg, const QList<T> &list) +inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantList &list) { - int id = qMetaTypeId<T>(); - arg.beginArray(id); - typename QList<T>::ConstIterator it = list.constBegin(); - typename QList<T>::ConstIterator end = list.constEnd(); - for ( ; it != end; ++it) - arg << *it; + arg.beginArray(QMetaType::fromType<QDBusVariant>()); + for (const QVariant &value : list) + arg << QDBusVariant(value); arg.endArray(); return arg; } -template<typename T> -inline const QDBusArgument &operator>>(const QDBusArgument &arg, QList<T> &list) +// Specializations for associative containers +template <template <typename, typename> class Container, typename Key, typename T, + QtPrivate::IfAssociativeIteratorHasKeyAndValue<typename Container<Key, T>::iterator> = true> +inline QDBusArgument &operator<<(QDBusArgument &arg, const Container<Key, T> &map) { - arg.beginArray(); - list.clear(); - while (!arg.atEnd()) { - T item; - arg >> item; - list.push_back(item); + arg.beginMap(QMetaType::fromType<Key>(), QMetaType::fromType<T>()); + auto it = map.begin(); + auto end = map.end(); + for ( ; it != end; ++it) { + arg.beginMapEntry(); + arg << it.key() << it.value(); + arg.endMapEntry(); } - arg.endArray(); - - return arg; -} - -inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantList &list) -{ - int id = qMetaTypeId<QDBusVariant>(); - arg.beginArray(id); - QVariantList::ConstIterator it = list.constBegin(); - QVariantList::ConstIterator end = list.constEnd(); - for ( ; it != end; ++it) - arg << QDBusVariant(*it); - arg.endArray(); + arg.endMap(); return arg; } -// QMap specializations -template<typename Key, typename T> -inline QDBusArgument &operator<<(QDBusArgument &arg, const QMap<Key, T> &map) +template <template <typename, typename> class Container, typename Key, typename T, + QtPrivate::IfAssociativeIteratorHasFirstAndSecond<typename Container<Key, T>::iterator> = true> +inline QDBusArgument &operator<<(QDBusArgument &arg, const Container<Key, T> &map) { - int kid = qMetaTypeId<Key>(); - int vid = qMetaTypeId<T>(); - arg.beginMap(kid, vid); - typename QMap<Key, T>::ConstIterator it = map.constBegin(); - typename QMap<Key, T>::ConstIterator end = map.constEnd(); + arg.beginMap(QMetaType::fromType<Key>(), QMetaType::fromType<T>()); + auto it = map.begin(); + auto end = map.end(); for ( ; it != end; ++it) { arg.beginMapEntry(); - arg << it.key() << it.value(); + arg << it->first << it->second; arg.endMapEntry(); } arg.endMap(); return arg; } -template<typename Key, typename T> -inline const QDBusArgument &operator>>(const QDBusArgument &arg, QMap<Key, T> &map) +template <template <typename, typename> class Container, typename Key, typename T, + QtPrivate::IfAssociativeIteratorHasKeyAndValue<typename Container<Key, T>::iterator> = true> +inline const QDBusArgument &operator>>(const QDBusArgument &arg, Container<Key, T> &map) { arg.beginMap(); map.clear(); @@ -330,7 +272,7 @@ inline const QDBusArgument &operator>>(const QDBusArgument &arg, QMap<Key, T> &m T value; arg.beginMapEntry(); arg >> key >> value; - map.insertMulti(key, value); + map.insert(key, value); arg.endMapEntry(); } arg.endMap(); @@ -339,47 +281,10 @@ inline const QDBusArgument &operator>>(const QDBusArgument &arg, QMap<Key, T> &m inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantMap &map) { - arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>()); - QVariantMap::ConstIterator it = map.constBegin(); - QVariantMap::ConstIterator end = map.constEnd(); - for ( ; it != end; ++it) { - arg.beginMapEntry(); - arg << it.key() << QDBusVariant(it.value()); - arg.endMapEntry(); - } - arg.endMap(); - return arg; -} - -// QHash specializations -template<typename Key, typename T> -inline QDBusArgument &operator<<(QDBusArgument &arg, const QHash<Key, T> &map) -{ - int kid = qMetaTypeId<Key>(); - int vid = qMetaTypeId<T>(); - arg.beginMap(kid, vid); - typename QHash<Key, T>::ConstIterator it = map.constBegin(); - typename QHash<Key, T>::ConstIterator end = map.constEnd(); - for ( ; it != end; ++it) { - arg.beginMapEntry(); - arg << it.key() << it.value(); - arg.endMapEntry(); - } - arg.endMap(); - return arg; -} - -template<typename Key, typename T> -inline const QDBusArgument &operator>>(const QDBusArgument &arg, QHash<Key, T> &map) -{ - arg.beginMap(); - map.clear(); - while (!arg.atEnd()) { - Key key; - T value; + arg.beginMap(QMetaType::fromType<QString>(), QMetaType::fromType<QDBusVariant>()); + for (const auto &[key, value] : map.asKeyValueRange()) { arg.beginMapEntry(); - arg >> key >> value; - map.insertMulti(key, value); + arg << key << QDBusVariant(value); arg.endMapEntry(); } arg.endMap(); @@ -388,12 +293,10 @@ inline const QDBusArgument &operator>>(const QDBusArgument &arg, QHash<Key, T> & inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantHash &map) { - arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>()); - QVariantHash::ConstIterator it = map.constBegin(); - QVariantHash::ConstIterator end = map.constEnd(); - for ( ; it != end; ++it) { + arg.beginMap(QMetaType::fromType<QString>(), QMetaType::fromType<QDBusVariant>()); + for (const auto &[key, value] : map.asKeyValueRange()) { arg.beginMapEntry(); - arg << it.key() << QDBusVariant(it.value()); + arg << key << QDBusVariant(value); arg.endMapEntry(); } arg.endMap(); @@ -401,7 +304,7 @@ inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantHash &map) } template <typename T1, typename T2> -inline QDBusArgument &operator<<(QDBusArgument &arg, const QPair<T1, T2> &pair) +inline QDBusArgument &operator<<(QDBusArgument &arg, const std::pair<T1, T2> &pair) { arg.beginStructure(); arg << pair.first << pair.second; @@ -410,7 +313,7 @@ inline QDBusArgument &operator<<(QDBusArgument &arg, const QPair<T1, T2> &pair) } template <typename T1, typename T2> -inline const QDBusArgument &operator>>(const QDBusArgument &arg, QPair<T1, T2> &pair) +inline const QDBusArgument &operator>>(const QDBusArgument &arg, std::pair<T1, T2> &pair) { arg.beginStructure(); arg >> pair.first >> pair.second; |