1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDBus module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDBUSARGUMENT_H
43 #define QDBUSARGUMENT_H
45 #include <QtCore/qbytearray.h>
46 #include <QtCore/qhash.h>
47 #include <QtCore/qglobal.h>
48 #include <QtCore/qlist.h>
49 #include <QtCore/qmap.h>
50 #include <QtCore/qstring.h>
51 #include <QtCore/qstringlist.h>
52 #include <QtCore/qvariant.h>
53 #include <QtDBus/qdbusextratypes.h>
54 #include <QtDBus/qdbusmacros.h>
63 class QDBusUnixFileDescriptor;
65 class QDBusArgumentPrivate;
66 class QDBusDemarshaller;
67 class QDBusMarshaller;
68 class Q_DBUS_EXPORT QDBusArgument
82 QDBusArgument(const QDBusArgument &other);
83 QDBusArgument &operator=(const QDBusArgument &other);
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);
104 void beginStructure();
106 void beginArray(int elementMetaTypeId);
108 void beginMap(int keyMetaTypeId, int valueMetaTypeId);
110 void beginMapEntry();
113 void appendVariant(const QVariant &v);
115 // used for de-marshalling (D-BUS -> Qt)
116 QString currentSignature() const;
117 ElementType currentType() const;
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;
136 void beginStructure() const;
137 void endStructure() const;
138 void beginArray() const;
139 void endArray() const;
140 void beginMap() const;
142 void beginMapEntry() const;
143 void endMapEntry() const;
146 QVariant asVariant() const;
149 QDBusArgument(QDBusArgumentPrivate *d);
150 friend class QDBusArgumentPrivate;
151 mutable QDBusArgumentPrivate *d;
154 template<typename T> inline T qdbus_cast(const QDBusArgument &arg
165 template<typename T> inline T qdbus_cast(const QVariant &v
171 int id = v.userType();
172 if (id == qMetaTypeId<QDBusArgument>())
173 return qdbus_cast<T>(qvariant_cast<QDBusArgument>(v));
175 return qvariant_cast<T>(v);
178 // specialize for QVariant, allowing it to be used in place of QDBusVariant
179 template<> inline QVariant qdbus_cast<QVariant>(const QDBusArgument &arg, QVariant *)
183 return item.variant();
185 template<> inline QVariant qdbus_cast<QVariant>(const QVariant &v, QVariant *)
187 return qdbus_cast<QDBusVariant>(v).variant();
190 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QVariant &v);
193 #ifndef QDBUS_NO_SPECIALTYPES
195 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QDate &date);
196 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QDate &date);
198 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QTime &time);
199 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QTime &time);
201 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QDateTime &dt);
202 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QDateTime &dt);
204 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QRect &rect);
205 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QRect &rect);
207 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QRectF &rect);
208 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QRectF &rect);
210 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QSize &size);
211 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QSize &size);
213 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QSizeF &size);
214 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QSizeF &size);
216 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QPoint &pt);
217 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QPoint &pt);
219 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QPointF &pt);
220 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QPointF &pt);
222 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QLine &line);
223 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QLine &line);
225 Q_DBUS_EXPORT const QDBusArgument &operator>>(const QDBusArgument &a, QLineF &line);
226 Q_DBUS_EXPORT QDBusArgument &operator<<(QDBusArgument &a, const QLineF &line);
229 template<template <typename> class Container, typename T>
230 inline QDBusArgument &operator<<(QDBusArgument &arg, const Container<T> &list)
232 int id = qMetaTypeId<T>();
234 typename Container<T>::const_iterator it = list.begin();
235 typename Container<T>::const_iterator end = list.end();
236 for ( ; it != end; ++it)
242 template<template <typename> class Container, typename T>
243 inline const QDBusArgument &operator>>(const QDBusArgument &arg, Container<T> &list)
247 while (!arg.atEnd()) {
250 list.push_back(item);
257 // QList specializations
259 inline QDBusArgument &operator<<(QDBusArgument &arg, const QList<T> &list)
261 int id = qMetaTypeId<T>();
263 typename QList<T>::ConstIterator it = list.constBegin();
264 typename QList<T>::ConstIterator end = list.constEnd();
265 for ( ; it != end; ++it)
272 inline const QDBusArgument &operator>>(const QDBusArgument &arg, QList<T> &list)
276 while (!arg.atEnd()) {
279 list.push_back(item);
286 inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantList &list)
288 int id = qMetaTypeId<QDBusVariant>();
290 QVariantList::ConstIterator it = list.constBegin();
291 QVariantList::ConstIterator end = list.constEnd();
292 for ( ; it != end; ++it)
293 arg << QDBusVariant(*it);
298 // QMap specializations
299 template<typename Key, typename T>
300 inline QDBusArgument &operator<<(QDBusArgument &arg, const QMap<Key, T> &map)
302 int kid = qMetaTypeId<Key>();
303 int vid = qMetaTypeId<T>();
304 arg.beginMap(kid, vid);
305 typename QMap<Key, T>::ConstIterator it = map.constBegin();
306 typename QMap<Key, T>::ConstIterator end = map.constEnd();
307 for ( ; it != end; ++it) {
309 arg << it.key() << it.value();
316 template<typename Key, typename T>
317 inline const QDBusArgument &operator>>(const QDBusArgument &arg, QMap<Key, T> &map)
321 while (!arg.atEnd()) {
326 map.insertMulti(key, value);
333 inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantMap &map)
335 arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>());
336 QVariantMap::ConstIterator it = map.constBegin();
337 QVariantMap::ConstIterator end = map.constEnd();
338 for ( ; it != end; ++it) {
340 arg << it.key() << QDBusVariant(it.value());
347 // QHash specializations
348 template<typename Key, typename T>
349 inline QDBusArgument &operator<<(QDBusArgument &arg, const QHash<Key, T> &map)
351 int kid = qMetaTypeId<Key>();
352 int vid = qMetaTypeId<T>();
353 arg.beginMap(kid, vid);
354 typename QHash<Key, T>::ConstIterator it = map.constBegin();
355 typename QHash<Key, T>::ConstIterator end = map.constEnd();
356 for ( ; it != end; ++it) {
358 arg << it.key() << it.value();
365 template<typename Key, typename T>
366 inline const QDBusArgument &operator>>(const QDBusArgument &arg, QHash<Key, T> &map)
370 while (!arg.atEnd()) {
375 map.insertMulti(key, value);
382 inline QDBusArgument &operator<<(QDBusArgument &arg, const QVariantHash &map)
384 arg.beginMap(QVariant::String, qMetaTypeId<QDBusVariant>());
385 QVariantHash::ConstIterator it = map.constBegin();
386 QVariantHash::ConstIterator end = map.constEnd();
387 for ( ; it != end; ++it) {
389 arg << it.key() << QDBusVariant(it.value());
399 Q_DECLARE_METATYPE(QDBusArgument)