2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
2016-01-21 02:33:50 +00:00
|
|
|
** Copyright (C) 2016 Intel Corporation.
|
2016-01-15 07:08:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtDBus module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qdbuspendingreply.h"
|
|
|
|
#include "qdbuspendingcall_p.h"
|
|
|
|
#include "qdbusmetatype.h"
|
|
|
|
|
2019-08-22 08:17:12 +00:00
|
|
|
#include <QtCore/private/qlocking_p.h>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#ifndef QT_NO_DBUS
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QDBusPendingReply
|
|
|
|
\inmodule QtDBus
|
|
|
|
\since 4.5
|
|
|
|
|
2018-06-18 11:11:18 +00:00
|
|
|
\brief The QDBusPendingReply class contains the reply to an asynchronous method call.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The QDBusPendingReply is a template class with up to 8 template
|
|
|
|
parameters. Those parameters are the types that will be used to
|
|
|
|
extract the contents of the reply's data.
|
|
|
|
|
|
|
|
This class is similar in functionality to QDBusReply, but with two
|
|
|
|
important differences:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li QDBusReply accepts exactly one return type, whereas
|
2011-04-27 10:05:43 +00:00
|
|
|
QDBusPendingReply can have from 1 to 8 types
|
2012-03-01 14:28:31 +00:00
|
|
|
\li QDBusReply only works on already completed replies, whereas
|
2011-04-27 10:05:43 +00:00
|
|
|
QDBusPendingReply allows one to wait for replies from pending
|
|
|
|
calls
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
Where with QDBusReply you would write:
|
|
|
|
|
2012-05-07 12:03:02 +00:00
|
|
|
\snippet code/src_qdbus_qdbusreply.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
with QDBusPendingReply, the equivalent code (including the blocking
|
|
|
|
wait for the reply) would be:
|
|
|
|
|
2012-05-07 12:03:02 +00:00
|
|
|
\snippet code/src_qdbus_qdbuspendingreply.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For method calls that have more than one output argument, with
|
|
|
|
QDBusReply, you would write:
|
|
|
|
|
2012-05-07 12:03:02 +00:00
|
|
|
\snippet code/src_qdbus_qdbusreply.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
whereas with QDBusPendingReply, all of the output arguments should
|
|
|
|
be template parameters:
|
|
|
|
|
2012-05-07 12:03:02 +00:00
|
|
|
\snippet code/src_qdbus_qdbuspendingreply.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QDBusPendingReply objects can be associated with
|
|
|
|
QDBusPendingCallWatcher objects, which emit signals when the reply
|
|
|
|
arrives.
|
|
|
|
|
2020-01-15 13:38:14 +00:00
|
|
|
\sa QDBusPendingCallWatcher, QDBusReply
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply<Types...>::QDBusPendingReply()
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Creates an empty QDBusPendingReply object. Without assigning a
|
|
|
|
QDBusPendingCall object to this reply, QDBusPendingReply cannot do
|
|
|
|
anything. All functions return their failure values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply<Types...>::QDBusPendingReply(const QDBusPendingReply &other)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Creates a copy of the \a other QDBusPendingReply object. Just like
|
|
|
|
QDBusPendingCall and QDBusPendingCallWatcher, this QDBusPendingReply
|
|
|
|
object will share the same pending call reference. All copies
|
|
|
|
share the same return values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply<Types...>::QDBusPendingReply(const QDBusPendingCall &call)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Creates a QDBusPendingReply object that will take its contents from
|
|
|
|
the \a call pending asynchronous call. This QDBusPendingReply object
|
|
|
|
will share the same pending call reference as \a call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply<Types...>::QDBusPendingReply(const QDBusMessage &message)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Creates a QDBusPendingReply object that will take its contents from
|
|
|
|
the message \a message. In this case, this object will be already
|
|
|
|
in its finished state and the reply's contents will be accessible.
|
|
|
|
|
|
|
|
\sa isFinished()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply &QDBusPendingReply<Types...>::operator=(const QDBusPendingReply &other)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Makes a copy of \a other and drops the reference to the current
|
|
|
|
pending call. If the current reference is to an unfinished pending
|
|
|
|
call and this is the last reference, the pending call will be
|
|
|
|
canceled and there will be no way of retrieving the reply's
|
|
|
|
contents, when they arrive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply &QDBusPendingReply<Types...>::operator=(const QDBusPendingCall &call)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Makes this object take its contents from the \a call pending call
|
|
|
|
and drops the reference to the current pending call. If the
|
|
|
|
current reference is to an unfinished pending call and this is the
|
|
|
|
last reference, the pending call will be canceled and there will
|
|
|
|
be no way of retrieving the reply's contents, when they arrive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply &QDBusPendingReply<Types...>::operator=(const QDBusMessage &message)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Makes this object take its contents from the \a message message
|
|
|
|
and drops the reference to the current pending call. If the
|
|
|
|
current reference is to an unfinished pending call and this is the
|
|
|
|
last reference, the pending call will be canceled and there will
|
|
|
|
be no way of retrieving the reply's contents, when they arrive.
|
|
|
|
|
|
|
|
After this function is finished, the QDBusPendingReply object will
|
|
|
|
be in its "finished" state and the \a message contents will be
|
|
|
|
accessible.
|
|
|
|
|
|
|
|
\sa isFinished()
|
|
|
|
*/
|
|
|
|
|
2017-03-16 09:29:51 +00:00
|
|
|
/*!
|
|
|
|
\enum QDBusPendingReply::anonymous
|
|
|
|
|
|
|
|
\value Count The number of arguments the reply is expected to have
|
|
|
|
*/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> int QDBusPendingReply<Types...>::count() const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Return the number of arguments the reply is supposed to have. This
|
|
|
|
number matches the number of non-void template parameters in this
|
|
|
|
class.
|
|
|
|
|
|
|
|
If the reply arrives with a different number of arguments (or with
|
|
|
|
different types), it will be transformed into an error reply
|
|
|
|
indicating a bad signature.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QVariant QDBusPendingReply<Types...>::argumentAt(int index) const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the argument at position \a index in the reply's
|
|
|
|
contents. If the reply doesn't have that many elements, this
|
|
|
|
function's return value is undefined (will probably cause an
|
|
|
|
assertion failure), so it is important to verify that the
|
|
|
|
processing is finished and the reply is valid.
|
2013-01-11 19:25:28 +00:00
|
|
|
|
|
|
|
If the reply does not contain an argument at position \a index or if the
|
|
|
|
reply was an error, this function returns an invalid QVariant. Since D-Bus
|
|
|
|
messages can never contain invalid QVariants, this return can be used to
|
|
|
|
detect an error condition.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> T1 QDBusPendingReply<Types...>::value() const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the first argument in this reply, cast to type \c T1 (the
|
|
|
|
first template parameter of this class). This is equivalent to
|
|
|
|
calling argumentAt<0>().
|
|
|
|
|
|
|
|
This function is provided as a convenience, matching the
|
|
|
|
QDBusReply::value() function.
|
|
|
|
|
|
|
|
Note that, if the reply hasn't arrived, this function causes the
|
|
|
|
calling thread to block until the reply is processed.
|
2013-01-11 19:25:28 +00:00
|
|
|
|
|
|
|
If the reply is an error reply, this function returns a default-constructed
|
|
|
|
\c T1 object, which may be indistinguishable from a valid value. To
|
|
|
|
reliably determine whether the message was an error, use isError().
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> QDBusPendingReply<Types...>::operator T1() const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the first argument in this reply, cast to type \c T1 (the
|
|
|
|
first template parameter of this class). This is equivalent to
|
|
|
|
calling argumentAt<0>().
|
|
|
|
|
|
|
|
This function is provided as a convenience, matching the
|
|
|
|
QDBusReply::value() function.
|
|
|
|
|
|
|
|
Note that, if the reply hasn't arrived, this function causes the
|
|
|
|
calling thread to block until the reply is processed.
|
2013-01-11 19:25:28 +00:00
|
|
|
|
|
|
|
If the reply is an error reply, this function returns a default-constructed
|
|
|
|
\c T1 object, which may be indistinguishable from a valid value. To
|
|
|
|
reliably determine whether the message was an error, use isError().
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2020-09-08 10:42:52 +00:00
|
|
|
\fn template<typename... Types> void QDBusPendingReply<Types...>::waitForFinished()
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Suspends the execution of the calling thread until the reply is
|
|
|
|
received and processed. After this function returns, isFinished()
|
|
|
|
should return true, indicating the reply's contents are ready to
|
|
|
|
be processed.
|
|
|
|
|
|
|
|
\sa QDBusPendingCallWatcher::waitForFinished()
|
|
|
|
*/
|
|
|
|
|
2020-09-08 10:42:52 +00:00
|
|
|
QDBusPendingReplyBase::QDBusPendingReplyBase()
|
2019-11-22 13:46:58 +00:00
|
|
|
: QDBusPendingCall(nullptr) // initialize base class empty
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:42:52 +00:00
|
|
|
QDBusPendingReplyBase::~QDBusPendingReplyBase()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:42:52 +00:00
|
|
|
void QDBusPendingReplyBase::assign(const QDBusPendingCall &other)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QDBusPendingCall::operator=(other);
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:42:52 +00:00
|
|
|
void QDBusPendingReplyBase::assign(const QDBusMessage &message)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2019-11-22 13:46:58 +00:00
|
|
|
d = new QDBusPendingCallPrivate(QDBusMessage(), nullptr); // drops the reference to the old one
|
2011-04-27 10:05:43 +00:00
|
|
|
d->replyMessage = message;
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:42:52 +00:00
|
|
|
QVariant QDBusPendingReplyBase::argumentAt(int index) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-01-11 19:25:28 +00:00
|
|
|
if (!d)
|
|
|
|
return QVariant();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-01-11 19:25:28 +00:00
|
|
|
d->waitForFinished(); // bypasses "const"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-01-11 19:25:28 +00:00
|
|
|
return d->replyMessage.arguments().value(index);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:42:52 +00:00
|
|
|
void QDBusPendingReplyBase::setMetaTypes(int count, const int *types)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(d);
|
2019-08-22 08:17:12 +00:00
|
|
|
const auto locker = qt_scoped_lock(d->mutex);
|
2011-04-27 10:05:43 +00:00
|
|
|
d->setMetaTypes(count, types);
|
|
|
|
d->checkReceivedSignature();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // QT_NO_DBUS
|