QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qgrpcasyncoperationbase_p.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 #include <QObject>
29 #include <QMutex>
30 
31 #include <functional>
32 #include <memory>
33 
34 #include "qabstractgrpcchannel.h"
35 #include "qabstractgrpcclient.h"
36 
37 #include "qtgrpcglobal.h"
38 
39 namespace QtProtobuf {
40 
46 class Q_GRPC_EXPORT QGrpcAsyncOperationBase : public QObject
47 {
48  Q_OBJECT
49 public:
54  template <typename T>
55  T read() {
56  QMutexLocker locker(&m_asyncLock);
57  T value;
58  try {
59  value.deserialize(static_cast<QAbstractGrpcClient*>(parent())->serializer(), m_data);
60  } catch (std::invalid_argument &) {
61  static const QLatin1String invalidArgumentErrorMessage("Response deserialization failed invalid field found");
62  error({QGrpcStatus::InvalidArgument, invalidArgumentErrorMessage});
63  } catch (std::out_of_range &) {
64  static const QLatin1String outOfRangeErrorMessage("Invalid size of received buffer");
65  error({QGrpcStatus::OutOfRange, outOfRangeErrorMessage});
66  } catch (...) {
67  error({QGrpcStatus::Internal, QLatin1String("Unknown exception caught during deserialization")});
68  }
69  return value;
70  }
71 
77  void setData(const QByteArray &data)
78  {
79  QMutexLocker locker(&m_asyncLock);
80  m_data = data;
81  }
82 
83 signals:
88  void finished();
89 
94  void error(const QtProtobuf::QGrpcStatus &status);
95 
96 protected:
98  QGrpcAsyncOperationBase(const std::shared_ptr<QAbstractGrpcChannel> &channel, QAbstractGrpcClient *parent) : QObject(parent)
99  , m_channel(channel) {}
101  virtual ~QGrpcAsyncOperationBase();
102 
103  std::shared_ptr<QAbstractGrpcChannel> m_channel;
104 private:
105  QGrpcAsyncOperationBase();
106  Q_DISABLE_COPY_MOVE(QGrpcAsyncOperationBase)
107 
108  friend class QAbstractGrpcClient;
109 
110  QByteArray m_data;
111  QMutex m_asyncLock;
112 };
113 
114 }
The QGrpcStatus class contains information about last gRPC operation.
Definition: qgrpcstatus.h:58
@ OutOfRange
The operation was attempted past the valid range.
Definition: qgrpcstatus.h:82
@ Internal
Internal errors.
Definition: qgrpcstatus.h:84
@ InvalidArgument
The client specified an invalid argument.
Definition: qgrpcstatus.h:73