15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STREAM_RANGE_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STREAM_RANGE_H
18#include "google/cloud/internal/call_context.h"
19#include "google/cloud/status.h"
20#include "google/cloud/status_or.h"
21#include "google/cloud/version.h"
22#include "absl/types/variant.h"
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
56using StreamReader = std::function<absl::variant<
Status, T>()>;
60StreamRange<T> MakeStreamRange(StreamReader<T>);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
99
100
101
102
103 template <
typename U>
106 using iterator_category = std::input_iterator_tag;
107 using value_type = U;
108 using difference_type = std::size_t;
109 using reference = value_type&;
110 using pointer = value_type*;
111 using const_reference = value_type
const&;
112 using const_pointer = value_type
const*;
117 reference
operator*() {
return owner_->current_; }
118 pointer
operator->() {
return &owner_->current_; }
119 const_reference
operator*()
const {
return owner_->current_; }
120 const_pointer
operator->()
const {
return &owner_->current_; }
124 is_end_ = owner_->is_end_;
135 return a.is_end_ == b.is_end_;
144 explicit IteratorImpl(StreamRange* owner)
145 : owner_(owner), is_end_(owner_->is_end_) {}
150 using value_type = StatusOr<T>;
152 using difference_type =
typename iterator::difference_type;
153 using reference =
typename iterator::reference;
154 using pointer =
typename iterator::pointer;
155 using const_reference =
typename iterator::const_reference;
156 using const_pointer =
typename iterator::const_pointer;
159
160
164 internal::ScopedCallContext scope(call_context_);
171 StreamRange&
operator=(StreamRange
const&) =
delete;
175 StreamRange&
operator=(StreamRange&&) =
default;
178 iterator
begin() {
return iterator(
this); }
179 iterator
end() {
return iterator(); }
184 if (!is_end_ && !current_ok_) {
188 struct UnpackVariant {
190 void operator()(
Status&& status) {
191 sr.is_end_ = status
.ok();
192 sr.current_ok_ = status
.ok();
193 if (!status
.ok()) sr.current_ = std::move(status);
195 void operator()(T&& t) {
197 sr.current_ok_ =
true;
198 sr.current_ = std::move(t);
201 internal::ScopedCallContext scope(call_context_);
203 absl::visit(UnpackVariant{*
this}, std::move(v));
206 template <
typename U>
207 friend StreamRange<U> internal::MakeStreamRange(internal::StreamReader<U>);
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 explicit StreamRange(internal::StreamReader<T> reader)
236 : reader_(std::move(reader)) {
240 internal::CallContext call_context_;
241 internal::StreamReader<T> reader_;
242 StatusOr<T> current_;
243 bool current_ok_ =
false;
250
251
252
253
254
255
256
257
258
259
261StreamRange<T> MakeStreamRange(StreamReader<T> reader) {
262 return StreamRange<T>{std::move(reader)};
Represents success or an error with info about the error.
Definition: status.h:295
bool ok() const
Returns true if the status code is StatusCode::kOk.
Definition: status.h:328
An input iterator for a StreamRange<T> – DO NOT USE DIRECTLY.
Definition: stream_range.h:104
friend bool operator==(IteratorImpl const &a, IteratorImpl const &b)
Definition: stream_range.h:134
pointer operator->()
Definition: stream_range.h:118
IteratorImpl & operator++()
Definition: stream_range.h:122
IteratorImpl operator++(int)
Definition: stream_range.h:128
const_pointer operator->() const
Definition: stream_range.h:120
IteratorImpl()=default
Constructs an "end" iterator.
const_reference operator*() const
Definition: stream_range.h:119
reference operator*()
Definition: stream_range.h:117
friend bool operator!=(IteratorImpl const &a, IteratorImpl const &b)
Definition: stream_range.h:138
StreamRange & operator=(StreamRange &&)=default
iterator begin()
Definition: stream_range.h:178
StreamRange()=default
Default-constructs an empty range.
StreamRange(StreamRange const &)=delete
StreamRange(StreamRange &&)=default
iterator end()
Definition: stream_range.h:179
StreamRange & operator=(StreamRange const &)=delete
~StreamRange()
Definition: stream_range.h:163
Contains all the Google Cloud C++ Library APIs.
Definition: async_operation.h:23
Definition: async_operation.h:22
#define GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Definition: version.h:45
#define GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
Definition: version.h:43