Debian Codegraph @

src/serviceframework/qservicereply.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qservicereply.h"
41 #include "qservicereply_p.h"
42
43 #include <QThread>
44
45 #define Q_SERVICE_REPLY_DEBUG 1
46
47 #ifdef Q_SERVICE_REPLY_DEBUG
48 #include <QDebug>
49 #endif
50
51 /*!
52 \class QServiceReplyBase
53 \ingroup servicefw
54 \inmodule QtServiceFramework
55 \brief The QServiceReplyBase class tracks non-blocking service framework calls.
56
57 The QServiceReplyBase class is a data-carrying class. Each instance is short-lived
58 and only exists during the lifetime of a QServiceManager call. The QServiceReplyBase
59 instance never owns any of the data it points to, it just serves to carry the payload
60 from the background request back to the caller.
61
62 When an instance is first created, after being returned from QServiceManager::loadInterface(),
63 that instance will return false from both the isRunning() and isFinished() functions.
64 Then the request is started, and it will emit the started() signal. After that, and
65 until the request is completed, the isRunning() function will return true.
66 Finally the request is completed, and it will emit the finished() signal. After that
67 the isRunning() function will return false, and the isFinished() function will return
68 true. At this point client code can access the proxyObject() function to obtain the
69 payload of the service request.
70
71 Typically there should be no reason to construct a QServiceReplyBase (or sub-class)
72 instance: instead simply use the instances returned from the QServiceManager::loadInterface()
73 function.
74
75 The service QObject returned from the proxyObject() function is owned by the caller of
76 the original QServiceManager::loadInterface() function which resulted in the
77 QServiceReplyBase instance. Likewise the QServiceObjectBase instance itself is owned
78 by the caller and after the payload is retrieved, it should be queued for destruction
79 with deleteLater() in the function which handles the finished() signal.
80
81 As a convenience the manager() function will return the QServiceManager associated with
82 the request, and the request() function will return a QString indicating the details of
83 the request itself.
84
85 For performance reasons the QServiceReplyBase object is \bold{not synchronized}, and
86 thread-safety is ensured by observing the following invariant condition:
87 \list
88 \o all calls to non-const methods are serialised
89 \endlist
90 In general client code should never have to worry about this, since the private slots
91 which can modify the reply are called via queued signal-slot connections behind the scenes
92 ensuring that such accesses are serialised.
93
94 In the case of a request based on an interface name, request() will return the interface name;
95 otherwise in the case of a request based on a descriptor it will return the interface name
96 of the descriptor.
97
98 \sa QServiceReplyTyped, QServiceManager
99 */
100
101 /*!
102 \class QServiceReplyTyped
103 \ingroup servicefw
104 \inmodule QtServiceFramework
105 \brief The QServiceReplyTyped class tracks typed non-blocking service framework calls.
106
107 This templated sub-class of QServiceReplyBase returns QObjects that are
108 already conveniently cast to the templated type. In all other respects instances of this
109 class function exactly the same as QServiceReplyBase.
110
111 To obtain a typed payload class, rather than just a QObject instance, you can use
112 one of QServiceManager's typed request functions and a typed QServiceReplyTyped
113 instantiation will be returned.
114
115 To obtain the untyped version of the QObject service, call the baseObject() function as
116 for the QServiceReplyBase class.
117
118 \sa QServiceReplyBase, QServiceManager
119 */
120
121 /*!
122 Constructs a new QServiceReplyBase object. All values are set to defaults. Generally
123 creating QServiceReplayBase instances should be left to the QServiceManager.
124 */
125 QServiceReplyBase::QServiceReplyBase(QObject *parent)
126 : QObject(parent),
127 d(new QServiceReplyPrivate)
128 {
129 // nothing to do here
130 }
131
132 /*!
133 Destroys this object recovering all resources.
134 */
135 QServiceReplyBase::~QServiceReplyBase()
136 {
137 delete d;
138 }
139
140 /*!
141 Convenience function that returns an informative string of the request which was
142 issued when this reply was created. This string is not used by the request processor
143 in any way and exists mainly for logging and debugging purposes.
144 */
145 QString QServiceReplyBase::request() const
146 {
147 return d->request;
148 }
149
150 /*!
151 Sets the informative \a request string for this reply. This function is called by the
152 QServiceManager object when the request is created. In general client code
153 should not need to call this function.
154 */
155 void QServiceReplyBase::setRequest(const QString &request)
156 {
157 Q_ASSERT_X(thread() == QThread::currentThread(), Q_FUNC_INFO, "Reply object access violation!");
158 d->request = request;
159 }
160
161 /*!
162 Returns true if the QServiceReplyBase isNoError completed. When this is true, the
163 baseObject() and proxyObject() functions may be called to retrieve the payload
164 of the reply. Note that you should check the value of the error() function
165 to see if the request completed successfully.
166
167 \sa isRunning(), error()
168 */
169 bool QServiceReplyBase::isFinished() const
170 {
171 return d->finished;
172 }
173
174 /*!
175 Returns true if the QServiceReplyBase is being processed. When this is true,
176 the baseObject() and proxyObject() should not be accessed as they are in an
177 undefined state. Instead wait for the finished() signal to be emitted and
178 access those value then.
179
180 \sa isFinished()
181 */
182 bool QServiceReplyBase::isRunning() const
183 {
184 return d->running;
185 }
186
187 /*!
188 Returns any error state that may have been set on this reply.
189
190 \sa isFinished()
191 */
192 QServiceManager::Error QServiceReplyBase::error() const
193 {
194 return d->error;
195 }
196
197 /*!
198 \internal
199 Sets the error condition of the reply to \a error, indicating that processing of
200 the associated request has encountered a problem.
201
202 Note that this is a private slot, and should be called by a queued connection, so
203 that any data modification is only done in the objects own thread.
204 */
205 void QServiceReplyBase::setError(QServiceManager::Error error)
206 {
207 Q_ASSERT_X(thread() == QThread::currentThread(), Q_FUNC_INFO, "Reply object access violation!");
208 if (d->error != error) {
209 d->error = error;
210 emit errorChanged();
211 }
212 }
213
214 /*!
215 \internal
216 Starts the reply, indicating that processing of the associated request has begun.
217
218 Note that this is a private slot, and should be called by a queued connection, so
219 that any data modification is only done in the objects own thread.
220 */
221 void QServiceReplyBase::start()
222 {
223 Q_ASSERT_X(thread() == QThread::currentThread(), Q_FUNC_INFO, "Reply object access violation!");
224 if (!d->running) {
225 d->running = true;
226 emit started();
227 }
228 #ifdef Q_SERVICE_REPLY_DEBUG
229 else
230 {
231 qWarning() << "Starting request that is" << ((d->finished) ? "finished:" : "started:") << d->request;
232 }
233 Q_ASSERT(!d->finished);
234 #endif
235 }
236
237 /*!
238 \internal
239 Finishes the reply, indicating that processing of the associated request has completed.
240
241 Note that this is a private slot, and should be called by a queued connection, so
242 that any data modification is only done in the objects own thread.
243 */
244 void QServiceReplyBase::finish()
245 {
246 Q_ASSERT_X(thread() == QThread::currentThread(), Q_FUNC_INFO, "Reply object access violation!");
247 if (!d->finished) {
248 d->running = false;
249 d->finished = true;
250 emit finished();
251 }
252 #ifdef Q_SERVICE_REPLY_DEBUG
253 else
254 {
255 qWarning() << "Attempt to finish request that has already finished:" << d->request;
256 }
257 Q_ASSERT(!d->running);
258 #endif
259 }