1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtDBus module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "qdbuspendingreply.h"
42#include "qdbuspendingcall_p.h"
43#include "qdbusmetatype.h"
44
45#include <QtCore/private/qlocking_p.h>
46
47#ifndef QT_NO_DBUS
48
49/*!
50 \class QDBusPendingReply
51 \inmodule QtDBus
52 \since 4.5
53
54 \brief The QDBusPendingReply class contains the reply to an asynchronous method call.
55
56 The QDBusPendingReply is a template class with up to 8 template
57 parameters. Those parameters are the types that will be used to
58 extract the contents of the reply's data.
59
60 This class is similar in functionality to QDBusReply, but with two
61 important differences:
62
63 \list
64 \li QDBusReply accepts exactly one return type, whereas
65 QDBusPendingReply can have from 1 to 8 types
66 \li QDBusReply only works on already completed replies, whereas
67 QDBusPendingReply allows one to wait for replies from pending
68 calls
69 \endlist
70
71 Where with QDBusReply you would write:
72
73 \snippet code/src_qdbus_qdbusreply.cpp 0
74
75 with QDBusPendingReply, the equivalent code (including the blocking
76 wait for the reply) would be:
77
78 \snippet code/src_qdbus_qdbuspendingreply.cpp 0
79
80 For method calls that have more than one output argument, with
81 QDBusReply, you would write:
82
83 \snippet code/src_qdbus_qdbusreply.cpp 1
84
85 whereas with QDBusPendingReply, all of the output arguments should
86 be template parameters:
87
88 \snippet code/src_qdbus_qdbuspendingreply.cpp 2
89
90 QDBusPendingReply objects can be associated with
91 QDBusPendingCallWatcher objects, which emit signals when the reply
92 arrives.
93
94 \sa QDBusPendingCallWatcher, QDBusReply
95*/
96
97/*!
98 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::QDBusPendingReply()
99
100 Creates an empty QDBusPendingReply object. Without assigning a
101 QDBusPendingCall object to this reply, QDBusPendingReply cannot do
102 anything. All functions return their failure values.
103*/
104
105/*!
106 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::QDBusPendingReply(const QDBusPendingReply &other)
107
108 Creates a copy of the \a other QDBusPendingReply object. Just like
109 QDBusPendingCall and QDBusPendingCallWatcher, this QDBusPendingReply
110 object will share the same pending call reference. All copies
111 share the same return values.
112*/
113
114/*!
115 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::QDBusPendingReply(const QDBusPendingCall &call)
116
117 Creates a QDBusPendingReply object that will take its contents from
118 the \a call pending asynchronous call. This QDBusPendingReply object
119 will share the same pending call reference as \a call.
120*/
121
122/*!
123 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::QDBusPendingReply(const QDBusMessage &message)
124
125 Creates a QDBusPendingReply object that will take its contents from
126 the message \a message. In this case, this object will be already
127 in its finished state and the reply's contents will be accessible.
128
129 \sa isFinished()
130*/
131
132/*!
133 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply &QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::operator=(const QDBusPendingReply &other)
134
135 Makes a copy of \a other and drops the reference to the current
136 pending call. If the current reference is to an unfinished pending
137 call and this is the last reference, the pending call will be
138 canceled and there will be no way of retrieving the reply's
139 contents, when they arrive.
140*/
141
142/*!
143 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply &QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::operator=(const QDBusPendingCall &call)
144
145 Makes this object take its contents from the \a call pending call
146 and drops the reference to the current pending call. If the
147 current reference is to an unfinished pending call and this is the
148 last reference, the pending call will be canceled and there will
149 be no way of retrieving the reply's contents, when they arrive.
150*/
151
152/*!
153 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply &QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::operator=(const QDBusMessage &message)
154
155 Makes this object take its contents from the \a message message
156 and drops the reference to the current pending call. If the
157 current reference is to an unfinished pending call and this is the
158 last reference, the pending call will be canceled and there will
159 be no way of retrieving the reply's contents, when they arrive.
160
161 After this function is finished, the QDBusPendingReply object will
162 be in its "finished" state and the \a message contents will be
163 accessible.
164
165 \sa isFinished()
166*/
167
168/*!
169 \enum QDBusPendingReply::anonymous
170
171 \value Count The number of arguments the reply is expected to have
172 */
173
174/*!
175 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> int QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::count() const
176
177 Return the number of arguments the reply is supposed to have. This
178 number matches the number of non-void template parameters in this
179 class.
180
181 If the reply arrives with a different number of arguments (or with
182 different types), it will be transformed into an error reply
183 indicating a bad signature.
184*/
185
186/*!
187 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QVariant QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::argumentAt(int index) const
188
189 Returns the argument at position \a index in the reply's
190 contents. If the reply doesn't have that many elements, this
191 function's return value is undefined (will probably cause an
192 assertion failure), so it is important to verify that the
193 processing is finished and the reply is valid.
194
195 If the reply does not contain an argument at position \a index or if the
196 reply was an error, this function returns an invalid QVariant. Since D-Bus
197 messages can never contain invalid QVariants, this return can be used to
198 detect an error condition.
199*/
200
201/*!
202 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> T1 QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::value() const
203
204 Returns the first argument in this reply, cast to type \c T1 (the
205 first template parameter of this class). This is equivalent to
206 calling argumentAt<0>().
207
208 This function is provided as a convenience, matching the
209 QDBusReply::value() function.
210
211 Note that, if the reply hasn't arrived, this function causes the
212 calling thread to block until the reply is processed.
213
214 If the reply is an error reply, this function returns a default-constructed
215 \c T1 object, which may be indistinguishable from a valid value. To
216 reliably determine whether the message was an error, use isError().
217*/
218
219/*!
220 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::operator T1() const
221
222 Returns the first argument in this reply, cast to type \c T1 (the
223 first template parameter of this class). This is equivalent to
224 calling argumentAt<0>().
225
226 This function is provided as a convenience, matching the
227 QDBusReply::value() function.
228
229 Note that, if the reply hasn't arrived, this function causes the
230 calling thread to block until the reply is processed.
231
232 If the reply is an error reply, this function returns a default-constructed
233 \c T1 object, which may be indistinguishable from a valid value. To
234 reliably determine whether the message was an error, use isError().
235*/
236
237/*!
238 \fn template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> void QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::waitForFinished()
239
240 Suspends the execution of the calling thread until the reply is
241 received and processed. After this function returns, isFinished()
242 should return true, indicating the reply's contents are ready to
243 be processed.
244
245 \sa QDBusPendingCallWatcher::waitForFinished()
246*/
247
248QDBusPendingReplyData::QDBusPendingReplyData()
249 : QDBusPendingCall(nullptr) // initialize base class empty
250{
251}
252
253QDBusPendingReplyData::~QDBusPendingReplyData()
254{
255}
256
257void QDBusPendingReplyData::assign(const QDBusPendingCall &other)
258{
259 QDBusPendingCall::operator=(other);
260}
261
262void QDBusPendingReplyData::assign(const QDBusMessage &message)
263{
264 d = new QDBusPendingCallPrivate(QDBusMessage(), nullptr); // drops the reference to the old one
265 d->replyMessage = message;
266}
267
268QVariant QDBusPendingReplyData::argumentAt(int index) const
269{
270 if (!d)
271 return QVariant();
272
273 d->waitForFinished(); // bypasses "const"
274
275 return d->replyMessage.arguments().value(i: index);
276}
277
278void QDBusPendingReplyData::setMetaTypes(int count, const int *types)
279{
280 Q_ASSERT(d);
281 const auto locker = qt_scoped_lock(mutex&: d->mutex);
282 d->setMetaTypes(count, types);
283 d->checkReceivedSignature();
284}
285
286#endif // QT_NO_DBUS
287

source code of qtbase/src/dbus/qdbuspendingreply.cpp