summaryrefslogtreecommitdiffstats
path: root/qt/qdbustypehelper_p.h
blob: 72d5bbe1797b92365042f9b0acec579fb7815da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* qdbuslisthelper_p.h Helper class to convert to and from QVariantList
 *
 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
 * Copyright (C) 2006 Trolltech AS. All rights reserved.
 *    Author: Thiago Macieira <thiago.macieira@trolltech.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

//
//  W A R N I N G
//  -------------
//
// This file is not part of the public API.  This header file may
// change from version to version without notice, or even be
// removed.
//
// We mean it.
//
//

#ifndef QDBUSTYPEHELPERPRIVATE_H
#define QDBUSTYPEHELPERPRIVATE_H

#include <QtCore/qlist.h>
#include <QtCore/qvariant.h>
#include <QtCore/qmetatype.h>

// we're going to support all D-Bus primitive types here:
// uchar -- not needed: QByteArray
// bool
// short
// ushort
// int
// uint
// qlonglong
// qulonglong
// double
// QString -- not needed: QStringList
// QList -- not possible: will use QVariant
// QVariant
// QDBusStruct -- not yet existant
// QMap -- not possible: will use QVariant

inline QDBUS_EXPORT int qDBusMetaTypeId(bool *) { return QVariant::Bool; }
inline QDBUS_EXPORT int qDBusMetaTypeId(uchar *) { return QMetaType::UChar; }
inline QDBUS_EXPORT int qDBusMetaTypeId(short *) { return QMetaType::Short; }
inline QDBUS_EXPORT int qDBusMetaTypeId(ushort *) { return QMetaType::UShort; }
inline QDBUS_EXPORT int qDBusMetaTypeId(int *) { return QVariant::Int; }
inline QDBUS_EXPORT int qDBusMetaTypeId(uint *) { return QVariant::UInt; }
inline QDBUS_EXPORT int qDBusMetaTypeId(qlonglong *) { return QVariant::LongLong; }
inline QDBUS_EXPORT int qDBusMetaTypeId(qulonglong *) { return QVariant::ULongLong; }
inline QDBUS_EXPORT int qDBusMetaTypeId(double *) { return QVariant::Double; }
inline QDBUS_EXPORT int qDBusMetaTypeId(QString *) { return QVariant::String; }
QDBUS_EXPORT int qDBusMetaTypeId(QVariant *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<bool> *);
inline QDBUS_EXPORT int qDBusMetaTypeId(QByteArray *) { return QVariant::ByteArray; }
QDBUS_EXPORT int qDBusMetaTypeId(QList<short> *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<ushort> *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<int> *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<uint> *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<qlonglong> *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<qulonglong> *);
QDBUS_EXPORT int qDBusMetaTypeId(QList<double> *);
inline QDBUS_EXPORT int qDBusMetaTypeId(QStringList *) { return QVariant::StringList; }
inline QDBUS_EXPORT int qDBusMetaTypeId(QVariantList *) { return QVariant::List; }
inline QDBUS_EXPORT int qDBusMetaTypeId(QVariantMap *) { return QVariant::Map; }

// implement the copy mechanism
template<class T>
struct QDBusTypeHelper
{
    typedef T Type;
    typedef QList<T> List;

    static inline int id()
    {
        Type* t = 0;
        return qDBusMetaTypeId(t);
    }

    static inline int listId()
    {
        List *l = 0;
        return qDBusMetaTypeId(l);
    }
    
    static inline QVariant toVariant(const Type &t)
    {
        return QVariant(id(), &t);
    }

    static bool canSpecialConvert(const QVariant &);
    static Type specialConvert(const QVariant &);

    static inline Type fromVariant(const QVariant &v)
    {
        if (canSpecialConvert(v))
            return specialConvert(v);

        QVariant copy(v);
        if (copy.convert( QVariant::Type(id()) ))
            return *reinterpret_cast<const Type *>(copy.constData());
        return Type();
    }

    static inline QVariantList toVariantList(const List &list)
    {
        QVariantList tmp;
        Q_FOREACH (const Type &t, list)
            tmp.append(toVariant(t));
        return tmp;
    }

    static inline QVariantList toVariantList(const QVariant &v)
    {
        return toVariantList(QDBusTypeHelper<List>::fromVariant(v));
    }

    static inline List fromVariantList(const QVariantList &list)
    {
        List tmp;
        Q_FOREACH (const QVariant &v, list)
            tmp.append(fromVariant(v));
        return tmp;
    }
};

template<>
struct QDBusTypeHelper<QVariant>
{
    static inline int id()
    {
        QVariant *t = 0;
        return qDBusMetaTypeId(t);
    }

    static inline int listId()
    {
        return QVariant::List;
    }

    static inline QVariant toVariant(const QVariant &t)
    {
        return QVariant(id(), &t);
    }

    static inline QVariant fromVariant(const QVariant &v)
    {
        if (v.userType() == id())
            return *reinterpret_cast<const QVariant *>(v.constData());
        return v;
    }

    static inline QVariantList toVariantList(const QVariantList &list)
    {
        return list;
    }

    static inline QVariantList fromVariantList(const QVariantList &list)
    {
        return list;
    }
};

#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_NO_CAST_TO_ASCII)
template<>
struct QDBusTypeHelper<char *>
{
    static inline int id()
    { return QVariant::String; }

    static inline QVariant toVariant(const char *t)
    { return QVariant(t); }

    static inline QByteArray fromVariant(const QVariant &v)
    { return v.toString().toAscii(); }
};

template<>
struct QDBusTypeHelper<const char *>
{
    static inline int id()
    { return QVariant::String; }

    static inline QVariant toVariant(const char *t)
    { return QVariant(t); }

    static inline QByteArray fromVariant(const QVariant &v)
    { return v.toString().toAscii(); }
};
#endif

// support three exceptions: uchar, short and ushort
// we have to do this as long as QVariant can't convert to/from the integer metatypes
template<> inline bool QDBusTypeHelper<short>::canSpecialConvert(const QVariant &v)
{ return v.userType() < int(QVariant::UserType); }
template<> inline short QDBusTypeHelper<short>::specialConvert(const QVariant &v)
{ return v.toInt(); }

template<> inline bool QDBusTypeHelper<ushort>::canSpecialConvert(const QVariant &v)
{ return v.userType() < int(QVariant::UserType); }
template<> inline ushort QDBusTypeHelper<ushort>::specialConvert(const QVariant &v)
{ return v.toUInt(); }

template<> inline bool QDBusTypeHelper<uchar>::canSpecialConvert(const QVariant &v)
{ return v.userType() < int(QVariant::UserType); }
template<> inline uchar QDBusTypeHelper<uchar>::specialConvert(const QVariant &v)
{ return v.toUInt(); }

template<typename T> inline bool QDBusTypeHelper<T>::canSpecialConvert(const QVariant &)
{ return false; }
template<typename T> inline T QDBusTypeHelper<T>::specialConvert(const QVariant &)
{ return T(); }

#endif