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 QGRPCSTATUS_H |
6 | #define QGRPCSTATUS_H |
7 | |
8 | #include <QtGrpc/qtgrpcglobal.h> |
9 | #include <QtGrpc/qtgrpcnamespace.h> |
10 | |
11 | #include <QtCore/qanystringview.h> |
12 | #include <QtCore/qcompare.h> |
13 | #include <QtCore/qmetatype.h> |
14 | #include <QtCore/qobjectdefs.h> |
15 | #include <QtCore/qstring.h> |
16 | #include <QtCore/qtclasshelpermacros.h> |
17 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | class QDataStream; |
21 | class QDebug; |
22 | class QVariant; |
23 | |
24 | class QGrpcStatus final |
25 | { |
26 | Q_GADGET_EXPORT(Q_GRPC_EXPORT) |
27 | Q_PROPERTY(QtGrpc::StatusCode code READ code CONSTANT) |
28 | Q_PROPERTY(QString message READ message CONSTANT) |
29 | |
30 | public: |
31 | Q_GRPC_EXPORT Q_IMPLICIT QGrpcStatus(QtGrpc::StatusCode code = {}, QAnyStringView message = {}); |
32 | Q_GRPC_EXPORT ~QGrpcStatus(); |
33 | Q_GRPC_EXPORT QGrpcStatus(const QGrpcStatus &other); |
34 | Q_GRPC_EXPORT QGrpcStatus &operator=(const QGrpcStatus &other); |
35 | QGrpcStatus(QGrpcStatus &&other) noexcept = default; |
36 | QGrpcStatus &operator=(QGrpcStatus &&other) noexcept = default; |
37 | |
38 | Q_GRPC_EXPORT Q_IMPLICIT operator QVariant() const; |
39 | |
40 | void swap(QGrpcStatus &other) noexcept |
41 | { |
42 | std::swap(a&: m_code, b&: other.m_code); |
43 | m_message.swap(other&: other.m_message); |
44 | } |
45 | |
46 | [[nodiscard]] QtGrpc::StatusCode code() const noexcept { return m_code; } |
47 | [[nodiscard]] bool isOk() const noexcept { return code() == QtGrpc::StatusCode::Ok; } |
48 | |
49 | [[nodiscard]] const QString &message() const & noexcept { return m_message; } |
50 | [[nodiscard]] QString message() && noexcept { return std::move(m_message); } |
51 | |
52 | private: |
53 | QtGrpc::StatusCode m_code; |
54 | QString m_message; |
55 | |
56 | friend bool comparesEqual(const QGrpcStatus &lhs, QtGrpc::StatusCode rhs) noexcept |
57 | { |
58 | return lhs.code() == rhs; |
59 | } |
60 | friend bool comparesEqual(const QGrpcStatus &lhs, const QGrpcStatus &rhs) noexcept |
61 | { |
62 | return lhs.code() == rhs.code(); |
63 | } |
64 | Q_DECLARE_EQUALITY_COMPARABLE(QGrpcStatus, QtGrpc::StatusCode) |
65 | Q_DECLARE_EQUALITY_COMPARABLE(QGrpcStatus) |
66 | |
67 | friend size_t qHash(const QGrpcStatus &key, size_t seed = 0) noexcept |
68 | { |
69 | return qHash(e: key.code(), seed); |
70 | } |
71 | |
72 | #ifndef QT_NO_DEBUG_STREAM |
73 | friend Q_GRPC_EXPORT QDebug operator<<(QDebug debug, const QGrpcStatus &status); |
74 | #endif |
75 | #ifndef QT_NO_DATASTREAM |
76 | friend Q_GRPC_EXPORT QDataStream &operator<<(QDataStream &out, const QGrpcStatus &status); |
77 | friend Q_GRPC_EXPORT QDataStream &operator>>(QDataStream &in, QGrpcStatus &status); |
78 | #endif |
79 | }; |
80 | |
81 | Q_DECLARE_SHARED(QGrpcStatus) |
82 | |
83 | QT_END_NAMESPACE |
84 | |
85 | #endif // QGRPCSTATUS_H |
86 | |