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 | #ifndef QGRPCOPERATION_H |
6 | #define QGRPCOPERATION_H |
7 | |
8 | #include <QtGrpc/qgrpcstatus.h> |
9 | #include <QtGrpc/qtgrpcglobal.h> |
10 | |
11 | #include <QtProtobuf/qtprotobuftypes.h> |
12 | |
13 | #include <QtCore/qhash.h> |
14 | #include <QtCore/qobject.h> |
15 | #include <QtCore/qstringfwd.h> |
16 | |
17 | #include <optional> |
18 | |
19 | QT_BEGIN_NAMESPACE |
20 | |
21 | class QGrpcOperationContext; |
22 | class QGrpcOperationPrivate; |
23 | |
24 | class Q_GRPC_EXPORT QGrpcOperation : public QObject |
25 | { |
26 | Q_OBJECT |
27 | public: |
28 | ~QGrpcOperation() override; |
29 | |
30 | template <typename T, QtProtobuf::if_protobuf_message<T> = true> |
31 | std::optional<T> read() const |
32 | { |
33 | std::optional<T> r(std::in_place); |
34 | if (!read(&*r)) |
35 | r.reset(); |
36 | return r; |
37 | } |
38 | bool read(QProtobufMessage *message) const; |
39 | |
40 | [[nodiscard]] const QHash<QByteArray, QByteArray> &metadata() const & noexcept; |
41 | void metadata() const && = delete; |
42 | |
43 | [[nodiscard]] QLatin1StringView method() const noexcept; |
44 | |
45 | [[nodiscard]] bool isFinished() const noexcept; |
46 | |
47 | Q_SIGNALS: |
48 | void finished(const QGrpcStatus &status); |
49 | |
50 | public Q_SLOTS: |
51 | void cancel(); |
52 | |
53 | protected: |
54 | explicit QGrpcOperation(std::shared_ptr<QGrpcOperationContext> operationContext, |
55 | QObject *parent = nullptr); |
56 | |
57 | [[nodiscard]] const QGrpcOperationContext &context() const & noexcept; |
58 | [[nodiscard]] QGrpcOperationContext &context() & noexcept |
59 | { |
60 | return const_cast<QGrpcOperationContext &>(std::as_const(t&: *this).context()); |
61 | } |
62 | void context() && = delete; |
63 | |
64 | private: |
65 | Q_DISABLE_COPY_MOVE(QGrpcOperation) |
66 | Q_DECLARE_PRIVATE(QGrpcOperation) |
67 | |
68 | public: |
69 | bool event(QEvent *event) override; |
70 | }; |
71 | |
72 | QT_END_NAMESPACE |
73 | |
74 | #endif // QGRPCOPERATION_H |
75 |