15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_OR_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_OR_H
18#include "google/cloud/internal/throw_delegate.h"
19#include "google/cloud/status.h"
20#include "google/cloud/version.h"
21#include "absl/types/optional.h"
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
91 static_assert(!std::is_reference<T>::value,
92 "StatusOr<T> requires T to **not** be a reference type");
95
96
97
98
102
103
104 StatusOr() : StatusOr(MakeDefaultStatus()) {}
106 StatusOr(StatusOr
const&) =
default;
107 StatusOr&
operator=(StatusOr
const&) =
default;
110 : status_(std::move(other.status_)), value_(std::move(other.value_)) {
111 other.status_ = MakeDefaultStatus();
114 StatusOr&
operator=(StatusOr&& other) {
115 status_ = std::move(other.status_);
116 value_ = std::move(other.value_);
117 other.status_ = MakeDefaultStatus();
122
123
124
125
126
127
128
129
130
134 google::
cloud::internal::ThrowInvalidArgument(
__func__);
139
140
141
142
143
145 *
this = StatusOr(std::move(status));
150
151
155 template <
typename U = T>
156 typename std::enable_if<
157 !std::is_same<StatusOr,
typename std::decay<U>::type>::value,
161 value_ = std::forward<U>(rhs);
166
167
168
169
170
171
172
173
174
176 StatusOr(T&& rhs) : value_(std::move(rhs)) {}
179 StatusOr(T
const& rhs) : value_(rhs) {}
182 bool ok()
const {
return status_
.ok(); }
189
190
191
192
193
194
195
196
197 T&
operator*() & {
return *value_; }
199 T
const&
operator*()
const& {
return *value_; }
201 T&&
operator*() && {
return *std::move(value_); }
203 T
const&&
operator*()
const&& {
return *std::move(value_); }
208
209
210
211
212
213
214
215
216 T*
operator->() & {
return &*value_; }
218 T
const*
operator->()
const& {
return &*value_; }
223
224
225
226
227
228
229
230
236 T
const&
value()
const& {
243 return std::move(**
this);
246 T
const&&
value()
const&& {
248 return std::move(**
this);
254
255
256
257
263 static Status MakeDefaultStatus() {
267 void CheckHasValue()
const& {
269 internal::ThrowStatus(status_);
274 void CheckHasValue() && {
276 internal::ThrowStatus(std::move(status_));
281 absl::optional<T> value_;
287bool operator==(StatusOr<T>
const& a, StatusOr<T>
const& b) {
288 if (!a || !b)
return a.status() == b.status();
295bool operator!=(StatusOr<T>
const& a, StatusOr<T>
const& b) {
301 return StatusOr<T>(std::move(rhs));
StatusOr(T const &rhs)
Definition: status_or.h:179
T && operator*() &&
Definition: status_or.h:201
T const * operator->() const &
Definition: status_or.h:218
T const && operator*() const &&
Definition: status_or.h:203
StatusOr(StatusOr &&other)
Definition: status_or.h:109
T const && value() const &&
Definition: status_or.h:246
Status && status() &&
Definition: status_or.h:259
StatusOr & operator=(Status status)
Assigns the given non-OK Status to this StatusOr<T>.
Definition: status_or.h:144
operator bool() const
Returns true when this holds a value.
Definition: status_or.h:185
Status const & status() const &
Definition: status_or.h:258
StatusOr(StatusOr const &)=default
T & operator*() &
Definition: status_or.h:197
T * operator->() &
Definition: status_or.h:216
StatusOr()
Initializes with an error status (StatusCode::kUnknown).
Definition: status_or.h:104
T const & operator*() const &
Definition: status_or.h:199
std::enable_if<!std::is_same< StatusOr, typenamestd::decay< U >::type >::value, StatusOr >::type & operator=(U &&rhs)
Assign a T (or anything convertible to T) into the StatusOr.
Definition: status_or.h:159
StatusOr(Status rhs)
Creates a new StatusOr<T> holding the error condition rhs.
Definition: status_or.h:132
T const & value() const &
Definition: status_or.h:236
StatusOr & operator=(StatusOr const &)=default
T && value() &&
Definition: status_or.h:241
StatusOr(T &&rhs)
Creates a new StatusOr<T> holding the value rhs.
Definition: status_or.h:176
StatusOr & operator=(StatusOr &&other)
Definition: status_or.h:114
T & value() &
Definition: status_or.h:231
bool ok() const
Returns true when this holds a value.
Definition: status_or.h:182
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
Status & operator=(Status &&) noexcept
Status()
Default constructor, initializes to StatusCode::kOk.
Status(StatusCode code, std::string message, ErrorInfo info={})
Construct from a status code, message and (optional) error info.
Status(Status &&) noexcept
Contains all the Google Cloud C++ Library APIs.
Definition: async_operation.h:23
StatusOr< T > make_status_or(T rhs)
Definition: status_or.h:300
StatusCode
Well-known status codes with grpc::StatusCode-compatible values.
Definition: status.h:35
@ kUnknown
kUnknown (gRPC code UNKNOWN) indicates an unknown error occurred.
bool operator==(StatusOr< T > const &a, StatusOr< T > const &b)
Definition: status_or.h:287
bool operator!=(StatusOr< T > const &a, StatusOr< T > const &b)
Definition: status_or.h:295
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