7ebd7f3e48ffd4ff3ba04aa03d68a23e4107e02f
[platform/upstream/dbus.git] / qt / qdbustypehelper_p.h
1 /* qdbuslisthelper_p.h Helper class to convert to and from QVariantList
2  *
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>
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
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.
13  *
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.
18  *
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.
22  *
23  */
24
25 //
26 //  W A R N I N G
27 //  -------------
28 //
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
31 // removed.
32 //
33 // We mean it.
34 //
35 //
36
37 #ifndef QDBUSTYPEHELPERPRIVATE_H
38 #define QDBUSTYPEHELPERPRIVATE_H
39
40 #include <QtCore/qlist.h>
41 #include <QtCore/qvariant.h>
42 #include <QtCore/qmetatype.h>
43
44 // we're going to support all D-Bus primitive types here:
45 // uchar -- not needed: QByteArray
46 // bool
47 // short
48 // ushort
49 // int
50 // uint
51 // qlonglong
52 // qulonglong
53 // double
54 // QString -- not needed: QStringList
55 // QList -- not possible: will use QVariant
56 // QVariant
57 // QDBusStruct -- not yet existant
58 // QMap -- not possible: will use QVariant
59
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; }
83
84 // implement the copy mechanism
85 template<class T>
86 struct QDBusTypeHelper
87 {
88     typedef T Type;
89     typedef QList<T> List;
90
91     static inline int id()
92     {
93         Type* t = 0;
94         return qDBusMetaTypeId(t);
95     }
96
97     static inline int listId()
98     {
99         List *l = 0;
100         return qDBusMetaTypeId(l);
101     }
102     
103     static inline QVariant toVariant(const Type &t)
104     {
105         return QVariant(id(), &t);
106     }
107
108     static bool canSpecialConvert(const QVariant &);
109     static Type specialConvert(const QVariant &);
110
111     static inline Type fromVariant(const QVariant &v)
112     {
113         if (canSpecialConvert(v))
114             return specialConvert(v);
115
116         QVariant copy(v);
117         if (copy.convert( QVariant::Type(id()) ))
118             return *reinterpret_cast<const Type *>(copy.constData());
119         return Type();
120     }
121
122     static inline QVariantList toVariantList(const List list)
123     {
124         QVariantList tmp;
125         Q_FOREACH (const Type &t, list)
126             tmp.append(toVariant(t));
127         return tmp;
128     }
129
130     static inline QVariantList toVariantList(const QVariant &v)
131     {
132         return toVariantList(QDBusTypeHelper<List>::fromVariant(v));
133     }
134
135     static inline List fromVariantList(const QVariantList list)
136     {
137         List tmp;
138         Q_FOREACH (const QVariant &v, list)
139             tmp.append(fromVariant(v));
140         return tmp;
141     }
142 };
143
144 template<>
145 struct QDBusTypeHelper<QVariant>
146 {
147     static inline int id()
148     {
149         QVariant *t = 0;
150         return qDBusMetaTypeId(t);
151     }
152
153     static inline int listId()
154     {
155         return QVariant::List;
156     }
157
158     static inline QVariant toVariant(const QVariant &t)
159     {
160         return QVariant(id(), &t);
161     }
162
163     static inline QVariant fromVariant(const QVariant &v)
164     {
165         if (v.userType() == id())
166             return *reinterpret_cast<const QVariant *>(v.constData());
167         return v;
168     }
169
170     static inline QVariantList toVariantList(const QVariantList &list)
171     {
172         return list;
173     }
174
175     static inline QVariantList fromVariantList(const QVariantList &list)
176     {
177         return list;
178     }
179 };
180
181 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_NO_CAST_TO_ASCII)
182 template<>
183 struct QDBusTypeHelper<char *>
184 {
185     static inline int id()
186     { return QVariant::String; }
187
188     static inline QVariant toVariant(const char *t)
189     { return QVariant(t); }
190
191     static inline QByteArray fromVariant(const QVariant &v)
192     { return v.toString().toAscii(); }
193 };
194
195 template<>
196 struct QDBusTypeHelper<const char *>
197 {
198     static inline int id()
199     { return QVariant::String; }
200
201     static inline QVariant toVariant(const char *t)
202     { return QVariant(t); }
203
204     static inline QByteArray fromVariant(const QVariant &v)
205     { return v.toString().toAscii(); }
206 };
207 #endif
208
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(); }
215
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(); }
220
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(); }
225
226 template<typename T> inline bool QDBusTypeHelper<T>::canSpecialConvert(const QVariant &)
227 { return false; }
228 template<typename T> inline T QDBusTypeHelper<T>::specialConvert(const QVariant &)
229 { return T(); }
230
231 #endif