1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QDBUSARGUMENT_H
41#define QDBUSARGUMENT_H
42
43#include <QtDBus/qtdbusglobal.h>
44#include <QtCore/qbytearray.h>
45#include <QtCore/qhash.h>
46#include <QtCore/qglobal.h>
47#include <QtCore/qlist.h>
48#include <QtCore/qmap.h>
49#include <QtCore/qstring.h>
50#include <QtCore/qstringlist.h>
51#include <QtCore/qvariant.h>
52#include <QtDBus/qdbusextratypes.h>
53
54#ifndef QT_NO_DBUS
55
56QT_BEGIN_NAMESPACE
57
58
59class QDBusUnixFileDescriptor;
60
61class QDBusArgumentPrivate;
62class QDBusDemarshaller;
63class QDBusMarshaller;
64class Q_DBUS_EXPORT QDBusArgument
65{
66public:
67 enum ElementType {
68 BasicType,
69 VariantType,
70 ArrayType,
71 StructureType,
72 MapType,
73 MapEntryType,
74 UnknownType = -1
75 };
76
77 QDBusArgument();
78 QDBusArgument(const QDBusArgument &other);
79 QDBusArgument(QDBusArgument &&other) noexcept : d(other.d) { other.d = nullptr; }
80 QDBusArgument &operator=(QDBusArgument &&other) noexcept { swap(other); return *this; }
81 QDBusArgument &operator=(const QDBusArgument &other);
82 ~QDBusArgument();
83
84 void swap(QDBusArgument &other) noexcept { qSwap(d, other.d); }
85
86 // used for marshalling (Qt -> D-BUS)
87 QDBusArgument &operator<<(uchar arg);
88 QDBusArgument &operator<<(bool arg);
89 QDBusArgument &operator<<(short arg);
90 QDBusArgument &operator<<(ushort arg);
91 QDBusArgument &operator<<(int arg);
92 QDBusArgument &operator<<(uint arg);
93 QDBusArgument &operator<<(qlonglong arg);
94 QDBusArgument &operator<<(qulonglong arg);
95 QDBusArgument &operator<<(double arg);
96 QDBusArgument &operator<<(const QString &arg);
97 QDBusArgument &operator<<(const QDBusVariant &arg);
98 QDBusArgument &operator<<(const QDBusObjectPath &arg);
99 QDBusArgument &operator<<(const QDBusSignature &arg);
100 QDBusArgument &operator<<(const QDBusUnixFileDescriptor &arg);
101 QDBusArgument &operator<<(const QStringList &arg);
102 QDBusArgument &operator<<(const QByteArray &arg);
103
104 void beginStructure();
105 void endStructure();
106 void beginArray(int elementMetaTypeId);
107 void endArray();
108 void beginMap(int keyMetaTypeId, int valueMetaTypeId);
109 void endMap();
110 void beginMapEntry();
111 void endMapEntry();
112
113 void appendVariant(const QVariant &v);
114
115 // used for de-marshalling (D-BUS -> Qt)
116 QString currentSignature() const;
117 ElementType currentType() const;
118
119 const QDBusArgument &operator>>(uchar &arg) const;
120 const QDBusArgument &operator>>(bool &arg) const;
121 const QDBusArgument &operator>>(short &arg) const;
122 const QDBusArgument &operator>>(ushort &arg) const;
123 const QDBusArgument &operator>>(int &arg) const;
124 const QDBusArgument &operator>>(uint &arg) const;
125 const QDBusArgument &operator>>(qlonglong &arg) const;
126 const QDBusArgument &operator>>(qulonglong &arg) const;
127 const QDBusArgument &operator>>(double &arg) const;
128 const QDBusArgument &operator>>(QString &arg) const;
129 const QDBusArgument &operator>>(QDBusVariant &arg) const;
130 const QDBusArgument &operator>>(QDBusObjectPath &arg) const;
131 const QDBusArgument &operator>>(QDBusSignature &arg) const;
132 const QDBusArgument &operator>>(QDBusUnixFileDescriptor &arg) const;
133 const QDBusArgument &operator>>(QStringList &arg) const;
134 const QDBusArgument &operator>>(QByteArray &arg) const;
135
136 void beginStructure() const;
137 void endStructure() const;
138 void beginArray() const;
139 void endArray() const;
140 void beginMap() const;
141 void endMap() const;
142 void beginMapEntry() const;
143 void endMapEntry() const;
144 bool atEnd() const;
145
146 QVariant asVariant() const;
147
148protected:
149 QDBusArgument(QDBusArgumentPrivate *d);
150 friend class QDBusArgumentPrivate;
151 mutable QDBusArgumentPrivate *d;
152};
153Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QDBusArgument)
154
155QT_END_NAMESPACE
156Q_DECLARE_METATYPE(QDBusArgument)
157QT_BEGIN_NAMESPACE
158
159// ### Qt6: remove the defaulted T * = nullptr from these two (MSVC6 work-around):
160template<typename T> inline T qdbus_cast(const QDBusArgument &arg, T * = nullptr)
161{
162 T item;
163 arg >> item;
164 return item;
165}
166
167template<typename T> inline T qdbus_cast(const QVariant &v, T * = nullptr)
168{
169 int id = v.userType();
170 if (id == qMetaTypeId<QDBusArgument>())
171 return qdbus_cast<T>(qvariant_cast<QDBusArgument>(v));
172 else
173 return qvariant_cast<T>(v);
174}
175
176// specialize for QVariant, allowing it to be used in place of QDBusVariant
177template<> inline QVariant qdbus_cast<QVariant>(const QDBusArgument &arg, QVariant *)
178{
179 QDBusVariant item;
180 arg >> item;
181 return item.variant();
182}
183template<> inline QVariant qdbus_cast<QVariant>(const QVariant &v, QVariant *)
184{
185 return qdbus_cast<QDBusVariant>(v).variant();
186}
187
188Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QVariant &v);
189
190// QVariant types
191#ifndef QDBUS_NO_SPECIALTYPES
192
193Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QDate &date);
194Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QDate &date);
195
196Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QTime &time);
197Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QTime &time);
198
199Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QDateTime &dt);
200Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QDateTime &dt);
201
202Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QRect &rect);
203Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QRect &rect);
204
205Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QRectF &rect);
206Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QRectF &rect);
207
208Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QSize &size);
209Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QSize &size);
210
211Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QSizeF &size);
212Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QSizeF &size);
213
214Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QPoint &pt);
215Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QPoint &pt);
216
217Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QPointF &pt);
218Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QPointF &pt);
219
220Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QLine &line);
221Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QLine &line);
222
223Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QLineF &line);
224Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QLineF &line);
225#endif
226
227template<template <typename> class Container, typename T>
228inline QDBusArgument &operator<<(QDBusArgument &arg, const Container<T> &list)
229{
230 int id = qMetaTypeId<T>();
231 arg.beginArray(id);
232 typename Container<T>::const_iterator it = list.begin();
233 typename Container<T>::const_iterator end = list.end();
234 for ( ; it != end; ++it)
235 arg << *it;
236 arg.endArray();
237 return arg;
238}
239
240template<template <typename> class Container, typename T>
241inline const QDBusArgument &operator>>(const QDBusArgument &arg, Container<T> &list)
242{
243 arg.beginArray();
244 list.clear();
245 while (!arg.atEnd()) {
246 T item;
247 arg >> item;
248 list.push_back(item);
249 }
250
251 arg.endArray();
252 return arg;
253}
254
255// QList specializations
256template<typename T>
257inline QDBusArgument &operator<<(QDBusArgument &arg, const QList<T> &list)
258{
259 int id = qMetaTypeId<T>();
260 arg.beginArray(id);
261 typename QList<T>::ConstIterator it = list.constBegin();
262 typename QList<T>::ConstIterator end = list.constEnd();
263 for ( ; it != end; ++it)
264 arg << *it;
265 arg.endArray();
266 return arg;
267}
268
269template<typename T>
270inline const QDBusArgument &operator>>(const QDBusArgument &arg, QList<T> &list)
271{
272 arg.beginArray();
273 list.clear();
274 while (!arg.atEnd()) {
275 T item;
276 arg >> item;
277 list.push_back(item);
278 }
279 arg.endArray();
280
281 return arg;
282}
283
284inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantList &list)
285{
286 int id = qMetaTypeId<QDBusVariant>();
287 arg.beginArray(id);
288 QVariantList::ConstIterator it = list.constBegin();
289 QVariantList::ConstIterator end = list.constEnd();
290 for ( ; it != end; ++it)
291 arg << QDBusVariant(*it);
292 arg.endArray();
293 return arg;
294}
295
296// QMap specializations
297template<typename Key, typename T>
298inline QDBusArgument &operator<<(QDBusArgument &arg, const QMap<Key, T> &map)
299{
300 int kid = qMetaTypeId<Key>();
301 int vid = qMetaTypeId<T>();
302 arg.beginMap(kid, vid);
303 typename QMap<Key, T>::ConstIterator it = map.constBegin();
304 typename QMap<Key, T>::ConstIterator end = map.constEnd();
305 for ( ; it != end; ++it) {
306 arg.beginMapEntry();
307 arg << it.key() << it.value();
308 arg.endMapEntry();
309 }
310 arg.endMap();
311 return arg;
312}
313
314template<typename Key, typename T>
315inline const QDBusArgument &operator>>(const QDBusArgument &arg, QMap<Key, T> &map)
316{
317 arg.beginMap();
318 map.clear();
319 while (!arg.atEnd()) {
320 Key key;
321 T value;
322 arg.beginMapEntry();
323 arg >> key >> value;
324 map.insertMulti(key, value);
325 arg.endMapEntry();
326 }
327 arg.endMap();
328 return arg;
329}
330
331inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantMap &map)
332{
333 arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>());
334 QVariantMap::ConstIterator it = map.constBegin();
335 QVariantMap::ConstIterator end = map.constEnd();
336 for ( ; it != end; ++it) {
337 arg.beginMapEntry();
338 arg << it.key() << QDBusVariant(it.value());
339 arg.endMapEntry();
340 }
341 arg.endMap();
342 return arg;
343}
344
345// QHash specializations
346template<typename Key, typename T>
347inline QDBusArgument &operator<<(QDBusArgument &arg, const QHash<Key, T> &map)
348{
349 int kid = qMetaTypeId<Key>();
350 int vid = qMetaTypeId<T>();
351 arg.beginMap(kid, vid);
352 typename QHash<Key, T>::ConstIterator it = map.constBegin();
353 typename QHash<Key, T>::ConstIterator end = map.constEnd();
354 for ( ; it != end; ++it) {
355 arg.beginMapEntry();
356 arg << it.key() << it.value();
357 arg.endMapEntry();
358 }
359 arg.endMap();
360 return arg;
361}
362
363template<typename Key, typename T>
364inline const QDBusArgument &operator>>(const QDBusArgument &arg, QHash<Key, T> &map)
365{
366 arg.beginMap();
367 map.clear();
368 while (!arg.atEnd()) {
369 Key key;
370 T value;
371 arg.beginMapEntry();
372 arg >> key >> value;
373 map.insertMulti(key, value);
374 arg.endMapEntry();
375 }
376 arg.endMap();
377 return arg;
378}
379
380inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantHash &map)
381{
382 arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>());
383 QVariantHash::ConstIterator it = map.constBegin();
384 QVariantHash::ConstIterator end = map.constEnd();
385 for ( ; it != end; ++it) {
386 arg.beginMapEntry();
387 arg << it.key() << QDBusVariant(it.value());
388 arg.endMapEntry();
389 }
390 arg.endMap();
391 return arg;
392}
393
394template <typename T1, typename T2>
395inline QDBusArgument &operator<<(QDBusArgument &arg, const QPair<T1, T2> &pair)
396{
397 arg.beginStructure();
398 arg << pair.first << pair.second;
399 arg.endStructure();
400 return arg;
401}
402
403template <typename T1, typename T2>
404inline const QDBusArgument &operator>>(const QDBusArgument &arg, QPair<T1, T2> &pair)
405{
406 arg.beginStructure();
407 arg >> pair.first >> pair.second;
408 arg.endStructure();
409 return arg;
410}
411
412QT_END_NAMESPACE
413
414#endif // QT_NO_DBUS
415#endif
416