// Copyright 2020 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // ----------------------------------------------------------------------------- // File: statusor.h // ----------------------------------------------------------------------------- // // An `absl::StatusOr` represents a union of an `absl::Status` object // and an object of type `T`. The `absl::StatusOr` will either contain an // object of type `T` (indicating a successful operation), or an error (of type // `absl::Status`) explaining why such a value is not present. // // In general, check the success of an operation returning an // `absl::StatusOr` like you would an `absl::Status` by using the `ok()` // member function. // // Example: // // StatusOr result = Calculation(); // if (result.ok()) { // result->DoSomethingCool(); // } else { // LOG(ERROR) << result.status(); // } #ifndef ABSL_STATUS_STATUSOR_H_ #define ABSL_STATUS_STATUSOR_H_ #include #include #include #include #include #include #include #include "absl/base/attributes.h" #include "absl/base/nullability.h" #include "absl/base/call_once.h" #include "absl/meta/type_traits.h" #include "absl/status/internal/statusor_internal.h" #include "absl/status/status.h" #include "absl/strings/has_absl_stringify.h" #include "absl/strings/has_ostream_operator.h" #include "absl/strings/str_format.h" #include "absl/types/variant.h" #include "absl/utility/utility.h" namespace absl { ABSL_NAMESPACE_BEGIN // BadStatusOrAccess // // This class defines the type of object to throw (if exceptions are enabled), // when accessing the value of an `absl::StatusOr` object that does not // contain a value. This behavior is analogous to that of // `std::bad_optional_access` in the case of accessing an invalid // `std::optional` value. // // Example: // // try { // absl::StatusOr v = FetchInt(); // DoWork(v.value()); // Accessing value() when not "OK" may throw // } catch (absl::BadStatusOrAccess& ex) { // LOG(ERROR) << ex.status(); // } class BadStatusOrAccess : public std::exception { public: explicit BadStatusOrAccess(absl::Status status); ~BadStatusOrAccess() override = default; BadStatusOrAccess(const BadStatusOrAccess& other); BadStatusOrAccess& operator=(const BadStatusOrAccess& other); BadStatusOrAccess(BadStatusOrAccess&& other); BadStatusOrAccess& operator=(BadStatusOrAccess&& other); // BadStatusOrAccess::what() // // Returns the associated explanatory string of the `absl::StatusOr` // object's error code. This function contains information about the failing // status, but its exact formatting may change and should not be depended on. // // The pointer of this string is guaranteed to be valid until any non-const // function is invoked on the exception object. absl::Nonnull what() const noexcept override; // BadStatusOrAccess::status() // // Returns the associated `absl::Status` of the `absl::StatusOr` object's // error. const absl::Status& status() const; private: void InitWhat() const; absl::Status status_; mutable absl::once_flag init_what_; mutable std::string what_; }; // Returned StatusOr objects may not be ignored. template #if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) // TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict // [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available. class [[nodiscard]] StatusOr; #else class ABSL_MUST_USE_RESULT StatusOr; #endif // ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) // absl::StatusOr // // The `absl::StatusOr` class template is a union of an `absl::Status` object // and an object of type `T`. The `absl::StatusOr` models an object that is // either a usable object, or an error (of type `absl::Status`) explaining why // such an object is not present. An `absl::StatusOr` is typically the return // value of a function which may fail. // // An `absl::StatusOr` can never hold an "OK" status (an // `absl::StatusCode::kOk` value); instead, the presence of an object of type // `T` indicates success. Instead of checking for a `kOk` value, use the // `absl::StatusOr::ok()` member function. (It is for this reason, and code // readability, that using the `ok()` function is preferred for `absl::Status` // as well.) // // Example: // // StatusOr result = DoBigCalculationThatCouldFail(); // if (result.ok()) { // result->DoSomethingCool(); // } else { // LOG(ERROR) << result.status(); // } // // Accessing the object held by an `absl::StatusOr` should be performed via // `operator*` or `operator->`, after a call to `ok()` confirms that the // `absl::StatusOr` holds an object of type `T`: // // Example: // // absl::StatusOr i = GetCount(); // if (i.ok()) { // updated_total += *i; // } // // NOTE: using `absl::StatusOr::value()` when no valid value is present will // throw an exception if exceptions are enabled or terminate the process when // exceptions are not enabled. // // Example: // // StatusOr result = DoBigCalculationThatCouldFail(); // const Foo& foo = result.value(); // Crash/exception if no value present // foo.DoSomethingCool(); // // A `absl::StatusOr` can be constructed from a null pointer like any other // pointer value, and the result will be that `ok()` returns `true` and // `value()` returns `nullptr`. Checking the value of pointer in an // `absl::StatusOr` generally requires a bit more care, to ensure both that // a value is present and that value is not null: // // StatusOr> result = FooFactory::MakeNewFoo(arg); // if (!result.ok()) { // LOG(ERROR) << result.status(); // } else if (*result == nullptr) { // LOG(ERROR) << "Unexpected null pointer"; // } else { // (*result)->DoSomethingCool(); // } // // Example factory implementation returning StatusOr: // // StatusOr FooFactory::MakeFoo(int arg) { // if (arg <= 0) { // return absl::Status(absl::StatusCode::kInvalidArgument, // "Arg must be positive"); // } // return Foo(arg); // } template class StatusOr : private internal_statusor::StatusOrData, private internal_statusor::CopyCtorBase, private internal_statusor::MoveCtorBase, private internal_statusor::CopyAssignBase, private internal_statusor::MoveAssignBase { template friend class StatusOr; typedef internal_statusor::StatusOrData Base; public: // StatusOr::value_type // // This instance data provides a generic `value_type` member for use within // generic programming. This usage is analogous to that of // `optional::value_type` in the case of `std::optional`. typedef T value_type; // Constructors // Constructs a new `absl::StatusOr` with an `absl::StatusCode::kUnknown` // status. This constructor is marked 'explicit' to prevent usages in return // values such as 'return {};', under the misconception that // `absl::StatusOr>` will be initialized with an empty // vector, instead of an `absl::StatusCode::kUnknown` error code. explicit StatusOr(); // `StatusOr` is copy constructible if `T` is copy constructible. StatusOr(const StatusOr&) = default; // `StatusOr` is copy assignable if `T` is copy constructible and copy // assignable. StatusOr& operator=(const StatusOr&) = default; // `StatusOr` is move constructible if `T` is move constructible. StatusOr(StatusOr&&) = default; // `StatusOr` is moveAssignable if `T` is move constructible and move // assignable. StatusOr& operator=(StatusOr&&) = default; // Converting Constructors // Constructs a new `absl::StatusOr` from an `absl::StatusOr`, when `T` // is constructible from `U`. To avoid ambiguity, these constructors are // disabled if `T` is also constructible from `StatusOr.`. This constructor // is explicit if and only if the corresponding construction of `T` from `U` // is explicit. (This constructor inherits its explicitness from the // underlying constructor.) template ::value, int> = 0> StatusOr(const StatusOr& other) // NOLINT : Base(static_cast::Base&>(other)) {} template ::value, int> = 0> StatusOr(const StatusOr& other ABSL_ATTRIBUTE_LIFETIME_BOUND) // NOLINT : Base(static_cast::Base&>(other)) {} template ::value, int> = 0> explicit StatusOr(const StatusOr& other) : Base(static_cast::Base&>(other)) {} template ::value, int> = 0> explicit StatusOr(const StatusOr& other ABSL_ATTRIBUTE_LIFETIME_BOUND) : Base(static_cast::Base&>(other)) {} template ::value, int> = 0> StatusOr(StatusOr&& other) // NOLINT : Base(static_cast::Base&&>(other)) {} template ::value, int> = 0> StatusOr(StatusOr&& other ABSL_ATTRIBUTE_LIFETIME_BOUND) // NOLINT : Base(static_cast::Base&&>(other)) {} template ::value, int> = 0> explicit StatusOr(StatusOr&& other) : Base(static_cast::Base&&>(other)) {} template ::value, int> = 0> explicit StatusOr(StatusOr&& other ABSL_ATTRIBUTE_LIFETIME_BOUND) : Base(static_cast::Base&&>(other)) {} // Converting Assignment Operators // Creates an `absl::StatusOr` through assignment from an // `absl::StatusOr` when: // // * Both `absl::StatusOr` and `absl::StatusOr` are OK by assigning // `U` to `T` directly. // * `absl::StatusOr` is OK and `absl::StatusOr` contains an error // code by destroying `absl::StatusOr`'s value and assigning from // `absl::StatusOr' // * `absl::StatusOr` contains an error code and `absl::StatusOr` is // OK by directly initializing `T` from `U`. // * Both `absl::StatusOr` and `absl::StatusOr` contain an error // code by assigning the `Status` in `absl::StatusOr` to // `absl::StatusOr` // // These overloads only apply if `absl::StatusOr` is constructible and // assignable from `absl::StatusOr` and `StatusOr` cannot be directly // assigned from `StatusOr`. template ::value, int> = 0> StatusOr& operator=(const StatusOr& other) { this->Assign(other); return *this; } template ::value, int> = 0> StatusOr& operator=(const StatusOr& other ABSL_ATTRIBUTE_LIFETIME_BOUND) { this->Assign(other); return *this; } template ::value, int> = 0> StatusOr& operator=(StatusOr&& other) { this->Assign(std::move(other)); return *this; } template ::value, int> = 0> StatusOr& operator=(StatusOr&& other ABSL_ATTRIBUTE_LIFETIME_BOUND) { this->Assign(std::move(other)); return *this; } // Constructs a new `absl::StatusOr` with a non-ok status. After calling // this constructor, `this->ok()` will be `false` and calls to `value()` will // crash, or produce an exception if exceptions are enabled. // // The constructor also takes any type `U` that is convertible to // `absl::Status`. This constructor is explicit if an only if `U` is not of // type `absl::Status` and the conversion from `U` to `Status` is explicit. // // REQUIRES: !Status(std::forward(v)).ok(). This requirement is DCHECKed. // In optimized builds, passing absl::OkStatus() here will have the effect // of passing absl::StatusCode::kInternal as a fallback. template ::value, int> = 0> StatusOr(U&& v) : Base(std::forward(v)) {} template ::value, int> = 0> explicit StatusOr(U&& v) : Base(std::forward(v)) {} template ::value, int> = 0> StatusOr& operator=(U&& v) { this->AssignStatus(std::forward(v)); return *this; } // Perfect-forwarding value assignment operator. // If `*this` contains a `T` value before the call, the contained value is // assigned from `std::forward(v)`; Otherwise, it is directly-initialized // from `std::forward(v)`. // This function does not participate in overload unless: // 1. `std::is_constructible_v` is true, // 2. `std::is_assignable_v` is true. // 3. `std::is_same_v, std::remove_cvref_t>` is false. // 4. Assigning `U` to `T` is not ambiguous: // If `U` is `StatusOr` and `T` is constructible and assignable from // both `StatusOr` and `V`, the assignment is considered bug-prone and // ambiguous thus will fail to compile. For example: // StatusOr s1 = true; // s1.ok() && *s1 == true // StatusOr s2 = false; // s2.ok() && *s2 == false // s1 = s2; // ambiguous, `s1 = *s2` or `s1 = bool(s2)`? template ::value, int>::type = 0> StatusOr& operator=(U&& v) { this->Assign(std::forward(v)); return *this; } template ::value, int>::type = 0> StatusOr& operator=(U&& v ABSL_ATTRIBUTE_LIFETIME_BOUND) { this->Assign(std::forward(v)); return *this; } // Constructs the inner value `T` in-place using the provided args, using the // `T(args...)` constructor. template explicit StatusOr(absl::in_place_t, Args&&... args); template explicit StatusOr(absl::in_place_t, std::initializer_list ilist, Args&&... args); // Constructs the inner value `T` in-place using the provided args, using the // `T(U)` (direct-initialization) constructor. This constructor is only valid // if `T` can be constructed from a `U`. Can accept move or copy constructors. // // This constructor is explicit if `U` is not convertible to `T`. To avoid // ambiguity, this constructor is disabled if `U` is a `StatusOr`, where // `J` is convertible to `T`. template ::value, int> = 0> StatusOr(U&& u) // NOLINT : StatusOr(absl::in_place, std::forward(u)) {} template ::value, int> = 0> StatusOr(U&& u ABSL_ATTRIBUTE_LIFETIME_BOUND) // NOLINT : StatusOr(absl::in_place, std::forward(u)) {} template ::value, int> = 0> explicit StatusOr(U&& u) // NOLINT : StatusOr(absl::in_place, std::forward(u)) {} template ::value, int> = 0> explicit StatusOr(U&& u ABSL_ATTRIBUTE_LIFETIME_BOUND) // NOLINT : StatusOr(absl::in_place, std::forward(u)) {} // StatusOr::ok() // // Returns whether or not this `absl::StatusOr` holds a `T` value. This // member function is analogous to `absl::Status::ok()` and should be used // similarly to check the status of return values. // // Example: // // StatusOr result = DoBigCalculationThatCouldFail(); // if (result.ok()) { // // Handle result // else { // // Handle error // } ABSL_MUST_USE_RESULT bool ok() const { return this->status_.ok(); } // StatusOr::status() // // Returns a reference to the current `absl::Status` contained within the // `absl::StatusOr`. If `absl::StatusOr` contains a `T`, then this // function returns `absl::OkStatus()`. const Status& status() const&; Status status() &&; // StatusOr::value() // // Returns a reference to the held value if `this->ok()`. Otherwise, throws // `absl::BadStatusOrAccess` if exceptions are enabled, or is guaranteed to // terminate the process if exceptions are disabled. // // If you have already checked the status using `this->ok()`, you probably // want to use `operator*()` or `operator->()` to access the value instead of // `value`. // // Note: for value types that are cheap to copy, prefer simple code: // // T value = statusor.value(); // // Otherwise, if the value type is expensive to copy, but can be left // in the StatusOr, simply assign to a reference: // // T& value = statusor.value(); // or `const T&` // // Otherwise, if the value type supports an efficient move, it can be // used as follows: // // T value = std::move(statusor).value(); // // The `std::move` on statusor instead of on the whole expression enables // warnings about possible uses of the statusor object after the move. const T& value() const& ABSL_ATTRIBUTE_LIFETIME_BOUND; T& value() & ABSL_ATTRIBUTE_LIFETIME_BOUND; const T&& value() const&& ABSL_ATTRIBUTE_LIFETIME_BOUND; T&& value() && ABSL_ATTRIBUTE_LIFETIME_BOUND; // StatusOr:: operator*() // // Returns a reference to the current value. // // REQUIRES: `this->ok() == true`, otherwise the behavior is undefined. // // Use `this->ok()` to verify that there is a current value within the // `absl::StatusOr`. Alternatively, see the `value()` member function for a // similar API that guarantees crashing or throwing an exception if there is // no current value. const T& operator*() const& ABSL_ATTRIBUTE_LIFETIME_BOUND; T& operator*() & ABSL_ATTRIBUTE_LIFETIME_BOUND; const T&& operator*() const&& ABSL_ATTRIBUTE_LIFETIME_BOUND; T&& operator*() && ABSL_ATTRIBUTE_LIFETIME_BOUND; // StatusOr::operator->() // // Returns a pointer to the current value. // // REQUIRES: `this->ok() == true`, otherwise the behavior is undefined. // // Use `this->ok()` to verify that there is a current value. const T* operator->() const ABSL_ATTRIBUTE_LIFETIME_BOUND; T* operator->() ABSL_ATTRIBUTE_LIFETIME_BOUND; // StatusOr::value_or() // // Returns the current value if `this->ok() == true`. Otherwise constructs a // value using the provided `default_value`. // // Unlike `value`, this function returns by value, copying the current value // if necessary. If the value type supports an efficient move, it can be used // as follows: // // T value = std::move(statusor).value_or(def); // // Unlike with `value`, calling `std::move()` on the result of `value_or` will // still trigger a copy. template T value_or(U&& default_value) const&; template T value_or(U&& default_value) &&; // StatusOr::IgnoreError() // // Ignores any errors. This method does nothing except potentially suppress // complaints from any tools that are checking that errors are not dropped on // the floor. void IgnoreError() const; // StatusOr::emplace() // // Reconstructs the inner value T in-place using the provided args, using the // T(args...) constructor. Returns reference to the reconstructed `T`. template T& emplace(Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { if (ok()) { this->Clear(); this->MakeValue(std::forward(args)...); } else { this->MakeValue(std::forward(args)...); this->status_ = absl::OkStatus(); } return this->data_; } template < typename U, typename... Args, absl::enable_if_t< std::is_constructible&, Args&&...>::value, int> = 0> T& emplace(std::initializer_list ilist, Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND { if (ok()) { this->Clear(); this->MakeValue(ilist, std::forward(args)...); } else { this->MakeValue(ilist, std::forward(args)...); this->status_ = absl::OkStatus(); } return this->data_; } // StatusOr::AssignStatus() // // Sets the status of `absl::StatusOr` to the given non-ok status value. // // NOTE: We recommend using the constructor and `operator=` where possible. // This method is intended for use in generic programming, to enable setting // the status of a `StatusOr` when `T` may be `Status`. In that case, the // constructor and `operator=` would assign into the inner value of type // `Status`, rather than status of the `StatusOr` (b/280392796). // // REQUIRES: !Status(std::forward(v)).ok(). This requirement is DCHECKed. // In optimized builds, passing absl::OkStatus() here will have the effect // of passing absl::StatusCode::kInternal as a fallback. using internal_statusor::StatusOrData::AssignStatus; private: using internal_statusor::StatusOrData::Assign; template void Assign(const absl::StatusOr& other); template void Assign(absl::StatusOr&& other); }; // operator==() // // This operator checks the equality of two `absl::StatusOr` objects. template bool operator==(const StatusOr& lhs, const StatusOr& rhs) { if (lhs.ok() && rhs.ok()) return *lhs == *rhs; return lhs.status() == rhs.status(); } // operator!=() // // This operator checks the inequality of two `absl::StatusOr` objects. template bool operator!=(const StatusOr& lhs, const StatusOr& rhs) { return !(lhs == rhs); } // Prints the `value` or the status in brackets to `os`. // // Requires `T` supports `operator<<`. Do not rely on the output format which // may change without notice. template ::value, int>::type = 0> std::ostream& operator<<(std::ostream& os, const StatusOr& status_or) { if (status_or.ok()) { os << status_or.value(); } else { os << internal_statusor::StringifyRandom::OpenBrackets() << status_or.status() << internal_statusor::StringifyRandom::CloseBrackets(); } return os; } // As above, but supports `StrCat`, `StrFormat`, etc. // // Requires `T` has `AbslStringify`. Do not rely on the output format which // may change without notice. template < typename Sink, typename T, typename std::enable_if::value, int>::type = 0> void AbslStringify(Sink& sink, const StatusOr& status_or) { if (status_or.ok()) { absl::Format(&sink, "%v", status_or.value()); } else { absl::Format(&sink, "%s%v%s", internal_statusor::StringifyRandom::OpenBrackets(), status_or.status(), internal_statusor::StringifyRandom::CloseBrackets()); } } //------------------------------------------------------------------------------ // Implementation details for StatusOr //------------------------------------------------------------------------------ // TODO(sbenza): avoid the string here completely. template StatusOr::StatusOr() : Base(Status(absl::StatusCode::kUnknown, "")) {} template template inline void StatusOr::Assign(const StatusOr& other) { if (other.ok()) { this->Assign(*other); } else { this->AssignStatus(other.status()); } } template template inline void StatusOr::Assign(StatusOr&& other) { if (other.ok()) { this->Assign(*std::move(other)); } else { this->AssignStatus(std::move(other).status()); } } template template StatusOr::StatusOr(absl::in_place_t, Args&&... args) : Base(absl::in_place, std::forward(args)...) {} template template StatusOr::StatusOr(absl::in_place_t, std::initializer_list ilist, Args&&... args) : Base(absl::in_place, ilist, std::forward(args)...) {} template const Status& StatusOr::status() const& { return this->status_; } template Status StatusOr::status() && { return ok() ? OkStatus() : std::move(this->status_); } template const T& StatusOr::value() const& { if (!this->ok()) internal_statusor::ThrowBadStatusOrAccess(this->status_); return this->data_; } template T& StatusOr::value() & { if (!this->ok()) internal_statusor::ThrowBadStatusOrAccess(this->status_); return this->data_; } template const T&& StatusOr::value() const&& { if (!this->ok()) { internal_statusor::ThrowBadStatusOrAccess(std::move(this->status_)); } return std::move(this->data_); } template T&& StatusOr::value() && { if (!this->ok()) { internal_statusor::ThrowBadStatusOrAccess(std::move(this->status_)); } return std::move(this->data_); } template const T& StatusOr::operator*() const& { this->EnsureOk(); return this->data_; } template T& StatusOr::operator*() & { this->EnsureOk(); return this->data_; } template const T&& StatusOr::operator*() const&& { this->EnsureOk(); return std::move(this->data_); } template T&& StatusOr::operator*() && { this->EnsureOk(); return std::move(this->data_); } template absl::Nonnull StatusOr::operator->() const { this->EnsureOk(); return &this->data_; } template absl::Nonnull StatusOr::operator->() { this->EnsureOk(); return &this->data_; } template template T StatusOr::value_or(U&& default_value) const& { if (ok()) { return this->data_; } return std::forward(default_value); } template template T StatusOr::value_or(U&& default_value) && { if (ok()) { return std::move(this->data_); } return std::forward(default_value); } template void StatusOr::IgnoreError() const { // no-op } ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STATUS_STATUSOR_H_