1 /* qdbuslisthelper_p.h Helper class to convert to and from QVariantList
3 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
4 * Copyright (C) 2006 Trolltech AS. All rights reserved.
5 * Author: Thiago Macieira <thiago.macieira@trolltech.com>
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 // This file is not part of the public API. This header file may
30 // change from version to version without notice, or even be
37 #ifndef QDBUSTYPEHELPERPRIVATE_H
38 #define QDBUSTYPEHELPERPRIVATE_H
40 #include <QtCore/qlist.h>
41 #include <QtCore/qvariant.h>
42 #include <QtCore/qmetatype.h>
44 // we're going to support all D-Bus primitive types here:
45 // uchar -- not needed: QByteArray
54 // QString -- not needed: QStringList
55 // QList -- not possible: will use QVariant
57 // QDBusStruct -- not yet existant
58 // QMap -- not possible: will use QVariant
60 inline QDBUS_EXPORT int qDBusMetaTypeId(bool *) { return QVariant::Bool; }
61 inline QDBUS_EXPORT int qDBusMetaTypeId(uchar *) { return QMetaType::UChar; }
62 inline QDBUS_EXPORT int qDBusMetaTypeId(short *) { return QMetaType::Short; }
63 inline QDBUS_EXPORT int qDBusMetaTypeId(ushort *) { return QMetaType::UShort; }
64 inline QDBUS_EXPORT int qDBusMetaTypeId(int *) { return QVariant::Int; }
65 inline QDBUS_EXPORT int qDBusMetaTypeId(uint *) { return QVariant::UInt; }
66 inline QDBUS_EXPORT int qDBusMetaTypeId(qlonglong *) { return QVariant::LongLong; }
67 inline QDBUS_EXPORT int qDBusMetaTypeId(qulonglong *) { return QVariant::ULongLong; }
68 inline QDBUS_EXPORT int qDBusMetaTypeId(double *) { return QVariant::Double; }
69 inline QDBUS_EXPORT int qDBusMetaTypeId(QString *) { return QVariant::String; }
70 QDBUS_EXPORT int qDBusMetaTypeId(QVariant *);
71 QDBUS_EXPORT int qDBusMetaTypeId(QList<bool> *);
72 inline QDBUS_EXPORT int qDBusMetaTypeId(QByteArray *) { return QVariant::ByteArray; }
73 QDBUS_EXPORT int qDBusMetaTypeId(QList<short> *);
74 QDBUS_EXPORT int qDBusMetaTypeId(QList<ushort> *);
75 QDBUS_EXPORT int qDBusMetaTypeId(QList<int> *);
76 QDBUS_EXPORT int qDBusMetaTypeId(QList<uint> *);
77 QDBUS_EXPORT int qDBusMetaTypeId(QList<qlonglong> *);
78 QDBUS_EXPORT int qDBusMetaTypeId(QList<qulonglong> *);
79 QDBUS_EXPORT int qDBusMetaTypeId(QList<double> *);
80 inline QDBUS_EXPORT int qDBusMetaTypeId(QStringList *) { return QVariant::StringList; }
81 inline QDBUS_EXPORT int qDBusMetaTypeId(QVariantList *) { return QVariant::List; }
82 inline QDBUS_EXPORT int qDBusMetaTypeId(QVariantMap *) { return QVariant::Map; }
84 // implement the copy mechanism
86 struct QDBusTypeHelper
89 typedef QList<T> List;
91 static inline int id()
94 return qDBusMetaTypeId(t);
97 static inline int listId()
100 return qDBusMetaTypeId(l);
103 static inline QVariant toVariant(const Type &t)
105 return QVariant(id(), &t);
108 static bool canSpecialConvert(const QVariant &);
109 static Type specialConvert(const QVariant &);
111 static inline Type fromVariant(const QVariant &v)
113 if (canSpecialConvert(v))
114 return specialConvert(v);
117 if (copy.convert( QVariant::Type(id()) ))
118 return *reinterpret_cast<const Type *>(copy.constData());
122 static inline QVariantList toVariantList(const List list)
125 Q_FOREACH (const Type &t, list)
126 tmp.append(toVariant(t));
130 static inline QVariantList toVariantList(const QVariant &v)
132 return toVariantList(QDBusTypeHelper<List>::fromVariant(v));
135 static inline List fromVariantList(const QVariantList list)
138 Q_FOREACH (const QVariant &v, list)
139 tmp.append(fromVariant(v));
145 struct QDBusTypeHelper<QVariant>
147 static inline int id()
150 return qDBusMetaTypeId(t);
153 static inline int listId()
155 return QVariant::List;
158 static inline QVariant toVariant(const QVariant &t)
160 return QVariant(id(), &t);
163 static inline QVariant fromVariant(const QVariant &v)
165 if (v.userType() == id())
166 return *reinterpret_cast<const QVariant *>(v.constData());
170 static inline QVariantList toVariantList(const QVariantList &list)
175 static inline QVariantList fromVariantList(const QVariantList &list)
181 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_NO_CAST_TO_ASCII)
183 struct QDBusTypeHelper<char *>
185 static inline int id()
186 { return QVariant::String; }
188 static inline QVariant toVariant(const char *t)
189 { return QVariant(t); }
191 static inline QByteArray fromVariant(const QVariant &v)
192 { return v.toString().toAscii(); }
196 struct QDBusTypeHelper<const char *>
198 static inline int id()
199 { return QVariant::String; }
201 static inline QVariant toVariant(const char *t)
202 { return QVariant(t); }
204 static inline QByteArray fromVariant(const QVariant &v)
205 { return v.toString().toAscii(); }
209 // support three exceptions: uchar, short and ushort
210 // we have to do this as long as QVariant can't convert to/from the integer metatypes
211 template<> inline bool QDBusTypeHelper<short>::canSpecialConvert(const QVariant &v)
212 { return v.userType() < int(QVariant::UserType); }
213 template<> inline short QDBusTypeHelper<short>::specialConvert(const QVariant &v)
214 { return v.toInt(); }
216 template<> inline bool QDBusTypeHelper<ushort>::canSpecialConvert(const QVariant &v)
217 { return v.userType() < int(QVariant::UserType); }
218 template<> inline ushort QDBusTypeHelper<ushort>::specialConvert(const QVariant &v)
219 { return v.toUInt(); }
221 template<> inline bool QDBusTypeHelper<uchar>::canSpecialConvert(const QVariant &v)
222 { return v.userType() < int(QVariant::UserType); }
223 template<> inline uchar QDBusTypeHelper<uchar>::specialConvert(const QVariant &v)
224 { return v.toUInt(); }
226 template<typename T> inline bool QDBusTypeHelper<T>::canSpecialConvert(const QVariant &)
228 template<typename T> inline T QDBusTypeHelper<T>::specialConvert(const QVariant &)