1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com> |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
4 | |
5 | #include "qgrpccallreply.h" |
6 | #include <QtCore/QThread> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | using namespace Qt::StringLiterals; |
11 | |
12 | /*! |
13 | \class QGrpcCallReply |
14 | \inmodule QtGrpc |
15 | |
16 | \brief The QGrpcCallReply class contains data for asynchronous call |
17 | of gRPC client API. |
18 | |
19 | The QGrpcCallReply object is owned by the client object that created it. |
20 | QGrpcCallReply can be used by QAbstractGrpcChannel implementations |
21 | to control call workflow and abort calls if possible in the event |
22 | of QGrpcCallReply::abort being called by a library user. |
23 | */ |
24 | |
25 | /*! |
26 | \fn template <typename Func1, typename Func2> void QGrpcCallReply::subscribe(QObject *receiver, |
27 | Func1 &&finishCallback, Func2 &&errorCallback, Qt::ConnectionType type = Qt::AutoConnection); |
28 | |
29 | Convenience function to connect the \a finishCallback and |
30 | \a errorCallback of \a receiver to the QGrpcCallReply::finished and |
31 | the QGrpcCallReply::errorOccurred signals with the given connection \a type. |
32 | |
33 | Calling this function is equivalent to the following: |
34 | \code |
35 | QObject::connect(this, &QGrpcCallReply::finished, receiver, |
36 | std::forward<Func1>(finishCallback), type); |
37 | QObject::connect(this, &QGrpcCallReply::errorOccurred, receiver, |
38 | std::forward<Func2>(errorCallback), type); |
39 | \endcode |
40 | */ |
41 | |
42 | /*! |
43 | \fn template <typename Func1> void QGrpcCallReply::subscribe(QObject *receiver, |
44 | Func1 &&finishCallback, Qt::ConnectionType type = Qt::AutoConnection); |
45 | |
46 | Convenience function to connect the \a finishCallback of \a receiver to |
47 | the QGrpcCallReply::finished signal with given connection \a type. |
48 | |
49 | Calling this function is equivalent to the following: |
50 | \code |
51 | QObject::connect(this, &QGrpcCallReply::finished, receiver, |
52 | std::forward<Func1>(finishCallback), type); |
53 | \endcode |
54 | */ |
55 | |
56 | QGrpcCallReply::QGrpcCallReply(std::shared_ptr<QAbstractProtobufSerializer> serializer) |
57 | : QGrpcOperation(std::move(serializer)) |
58 | { |
59 | } |
60 | |
61 | QGrpcCallReply::~QGrpcCallReply() = default; |
62 | |
63 | /*! |
64 | Aborts this reply and try to abort call in channel. |
65 | */ |
66 | void QGrpcCallReply::abort() |
67 | { |
68 | auto abortFunc = [&] { |
69 | setData({}); |
70 | emit errorOccurred( |
71 | status: { QGrpcStatus::StatusCode::Aborted, "Call aborted by user or timeout"_L1 }); |
72 | }; |
73 | if (thread() != QThread::currentThread()) |
74 | QMetaObject::invokeMethod(object: this, function&: abortFunc, type: Qt::BlockingQueuedConnection); |
75 | else |
76 | abortFunc(); |
77 | } |
78 | |
79 | QT_END_NAMESPACE |
80 | |
81 | #include "moc_qgrpccallreply.cpp" |
82 | |