userver: /data/code/userver/libraries/grpc-proto-structs/include/userver/grpc-proto-structs/client/stream_read_future.hpp Source File
Loading...
Searching...
No Matches
stream_read_future.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief @copybrief grpc_proto_structs::client::StreamReadFuture
5
6#include <userver/proto-structs/convert.hpp>
7#include <userver/ugrpc/client/stream_read_future.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace grpc_proto_structs::client {
12
13/// @brief proto-struct stream response future.
14template <typename StructsResponse>
15class StreamReadFuture final {
16public:
17 using ProtobufResponse = proto_structs::traits::CompatibleMessageType<StructsResponse>;
18 using ProtobufStreamFuture = ugrpc::client::StreamReadFuture<ProtobufResponse>;
19
20 explicit StreamReadFuture(ProtobufStreamFuture&& future, ProtobufResponse& response)
21 : future_{std::move(future)},
22 response_(response)
23 {}
24
25 /// @brief Await response
26 ///
27 /// Upon completion the result is available in `response` that was
28 /// specified when initiating the asynchronous read
29 ///
30 /// `Get` should not be called multiple times for the same StreamReadFuture.
31 ///
32 /// @throws ugrpc::client::RpcError on an RPC error
33 /// @throws ugrpc::client::RpcCancelledError on task cancellation
35 if (future_.Get()) {
36 StructsResponse response;
37 proto_structs::MessageToStruct(response_, response);
38 return {response};
39 }
40 return std::nullopt;
41 }
42
43 /// @brief Checks if the asynchronous call has completed
44 /// Note, that once user gets result, IsReady should not be called
45 /// @return true if result ready
46 [[nodiscard]] bool IsReady() const noexcept { return future_.IsReady(); }
47
48private:
49 ProtobufStreamFuture future_;
50 ProtobufResponse& response_;
51};
52
53} // namespace grpc_proto_structs::client
54
55USERVER_NAMESPACE_END