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