Merge remote-tracking branch 'origin/api_changes'
[profile/ivi/qtbase.git] / src / dbus / qdbuspendingreply.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDBus module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDBUSPENDINGREPLY_H
43 #define QDBUSPENDINGREPLY_H
44
45 #include <QtCore/qglobal.h>
46 #include <QtDBus/qdbusmacros.h>
47 #include <QtDBus/qdbusargument.h>
48 #include <QtDBus/qdbuspendingcall.h>
49
50 #ifndef QT_NO_DBUS
51
52 QT_BEGIN_HEADER
53
54 QT_BEGIN_NAMESPACE
55
56
57 class Q_DBUS_EXPORT QDBusPendingReplyData: public QDBusPendingCall
58 {
59 protected:
60     QDBusPendingReplyData();
61     ~QDBusPendingReplyData();
62     void assign(const QDBusPendingCall &call);
63     void assign(const QDBusMessage &message);
64
65     QVariant argumentAt(int index) const;
66     void setMetaTypes(int count, const int *metaTypes);
67 };
68
69 namespace QDBusPendingReplyTypes {
70     template<int Index,
71              typename T1, typename T2, typename T3, typename T4,
72              typename T5, typename T6, typename T7, typename T8>
73     struct Select
74     {
75         typedef Select<Index - 1, T2, T3, T4, T5, T6, T7, T8, void> Next;
76         typedef typename Next::Type Type;
77     };
78     template<typename T1, typename T2, typename T3, typename T4,
79              typename T5, typename T6, typename T7, typename T8>
80     struct Select<0, T1, T2, T3, T4, T5, T6, T7, T8>
81     {
82         typedef T1 Type;
83     };
84
85     template<typename T1> inline int metaTypeFor(T1 * = 0)
86     { return qMetaTypeId<T1>(); }
87     // specialize for QVariant, allowing it to be used in place of QDBusVariant
88     template<> inline int metaTypeFor<QVariant>(QVariant *)
89     { return qMetaTypeId<QDBusVariant>(); }
90
91     template<typename T1, typename T2, typename T3, typename T4,
92              typename T5, typename T6, typename T7, typename T8>
93     struct ForEach
94     {
95         typedef ForEach<T2, T3, T4, T5, T6, T7, T8, void> Next;
96         enum { Total = Next::Total + 1 };
97         static inline void fillMetaTypes(int *p)
98         {
99             *p = metaTypeFor<T1>(0);
100             Next::fillMetaTypes(++p);
101         }
102     };
103     template<>
104     struct ForEach<void, void, void, void,   void, void, void, void>
105     {
106         enum { Total = 0 };
107         static inline void fillMetaTypes(int *)
108         { }
109     };
110 } // namespace QDBusPendingReplyTypes
111
112 template<typename T1 = void, typename T2 = void, typename T3 = void, typename T4 = void,
113          typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
114 class QDBusPendingReply:
115 #ifdef Q_QDOC
116     public QDBusPendingCall
117 #else
118     public QDBusPendingReplyData
119 #endif
120 {
121     typedef QDBusPendingReplyTypes::ForEach<T1, T2, T3, T4, T5, T6, T7, T8> ForEach;
122     template<int Index> struct Select :
123     QDBusPendingReplyTypes::Select<Index, T1, T2, T3, T4, T5, T6, T7, T8>
124     {
125     };
126
127 public:
128     enum { Count = ForEach::Total };
129
130     inline QDBusPendingReply()
131     { }
132     inline QDBusPendingReply(const QDBusPendingReply &other)
133         : QDBusPendingReplyData(other)
134     { }
135     inline /*implicit*/ QDBusPendingReply(const QDBusPendingCall &call) // required by qdbusxml2cpp-generated code
136     { *this = call; }
137     inline /*implicit*/ QDBusPendingReply(const QDBusMessage &message)
138     { *this = message; }
139     inline QDBusPendingReply &operator=(const QDBusPendingReply &other)
140     { assign(other); return *this; }
141     inline QDBusPendingReply &operator=(const QDBusPendingCall &call)
142     { assign(call); return *this; }
143     inline QDBusPendingReply &operator=(const QDBusMessage &message)
144     { assign(message); return *this; }
145
146     inline int count() const { return Count; }
147
148 #if defined(Q_QDOC) || defined(Q_NO_USING_KEYWORD)
149     inline QVariant argumentAt(int index) const
150     { return QDBusPendingReplyData::argumentAt(index); }
151 #else
152     using QDBusPendingReplyData::argumentAt;
153 #endif
154
155 #if defined(Q_QDOC)
156     bool isFinished() const;
157     void waitForFinished();
158
159     bool isValid() const;
160     bool isError() const;
161     QDBusError error() const;
162     QDBusMessage reply() const;
163
164     template<int Index> inline Type argumentAt() const;
165     inline T1 value() const;
166     inline operator T1() const;
167 #else
168     template<int Index> inline
169     const typename Select<Index>::Type argumentAt() const
170     {
171         // static assert?
172         Q_ASSERT_X(Index < count() && Index >= 0, "QDBusPendingReply::argumentAt",
173                    "Index out of bounds");
174         typedef typename Select<Index>::Type ResultType;
175         return qdbus_cast<ResultType>(argumentAt(Index), 0);
176     }
177
178     inline typename Select<0>::Type value() const
179     {
180         return argumentAt<0>();
181     }
182
183     inline operator typename Select<0>::Type() const
184     {
185         return argumentAt<0>();
186     }
187 #endif
188
189 private:
190     inline void calculateMetaTypes()
191     {
192         if (!d) return;
193         int typeIds[Count > 0 ? Count : 1]; // use at least one since zero-sized arrays aren't valid
194         ForEach::fillMetaTypes(typeIds);
195         setMetaTypes(Count, typeIds);
196     }
197
198     inline void assign(const QDBusPendingCall &call)
199     {
200         QDBusPendingReplyData::assign(call);
201         calculateMetaTypes();
202     }
203
204     inline void assign(const QDBusMessage &message)
205     {
206         QDBusPendingReplyData::assign(message);
207         calculateMetaTypes();
208     }
209 };
210
211 QT_END_NAMESPACE
212
213 QT_END_HEADER
214
215 #endif // QT_NO_DBUS
216 #endif