1 // Copyright 2020 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: statusor.h
17 // -----------------------------------------------------------------------------
18 //
19 // An `absl::StatusOr<T>` represents a union of an `absl::Status` object
20 // and an object of type `T`. The `absl::StatusOr<T>` will either contain an
21 // object of type `T` (indicating a successful operation), or an error (of type
22 // `absl::Status`) explaining why such a value is not present.
23 //
24 // In general, check the success of an operation returning an
25 // `absl::StatusOr<T>` like you would an `absl::Status` by using the `ok()`
26 // member function.
27 //
28 // Example:
29 //
30 // StatusOr<Foo> result = Calculation();
31 // if (result.ok()) {
32 // result->DoSomethingCool();
33 // } else {
34 // LOG(ERROR) << result.status();
35 // }
36 #ifndef ABSL_STATUS_STATUSOR_H_
37 #define ABSL_STATUS_STATUSOR_H_
38
39 #include <exception>
40 #include <initializer_list>
41 #include <new>
42 #include <ostream>
43 #include <string>
44 #include <type_traits>
45 #include <utility>
46
47 #include "absl/base/attributes.h"
48 #include "absl/base/nullability.h"
49 #include "absl/base/call_once.h"
50 #include "absl/meta/type_traits.h"
51 #include "absl/status/internal/statusor_internal.h"
52 #include "absl/status/status.h"
53 #include "absl/strings/has_absl_stringify.h"
54 #include "absl/strings/has_ostream_operator.h"
55 #include "absl/strings/str_format.h"
56 #include "absl/types/variant.h"
57 #include "absl/utility/utility.h"
58
59 namespace absl {
60 ABSL_NAMESPACE_BEGIN
61
62 // BadStatusOrAccess
63 //
64 // This class defines the type of object to throw (if exceptions are enabled),
65 // when accessing the value of an `absl::StatusOr<T>` object that does not
66 // contain a value. This behavior is analogous to that of
67 // `std::bad_optional_access` in the case of accessing an invalid
68 // `std::optional` value.
69 //
70 // Example:
71 //
72 // try {
73 // absl::StatusOr<int> v = FetchInt();
74 // DoWork(v.value()); // Accessing value() when not "OK" may throw
75 // } catch (absl::BadStatusOrAccess& ex) {
76 // LOG(ERROR) << ex.status();
77 // }
78 class BadStatusOrAccess : public std::exception {
79 public:
80 explicit BadStatusOrAccess(absl::Status status);
81 ~BadStatusOrAccess() override = default;
82
83 BadStatusOrAccess(const BadStatusOrAccess& other);
84 BadStatusOrAccess& operator=(const BadStatusOrAccess& other);
85 BadStatusOrAccess(BadStatusOrAccess&& other);
86 BadStatusOrAccess& operator=(BadStatusOrAccess&& other);
87
88 // BadStatusOrAccess::what()
89 //
90 // Returns the associated explanatory string of the `absl::StatusOr<T>`
91 // object's error code. This function contains information about the failing
92 // status, but its exact formatting may change and should not be depended on.
93 //
94 // The pointer of this string is guaranteed to be valid until any non-const
95 // function is invoked on the exception object.
96 absl::Nonnull<const char*> what() const noexcept override;
97
98 // BadStatusOrAccess::status()
99 //
100 // Returns the associated `absl::Status` of the `absl::StatusOr<T>` object's
101 // error.
102 const absl::Status& status() const;
103
104 private:
105 void InitWhat() const;
106
107 absl::Status status_;
108 mutable absl::once_flag init_what_;
109 mutable std::string what_;
110 };
111
112 // Returned StatusOr objects may not be ignored.
113 template <typename T>
114 #if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
115 // TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict
116 // [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available.
117 class [[nodiscard]] StatusOr;
118 #else
119 class ABSL_MUST_USE_RESULT StatusOr;
120 #endif // ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
121
122 // absl::StatusOr<T>
123 //
124 // The `absl::StatusOr<T>` class template is a union of an `absl::Status` object
125 // and an object of type `T`. The `absl::StatusOr<T>` models an object that is
126 // either a usable object, or an error (of type `absl::Status`) explaining why
127 // such an object is not present. An `absl::StatusOr<T>` is typically the return
128 // value of a function which may fail.
129 //
130 // An `absl::StatusOr<T>` can never hold an "OK" status (an
131 // `absl::StatusCode::kOk` value); instead, the presence of an object of type
132 // `T` indicates success. Instead of checking for a `kOk` value, use the
133 // `absl::StatusOr<T>::ok()` member function. (It is for this reason, and code
134 // readability, that using the `ok()` function is preferred for `absl::Status`
135 // as well.)
136 //
137 // Example:
138 //
139 // StatusOr<Foo> result = DoBigCalculationThatCouldFail();
140 // if (result.ok()) {
141 // result->DoSomethingCool();
142 // } else {
143 // LOG(ERROR) << result.status();
144 // }
145 //
146 // Accessing the object held by an `absl::StatusOr<T>` should be performed via
147 // `operator*` or `operator->`, after a call to `ok()` confirms that the
148 // `absl::StatusOr<T>` holds an object of type `T`:
149 //
150 // Example:
151 //
152 // absl::StatusOr<int> i = GetCount();
153 // if (i.ok()) {
154 // updated_total += *i;
155 // }
156 //
157 // NOTE: using `absl::StatusOr<T>::value()` when no valid value is present will
158 // throw an exception if exceptions are enabled or terminate the process when
159 // exceptions are not enabled.
160 //
161 // Example:
162 //
163 // StatusOr<Foo> result = DoBigCalculationThatCouldFail();
164 // const Foo& foo = result.value(); // Crash/exception if no value present
165 // foo.DoSomethingCool();
166 //
167 // A `absl::StatusOr<T*>` can be constructed from a null pointer like any other
168 // pointer value, and the result will be that `ok()` returns `true` and
169 // `value()` returns `nullptr`. Checking the value of pointer in an
170 // `absl::StatusOr<T*>` generally requires a bit more care, to ensure both that
171 // a value is present and that value is not null:
172 //
173 // StatusOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
174 // if (!result.ok()) {
175 // LOG(ERROR) << result.status();
176 // } else if (*result == nullptr) {
177 // LOG(ERROR) << "Unexpected null pointer";
178 // } else {
179 // (*result)->DoSomethingCool();
180 // }
181 //
182 // Example factory implementation returning StatusOr<T>:
183 //
184 // StatusOr<Foo> FooFactory::MakeFoo(int arg) {
185 // if (arg <= 0) {
186 // return absl::Status(absl::StatusCode::kInvalidArgument,
187 // "Arg must be positive");
188 // }
189 // return Foo(arg);
190 // }
191 template <typename T>
192 class StatusOr : private internal_statusor::StatusOrData<T>,
193 private internal_statusor::CopyCtorBase<T>,
194 private internal_statusor::MoveCtorBase<T>,
195 private internal_statusor::CopyAssignBase<T>,
196 private internal_statusor::MoveAssignBase<T> {
197 template <typename U>
198 friend class StatusOr;
199
200 typedef internal_statusor::StatusOrData<T> Base;
201
202 public:
203 // StatusOr<T>::value_type
204 //
205 // This instance data provides a generic `value_type` member for use within
206 // generic programming. This usage is analogous to that of
207 // `optional::value_type` in the case of `std::optional`.
208 typedef T value_type;
209
210 // Constructors
211
212 // Constructs a new `absl::StatusOr` with an `absl::StatusCode::kUnknown`
213 // status. This constructor is marked 'explicit' to prevent usages in return
214 // values such as 'return {};', under the misconception that
215 // `absl::StatusOr<std::vector<int>>` will be initialized with an empty
216 // vector, instead of an `absl::StatusCode::kUnknown` error code.
217 explicit StatusOr();
218
219 // `StatusOr<T>` is copy constructible if `T` is copy constructible.
220 StatusOr(const StatusOr&) = default;
221 // `StatusOr<T>` is copy assignable if `T` is copy constructible and copy
222 // assignable.
223 StatusOr& operator=(const StatusOr&) = default;
224
225 // `StatusOr<T>` is move constructible if `T` is move constructible.
226 StatusOr(StatusOr&&) = default;
227 // `StatusOr<T>` is moveAssignable if `T` is move constructible and move
228 // assignable.
229 StatusOr& operator=(StatusOr&&) = default;
230
231 // Converting Constructors
232
233 // Constructs a new `absl::StatusOr<T>` from an `absl::StatusOr<U>`, when `T`
234 // is constructible from `U`. To avoid ambiguity, these constructors are
235 // disabled if `T` is also constructible from `StatusOr<U>.`. This constructor
236 // is explicit if and only if the corresponding construction of `T` from `U`
237 // is explicit. (This constructor inherits its explicitness from the
238 // underlying constructor.)
239 template <
240 typename U,
241 absl::enable_if_t<
242 absl::conjunction<
243 absl::negation<std::is_same<T, U>>,
244 std::is_constructible<T, const U&>,
245 std::is_convertible<const U&, T>,
246 absl::negation<
247 internal_statusor::IsConstructibleOrConvertibleFromStatusOr<
248 T, U>>>::value,
249 int> = 0>
StatusOr(const StatusOr<U> & other)250 StatusOr(const StatusOr<U>& other) // NOLINT
251 : Base(static_cast<const typename StatusOr<U>::Base&>(other)) {}
252 template <
253 typename U,
254 absl::enable_if_t<
255 absl::conjunction<
256 absl::negation<std::is_same<T, U>>,
257 std::is_constructible<T, const U&>,
258 absl::negation<std::is_convertible<const U&, T>>,
259 absl::negation<
260 internal_statusor::IsConstructibleOrConvertibleFromStatusOr<
261 T, U>>>::value,
262 int> = 0>
StatusOr(const StatusOr<U> & other)263 explicit StatusOr(const StatusOr<U>& other)
264 : Base(static_cast<const typename StatusOr<U>::Base&>(other)) {}
265
266 template <
267 typename U,
268 absl::enable_if_t<
269 absl::conjunction<
270 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>,
271 std::is_convertible<U&&, T>,
272 absl::negation<
273 internal_statusor::IsConstructibleOrConvertibleFromStatusOr<
274 T, U>>>::value,
275 int> = 0>
StatusOr(StatusOr<U> && other)276 StatusOr(StatusOr<U>&& other) // NOLINT
277 : Base(static_cast<typename StatusOr<U>::Base&&>(other)) {}
278 template <
279 typename U,
280 absl::enable_if_t<
281 absl::conjunction<
282 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>,
283 absl::negation<std::is_convertible<U&&, T>>,
284 absl::negation<
285 internal_statusor::IsConstructibleOrConvertibleFromStatusOr<
286 T, U>>>::value,
287 int> = 0>
StatusOr(StatusOr<U> && other)288 explicit StatusOr(StatusOr<U>&& other)
289 : Base(static_cast<typename StatusOr<U>::Base&&>(other)) {}
290
291 // Converting Assignment Operators
292
293 // Creates an `absl::StatusOr<T>` through assignment from an
294 // `absl::StatusOr<U>` when:
295 //
296 // * Both `absl::StatusOr<T>` and `absl::StatusOr<U>` are OK by assigning
297 // `U` to `T` directly.
298 // * `absl::StatusOr<T>` is OK and `absl::StatusOr<U>` contains an error
299 // code by destroying `absl::StatusOr<T>`'s value and assigning from
300 // `absl::StatusOr<U>'
301 // * `absl::StatusOr<T>` contains an error code and `absl::StatusOr<U>` is
302 // OK by directly initializing `T` from `U`.
303 // * Both `absl::StatusOr<T>` and `absl::StatusOr<U>` contain an error
304 // code by assigning the `Status` in `absl::StatusOr<U>` to
305 // `absl::StatusOr<T>`
306 //
307 // These overloads only apply if `absl::StatusOr<T>` is constructible and
308 // assignable from `absl::StatusOr<U>` and `StatusOr<T>` cannot be directly
309 // assigned from `StatusOr<U>`.
310 template <
311 typename U,
312 absl::enable_if_t<
313 absl::conjunction<
314 absl::negation<std::is_same<T, U>>,
315 std::is_constructible<T, const U&>,
316 std::is_assignable<T, const U&>,
317 absl::negation<
318 internal_statusor::
319 IsConstructibleOrConvertibleOrAssignableFromStatusOr<
320 T, U>>>::value,
321 int> = 0>
322 StatusOr& operator=(const StatusOr<U>& other) {
323 this->Assign(other);
324 return *this;
325 }
326 template <
327 typename U,
328 absl::enable_if_t<
329 absl::conjunction<
330 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>,
331 std::is_assignable<T, U&&>,
332 absl::negation<
333 internal_statusor::
334 IsConstructibleOrConvertibleOrAssignableFromStatusOr<
335 T, U>>>::value,
336 int> = 0>
337 StatusOr& operator=(StatusOr<U>&& other) {
338 this->Assign(std::move(other));
339 return *this;
340 }
341
342 // Constructs a new `absl::StatusOr<T>` with a non-ok status. After calling
343 // this constructor, `this->ok()` will be `false` and calls to `value()` will
344 // crash, or produce an exception if exceptions are enabled.
345 //
346 // The constructor also takes any type `U` that is convertible to
347 // `absl::Status`. This constructor is explicit if an only if `U` is not of
348 // type `absl::Status` and the conversion from `U` to `Status` is explicit.
349 //
350 // REQUIRES: !Status(std::forward<U>(v)).ok(). This requirement is DCHECKed.
351 // In optimized builds, passing absl::OkStatus() here will have the effect
352 // of passing absl::StatusCode::kInternal as a fallback.
353 template <
354 typename U = absl::Status,
355 absl::enable_if_t<
356 absl::conjunction<
357 std::is_convertible<U&&, absl::Status>,
358 std::is_constructible<absl::Status, U&&>,
359 absl::negation<std::is_same<absl::decay_t<U>, absl::StatusOr<T>>>,
360 absl::negation<std::is_same<absl::decay_t<U>, T>>,
361 absl::negation<std::is_same<absl::decay_t<U>, absl::in_place_t>>,
362 absl::negation<internal_statusor::HasConversionOperatorToStatusOr<
363 T, U&&>>>::value,
364 int> = 0>
StatusOr(U && v)365 StatusOr(U&& v) : Base(std::forward<U>(v)) {}
366
367 template <
368 typename U = absl::Status,
369 absl::enable_if_t<
370 absl::conjunction<
371 absl::negation<std::is_convertible<U&&, absl::Status>>,
372 std::is_constructible<absl::Status, U&&>,
373 absl::negation<std::is_same<absl::decay_t<U>, absl::StatusOr<T>>>,
374 absl::negation<std::is_same<absl::decay_t<U>, T>>,
375 absl::negation<std::is_same<absl::decay_t<U>, absl::in_place_t>>,
376 absl::negation<internal_statusor::HasConversionOperatorToStatusOr<
377 T, U&&>>>::value,
378 int> = 0>
StatusOr(U && v)379 explicit StatusOr(U&& v) : Base(std::forward<U>(v)) {}
380
381 template <
382 typename U = absl::Status,
383 absl::enable_if_t<
384 absl::conjunction<
385 std::is_convertible<U&&, absl::Status>,
386 std::is_constructible<absl::Status, U&&>,
387 absl::negation<std::is_same<absl::decay_t<U>, absl::StatusOr<T>>>,
388 absl::negation<std::is_same<absl::decay_t<U>, T>>,
389 absl::negation<std::is_same<absl::decay_t<U>, absl::in_place_t>>,
390 absl::negation<internal_statusor::HasConversionOperatorToStatusOr<
391 T, U&&>>>::value,
392 int> = 0>
393 StatusOr& operator=(U&& v) {
394 this->AssignStatus(std::forward<U>(v));
395 return *this;
396 }
397
398 // Perfect-forwarding value assignment operator.
399
400 // If `*this` contains a `T` value before the call, the contained value is
401 // assigned from `std::forward<U>(v)`; Otherwise, it is directly-initialized
402 // from `std::forward<U>(v)`.
403 // This function does not participate in overload unless:
404 // 1. `std::is_constructible_v<T, U>` is true,
405 // 2. `std::is_assignable_v<T&, U>` is true.
406 // 3. `std::is_same_v<StatusOr<T>, std::remove_cvref_t<U>>` is false.
407 // 4. Assigning `U` to `T` is not ambiguous:
408 // If `U` is `StatusOr<V>` and `T` is constructible and assignable from
409 // both `StatusOr<V>` and `V`, the assignment is considered bug-prone and
410 // ambiguous thus will fail to compile. For example:
411 // StatusOr<bool> s1 = true; // s1.ok() && *s1 == true
412 // StatusOr<bool> s2 = false; // s2.ok() && *s2 == false
413 // s1 = s2; // ambiguous, `s1 = *s2` or `s1 = bool(s2)`?
414 template <
415 typename U = T,
416 typename = typename std::enable_if<absl::conjunction<
417 std::is_constructible<T, U&&>, std::is_assignable<T&, U&&>,
418 absl::disjunction<
419 std::is_same<absl::remove_cvref_t<U>, T>,
420 absl::conjunction<
421 absl::negation<std::is_convertible<U&&, absl::Status>>,
422 absl::negation<internal_statusor::
423 HasConversionOperatorToStatusOr<T, U&&>>>>,
424 internal_statusor::IsForwardingAssignmentValid<T, U&&>>::value>::type>
425 StatusOr& operator=(U&& v) {
426 this->Assign(std::forward<U>(v));
427 return *this;
428 }
429
430 // Constructs the inner value `T` in-place using the provided args, using the
431 // `T(args...)` constructor.
432 template <typename... Args>
433 explicit StatusOr(absl::in_place_t, Args&&... args);
434 template <typename U, typename... Args>
435 explicit StatusOr(absl::in_place_t, std::initializer_list<U> ilist,
436 Args&&... args);
437
438 // Constructs the inner value `T` in-place using the provided args, using the
439 // `T(U)` (direct-initialization) constructor. This constructor is only valid
440 // if `T` can be constructed from a `U`. Can accept move or copy constructors.
441 //
442 // This constructor is explicit if `U` is not convertible to `T`. To avoid
443 // ambiguity, this constructor is disabled if `U` is a `StatusOr<J>`, where
444 // `J` is convertible to `T`.
445 template <
446 typename U = T,
447 absl::enable_if_t<
448 absl::conjunction<
449 internal_statusor::IsDirectInitializationValid<T, U&&>,
450 std::is_constructible<T, U&&>, std::is_convertible<U&&, T>,
451 absl::disjunction<
452 std::is_same<absl::remove_cvref_t<U>, T>,
453 absl::conjunction<
454 absl::negation<std::is_convertible<U&&, absl::Status>>,
455 absl::negation<
456 internal_statusor::HasConversionOperatorToStatusOr<
457 T, U&&>>>>>::value,
458 int> = 0>
StatusOr(U && u)459 StatusOr(U&& u) // NOLINT
460 : StatusOr(absl::in_place, std::forward<U>(u)) {}
461
462 template <
463 typename U = T,
464 absl::enable_if_t<
465 absl::conjunction<
466 internal_statusor::IsDirectInitializationValid<T, U&&>,
467 absl::disjunction<
468 std::is_same<absl::remove_cvref_t<U>, T>,
469 absl::conjunction<
470 absl::negation<std::is_constructible<absl::Status, U&&>>,
471 absl::negation<
472 internal_statusor::HasConversionOperatorToStatusOr<
473 T, U&&>>>>,
474 std::is_constructible<T, U&&>,
475 absl::negation<std::is_convertible<U&&, T>>>::value,
476 int> = 0>
StatusOr(U && u)477 explicit StatusOr(U&& u) // NOLINT
478 : StatusOr(absl::in_place, std::forward<U>(u)) {}
479
480 // StatusOr<T>::ok()
481 //
482 // Returns whether or not this `absl::StatusOr<T>` holds a `T` value. This
483 // member function is analogous to `absl::Status::ok()` and should be used
484 // similarly to check the status of return values.
485 //
486 // Example:
487 //
488 // StatusOr<Foo> result = DoBigCalculationThatCouldFail();
489 // if (result.ok()) {
490 // // Handle result
491 // else {
492 // // Handle error
493 // }
ok()494 ABSL_MUST_USE_RESULT bool ok() const { return this->status_.ok(); }
495
496 // StatusOr<T>::status()
497 //
498 // Returns a reference to the current `absl::Status` contained within the
499 // `absl::StatusOr<T>`. If `absl::StatusOr<T>` contains a `T`, then this
500 // function returns `absl::OkStatus()`.
501 const Status& status() const&;
502 Status status() &&;
503
504 // StatusOr<T>::value()
505 //
506 // Returns a reference to the held value if `this->ok()`. Otherwise, throws
507 // `absl::BadStatusOrAccess` if exceptions are enabled, or is guaranteed to
508 // terminate the process if exceptions are disabled.
509 //
510 // If you have already checked the status using `this->ok()`, you probably
511 // want to use `operator*()` or `operator->()` to access the value instead of
512 // `value`.
513 //
514 // Note: for value types that are cheap to copy, prefer simple code:
515 //
516 // T value = statusor.value();
517 //
518 // Otherwise, if the value type is expensive to copy, but can be left
519 // in the StatusOr, simply assign to a reference:
520 //
521 // T& value = statusor.value(); // or `const T&`
522 //
523 // Otherwise, if the value type supports an efficient move, it can be
524 // used as follows:
525 //
526 // T value = std::move(statusor).value();
527 //
528 // The `std::move` on statusor instead of on the whole expression enables
529 // warnings about possible uses of the statusor object after the move.
530 const T& value() const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
531 T& value() & ABSL_ATTRIBUTE_LIFETIME_BOUND;
532 const T&& value() const&& ABSL_ATTRIBUTE_LIFETIME_BOUND;
533 T&& value() && ABSL_ATTRIBUTE_LIFETIME_BOUND;
534
535 // StatusOr<T>:: operator*()
536 //
537 // Returns a reference to the current value.
538 //
539 // REQUIRES: `this->ok() == true`, otherwise the behavior is undefined.
540 //
541 // Use `this->ok()` to verify that there is a current value within the
542 // `absl::StatusOr<T>`. Alternatively, see the `value()` member function for a
543 // similar API that guarantees crashing or throwing an exception if there is
544 // no current value.
545 const T& operator*() const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
546 T& operator*() & ABSL_ATTRIBUTE_LIFETIME_BOUND;
547 const T&& operator*() const&& ABSL_ATTRIBUTE_LIFETIME_BOUND;
548 T&& operator*() && ABSL_ATTRIBUTE_LIFETIME_BOUND;
549
550 // StatusOr<T>::operator->()
551 //
552 // Returns a pointer to the current value.
553 //
554 // REQUIRES: `this->ok() == true`, otherwise the behavior is undefined.
555 //
556 // Use `this->ok()` to verify that there is a current value.
557 const T* operator->() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
558 T* operator->() ABSL_ATTRIBUTE_LIFETIME_BOUND;
559
560 // StatusOr<T>::value_or()
561 //
562 // Returns the current value if `this->ok() == true`. Otherwise constructs a
563 // value using the provided `default_value`.
564 //
565 // Unlike `value`, this function returns by value, copying the current value
566 // if necessary. If the value type supports an efficient move, it can be used
567 // as follows:
568 //
569 // T value = std::move(statusor).value_or(def);
570 //
571 // Unlike with `value`, calling `std::move()` on the result of `value_or` will
572 // still trigger a copy.
573 template <typename U>
574 T value_or(U&& default_value) const&;
575 template <typename U>
576 T value_or(U&& default_value) &&;
577
578 // StatusOr<T>::IgnoreError()
579 //
580 // Ignores any errors. This method does nothing except potentially suppress
581 // complaints from any tools that are checking that errors are not dropped on
582 // the floor.
583 void IgnoreError() const;
584
585 // StatusOr<T>::emplace()
586 //
587 // Reconstructs the inner value T in-place using the provided args, using the
588 // T(args...) constructor. Returns reference to the reconstructed `T`.
589 template <typename... Args>
emplace(Args &&...args)590 T& emplace(Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
591 if (ok()) {
592 this->Clear();
593 this->MakeValue(std::forward<Args>(args)...);
594 } else {
595 this->MakeValue(std::forward<Args>(args)...);
596 this->status_ = absl::OkStatus();
597 }
598 return this->data_;
599 }
600
601 template <
602 typename U, typename... Args,
603 absl::enable_if_t<
604 std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
605 int> = 0>
emplace(std::initializer_list<U> ilist,Args &&...args)606 T& emplace(std::initializer_list<U> ilist,
607 Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
608 if (ok()) {
609 this->Clear();
610 this->MakeValue(ilist, std::forward<Args>(args)...);
611 } else {
612 this->MakeValue(ilist, std::forward<Args>(args)...);
613 this->status_ = absl::OkStatus();
614 }
615 return this->data_;
616 }
617
618 // StatusOr<T>::AssignStatus()
619 //
620 // Sets the status of `absl::StatusOr<T>` to the given non-ok status value.
621 //
622 // NOTE: We recommend using the constructor and `operator=` where possible.
623 // This method is intended for use in generic programming, to enable setting
624 // the status of a `StatusOr<T>` when `T` may be `Status`. In that case, the
625 // constructor and `operator=` would assign into the inner value of type
626 // `Status`, rather than status of the `StatusOr` (b/280392796).
627 //
628 // REQUIRES: !Status(std::forward<U>(v)).ok(). This requirement is DCHECKed.
629 // In optimized builds, passing absl::OkStatus() here will have the effect
630 // of passing absl::StatusCode::kInternal as a fallback.
631 using internal_statusor::StatusOrData<T>::AssignStatus;
632
633 private:
634 using internal_statusor::StatusOrData<T>::Assign;
635 template <typename U>
636 void Assign(const absl::StatusOr<U>& other);
637 template <typename U>
638 void Assign(absl::StatusOr<U>&& other);
639 };
640
641 // operator==()
642 //
643 // This operator checks the equality of two `absl::StatusOr<T>` objects.
644 template <typename T>
645 bool operator==(const StatusOr<T>& lhs, const StatusOr<T>& rhs) {
646 if (lhs.ok() && rhs.ok()) return *lhs == *rhs;
647 return lhs.status() == rhs.status();
648 }
649
650 // operator!=()
651 //
652 // This operator checks the inequality of two `absl::StatusOr<T>` objects.
653 template <typename T>
654 bool operator!=(const StatusOr<T>& lhs, const StatusOr<T>& rhs) {
655 return !(lhs == rhs);
656 }
657
658 // Prints the `value` or the status in brackets to `os`.
659 //
660 // Requires `T` supports `operator<<`. Do not rely on the output format which
661 // may change without notice.
662 template <typename T, typename std::enable_if<
663 absl::HasOstreamOperator<T>::value, int>::type = 0>
664 std::ostream& operator<<(std::ostream& os, const StatusOr<T>& status_or) {
665 if (status_or.ok()) {
666 os << status_or.value();
667 } else {
668 os << internal_statusor::StringifyRandom::OpenBrackets()
669 << status_or.status()
670 << internal_statusor::StringifyRandom::CloseBrackets();
671 }
672 return os;
673 }
674
675 // As above, but supports `StrCat`, `StrFormat`, etc.
676 //
677 // Requires `T` has `AbslStringify`. Do not rely on the output format which
678 // may change without notice.
679 template <
680 typename Sink, typename T,
681 typename std::enable_if<absl::HasAbslStringify<T>::value, int>::type = 0>
AbslStringify(Sink & sink,const StatusOr<T> & status_or)682 void AbslStringify(Sink& sink, const StatusOr<T>& status_or) {
683 if (status_or.ok()) {
684 absl::Format(&sink, "%v", status_or.value());
685 } else {
686 absl::Format(&sink, "%s%v%s",
687 internal_statusor::StringifyRandom::OpenBrackets(),
688 status_or.status(),
689 internal_statusor::StringifyRandom::CloseBrackets());
690 }
691 }
692
693 //------------------------------------------------------------------------------
694 // Implementation details for StatusOr<T>
695 //------------------------------------------------------------------------------
696
697 // TODO(sbenza): avoid the string here completely.
698 template <typename T>
StatusOr()699 StatusOr<T>::StatusOr() : Base(Status(absl::StatusCode::kUnknown, "")) {}
700
701 template <typename T>
702 template <typename U>
Assign(const StatusOr<U> & other)703 inline void StatusOr<T>::Assign(const StatusOr<U>& other) {
704 if (other.ok()) {
705 this->Assign(*other);
706 } else {
707 this->AssignStatus(other.status());
708 }
709 }
710
711 template <typename T>
712 template <typename U>
Assign(StatusOr<U> && other)713 inline void StatusOr<T>::Assign(StatusOr<U>&& other) {
714 if (other.ok()) {
715 this->Assign(*std::move(other));
716 } else {
717 this->AssignStatus(std::move(other).status());
718 }
719 }
720 template <typename T>
721 template <typename... Args>
StatusOr(absl::in_place_t,Args &&...args)722 StatusOr<T>::StatusOr(absl::in_place_t, Args&&... args)
723 : Base(absl::in_place, std::forward<Args>(args)...) {}
724
725 template <typename T>
726 template <typename U, typename... Args>
StatusOr(absl::in_place_t,std::initializer_list<U> ilist,Args &&...args)727 StatusOr<T>::StatusOr(absl::in_place_t, std::initializer_list<U> ilist,
728 Args&&... args)
729 : Base(absl::in_place, ilist, std::forward<Args>(args)...) {}
730
731 template <typename T>
status()732 const Status& StatusOr<T>::status() const& {
733 return this->status_;
734 }
735 template <typename T>
status()736 Status StatusOr<T>::status() && {
737 return ok() ? OkStatus() : std::move(this->status_);
738 }
739
740 template <typename T>
value()741 const T& StatusOr<T>::value() const& {
742 if (!this->ok()) internal_statusor::ThrowBadStatusOrAccess(this->status_);
743 return this->data_;
744 }
745
746 template <typename T>
value()747 T& StatusOr<T>::value() & {
748 if (!this->ok()) internal_statusor::ThrowBadStatusOrAccess(this->status_);
749 return this->data_;
750 }
751
752 template <typename T>
value()753 const T&& StatusOr<T>::value() const&& {
754 if (!this->ok()) {
755 internal_statusor::ThrowBadStatusOrAccess(std::move(this->status_));
756 }
757 return std::move(this->data_);
758 }
759
760 template <typename T>
value()761 T&& StatusOr<T>::value() && {
762 if (!this->ok()) {
763 internal_statusor::ThrowBadStatusOrAccess(std::move(this->status_));
764 }
765 return std::move(this->data_);
766 }
767
768 template <typename T>
769 const T& StatusOr<T>::operator*() const& {
770 this->EnsureOk();
771 return this->data_;
772 }
773
774 template <typename T>
775 T& StatusOr<T>::operator*() & {
776 this->EnsureOk();
777 return this->data_;
778 }
779
780 template <typename T>
781 const T&& StatusOr<T>::operator*() const&& {
782 this->EnsureOk();
783 return std::move(this->data_);
784 }
785
786 template <typename T>
787 T&& StatusOr<T>::operator*() && {
788 this->EnsureOk();
789 return std::move(this->data_);
790 }
791
792 template <typename T>
793 absl::Nonnull<const T*> StatusOr<T>::operator->() const {
794 this->EnsureOk();
795 return &this->data_;
796 }
797
798 template <typename T>
799 absl::Nonnull<T*> StatusOr<T>::operator->() {
800 this->EnsureOk();
801 return &this->data_;
802 }
803
804 template <typename T>
805 template <typename U>
value_or(U && default_value)806 T StatusOr<T>::value_or(U&& default_value) const& {
807 if (ok()) {
808 return this->data_;
809 }
810 return std::forward<U>(default_value);
811 }
812
813 template <typename T>
814 template <typename U>
value_or(U && default_value)815 T StatusOr<T>::value_or(U&& default_value) && {
816 if (ok()) {
817 return std::move(this->data_);
818 }
819 return std::forward<U>(default_value);
820 }
821
822 template <typename T>
IgnoreError()823 void StatusOr<T>::IgnoreError() const {
824 // no-op
825 }
826
827 ABSL_NAMESPACE_END
828 } // namespace absl
829
830 #endif // ABSL_STATUS_STATUSOR_H_
831