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)}, response_(response) {}
22
23 /// @brief Await response
24 ///
25 /// Upon completion the result is available in `response` that was
26 /// specified when initiating the asynchronous read
27 ///
28 /// `Get` should not be called multiple times for the same StreamReadFuture.
29 ///
30 /// @throws ugrpc::client::RpcError on an RPC error
31 /// @throws ugrpc::client::RpcCancelledError on task cancellation
33 if (future_.Get()) {
34 StructsResponse response;
35 proto_structs::MessageToStruct(response_, response);
36 return {response};
37 }
38 return std::nullopt;
39 }
40
41 /// @brief Checks if the asynchronous call has completed
42 /// Note, that once user gets result, IsReady should not be called
43 /// @return true if result ready
44 [[nodiscard]] bool IsReady() const noexcept { return future_.IsReady(); }
45
46private:
47 ProtobufStreamFuture future_;
48 ProtobufResponse& response_;
49};
50
51} // namespace grpc_proto_structs::client
52
53USERVER_NAMESPACE_END