// 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. #ifndef ABSL_STATUS_INTERNAL_STATUSOR_INTERNAL_H_ #define ABSL_STATUS_INTERNAL_STATUSOR_INTERNAL_H_ #include #include #include "absl/meta/type_traits.h" #include "absl/status/status.h" #include "absl/utility/utility.h" namespace absl { ABSL_NAMESPACE_BEGIN template class ABSL_MUST_USE_RESULT StatusOr; namespace internal_statusor { // Detects whether `U` has conversion operator to `StatusOr`, i.e. `operator // StatusOr()`. template struct HasConversionOperatorToStatusOr : std::false_type {}; template void test(char (*)[sizeof(std::declval().operator absl::StatusOr())]); template struct HasConversionOperatorToStatusOr(0))> : std::true_type {}; // Detects whether `T` is constructible or convertible from `StatusOr`. template using IsConstructibleOrConvertibleFromStatusOr = absl::disjunction&>, std::is_constructible&>, std::is_constructible&&>, std::is_constructible&&>, std::is_convertible&, T>, std::is_convertible&, T>, std::is_convertible&&, T>, std::is_convertible&&, T>>; // Detects whether `T` is constructible or convertible or assignable from // `StatusOr`. template using IsConstructibleOrConvertibleOrAssignableFromStatusOr = absl::disjunction, std::is_assignable&>, std::is_assignable&>, std::is_assignable&&>, std::is_assignable&&>>; // Detects whether direct initializing `StatusOr` from `U` is ambiguous, i.e. // when `U` is `StatusOr` and `T` is constructible or convertible from `V`. template struct IsDirectInitializationAmbiguous : public absl::conditional_t< std::is_same>, U>::value, std::false_type, IsDirectInitializationAmbiguous< T, absl::remove_cv_t>>> {}; template struct IsDirectInitializationAmbiguous> : public IsConstructibleOrConvertibleFromStatusOr {}; // Checks against the constraints of the direction initialization, i.e. when // `StatusOr::StatusOr(U&&)` should participate in overload resolution. template using IsDirectInitializationValid = absl::disjunction< // Short circuits if T is basically U. std::is_same>>, absl::negation, absl::remove_cv_t>>, std::is_same>>, std::is_same>>, IsDirectInitializationAmbiguous>>>; // This trait detects whether `StatusOr::operator=(U&&)` is ambiguous, which // is equivalent to whether all the following conditions are met: // 1. `U` is `StatusOr`. // 2. `T` is constructible and assignable from `V`. // 3. `T` is constructible and assignable from `U` (i.e. `StatusOr`). // For example, the following code is considered ambiguous: // (`T` is `bool`, `U` is `StatusOr`, `V` is `bool`) // StatusOr s1 = true; // s1.ok() && s1.ValueOrDie() == true // StatusOr s2 = false; // s2.ok() && s2.ValueOrDie() == false // s1 = s2; // ambiguous, `s1 = s2.ValueOrDie()` or `s1 = bool(s2)`? template struct IsForwardingAssignmentAmbiguous : public absl::conditional_t< std::is_same>, U>::value, std::false_type, IsForwardingAssignmentAmbiguous< T, absl::remove_cv_t>>> {}; template struct IsForwardingAssignmentAmbiguous> : public IsConstructibleOrConvertibleOrAssignableFromStatusOr {}; // Checks against the constraints of the forwarding assignment, i.e. whether // `StatusOr::operator(U&&)` should participate in overload resolution. template using IsForwardingAssignmentValid = absl::disjunction< // Short circuits if T is basically U. std::is_same>>, absl::negation, absl::remove_cv_t>>, std::is_same>>, std::is_same>>, IsForwardingAssignmentAmbiguous>>>; class Helper { public: // Move type-agnostic error handling to the .cc. static void HandleInvalidStatusCtorArg(Status*); static void Crash(const absl::Status& status); }; // Construct an instance of T in `p` through placement new, passing Args... to // the constructor. // This abstraction is here mostly for the gcc performance fix. template void PlacementNew(void* p, Args&&... args) { #if defined(__GNUC__) && !defined(__clang__) // Teach gcc that 'p' cannot be null, fixing code size issues. if (p == nullptr) __builtin_unreachable(); #endif new (p) T(std::forward(args)...); } // Helper base class to hold the data and all operations. // We move all this to a base class to allow mixing with the appropriate // TraitsBase specialization. template class StatusOrData { template friend class StatusOrData; public: StatusOrData() = delete; StatusOrData(const StatusOrData& other) { if (other.ok()) { MakeValue(other.data_); MakeStatus(); } else { MakeStatus(other.status_); } } StatusOrData(StatusOrData&& other) noexcept { if (other.ok()) { MakeValue(std::move(other.data_)); MakeStatus(); } else { MakeStatus(std::move(other.status_)); } } template explicit StatusOrData(const StatusOrData& other) { if (other.ok()) { MakeValue(other.data_); MakeStatus(); } else { MakeStatus(other.status_); } } template explicit StatusOrData(StatusOrData&& other) { if (other.ok()) { MakeValue(std::move(other.data_)); MakeStatus(); } else { MakeStatus(std::move(other.status_)); } } template explicit StatusOrData(absl::in_place_t, Args&&... args) : data_(std::forward(args)...) { MakeStatus(); } explicit StatusOrData(const T& value) : data_(value) { MakeStatus(); } explicit StatusOrData(T&& value) : data_(std::move(value)) { MakeStatus(); } template ::value, int> = 0> explicit StatusOrData(U&& v) : status_(std::forward(v)) { EnsureNotOk(); } StatusOrData& operator=(const StatusOrData& other) { if (this == &other) return *this; if (other.ok()) Assign(other.data_); else AssignStatus(other.status_); return *this; } StatusOrData& operator=(StatusOrData&& other) { if (this == &other) return *this; if (other.ok()) Assign(std::move(other.data_)); else AssignStatus(std::move(other.status_)); return *this; } ~StatusOrData() { if (ok()) { status_.~Status(); data_.~T(); } else { status_.~Status(); } } template void Assign(U&& value) { if (ok()) { data_ = std::forward(value); } else { MakeValue(std::forward(value)); status_ = OkStatus(); } } template void AssignStatus(U&& v) { Clear(); status_ = static_cast(std::forward(v)); EnsureNotOk(); } bool ok() const { return status_.ok(); } protected: // status_ will always be active after the constructor. // We make it a union to be able to initialize exactly how we need without // waste. // Eg. in the copy constructor we use the default constructor of Status in // the ok() path to avoid an extra Ref call. union { Status status_; }; // data_ is active iff status_.ok()==true struct Dummy {}; union { // When T is const, we need some non-const object we can cast to void* for // the placement new. dummy_ is that object. Dummy dummy_; T data_; }; void Clear() { if (ok()) data_.~T(); } void EnsureOk() const { if (ABSL_PREDICT_FALSE(!ok())) Helper::Crash(status_); } void EnsureNotOk() { if (ABSL_PREDICT_FALSE(ok())) Helper::HandleInvalidStatusCtorArg(&status_); } // Construct the value (ie. data_) through placement new with the passed // argument. template void MakeValue(Arg&&... arg) { internal_statusor::PlacementNew(&dummy_, std::forward(arg)...); } // Construct the status (ie. status_) through placement new with the passed // argument. template void MakeStatus(Args&&... args) { internal_statusor::PlacementNew(&status_, std::forward(args)...); } }; // Helper base classes to allow implicitly deleted constructors and assignment // operators in `StatusOr`. For example, `CopyCtorBase` will explicitly delete // the copy constructor when T is not copy constructible and `StatusOr` will // inherit that behavior implicitly. template ::value> struct CopyCtorBase { CopyCtorBase() = default; CopyCtorBase(const CopyCtorBase&) = default; CopyCtorBase(CopyCtorBase&&) = default; CopyCtorBase& operator=(const CopyCtorBase&) = default; CopyCtorBase& operator=(CopyCtorBase&&) = default; }; template struct CopyCtorBase { CopyCtorBase() = default; CopyCtorBase(const CopyCtorBase&) = delete; CopyCtorBase(CopyCtorBase&&) = default; CopyCtorBase& operator=(const CopyCtorBase&) = default; CopyCtorBase& operator=(CopyCtorBase&&) = default; }; template ::value> struct MoveCtorBase { MoveCtorBase() = default; MoveCtorBase(const MoveCtorBase&) = default; MoveCtorBase(MoveCtorBase&&) = default; MoveCtorBase& operator=(const MoveCtorBase&) = default; MoveCtorBase& operator=(MoveCtorBase&&) = default; }; template struct MoveCtorBase { MoveCtorBase() = default; MoveCtorBase(const MoveCtorBase&) = default; MoveCtorBase(MoveCtorBase&&) = delete; MoveCtorBase& operator=(const MoveCtorBase&) = default; MoveCtorBase& operator=(MoveCtorBase&&) = default; }; template ::value&& std::is_copy_assignable::value> struct CopyAssignBase { CopyAssignBase() = default; CopyAssignBase(const CopyAssignBase&) = default; CopyAssignBase(CopyAssignBase&&) = default; CopyAssignBase& operator=(const CopyAssignBase&) = default; CopyAssignBase& operator=(CopyAssignBase&&) = default; }; template struct CopyAssignBase { CopyAssignBase() = default; CopyAssignBase(const CopyAssignBase&) = default; CopyAssignBase(CopyAssignBase&&) = default; CopyAssignBase& operator=(const CopyAssignBase&) = delete; CopyAssignBase& operator=(CopyAssignBase&&) = default; }; template ::value&& std::is_move_assignable::value> struct MoveAssignBase { MoveAssignBase() = default; MoveAssignBase(const MoveAssignBase&) = default; MoveAssignBase(MoveAssignBase&&) = default; MoveAssignBase& operator=(const MoveAssignBase&) = default; MoveAssignBase& operator=(MoveAssignBase&&) = default; }; template struct MoveAssignBase { MoveAssignBase() = default; MoveAssignBase(const MoveAssignBase&) = default; MoveAssignBase(MoveAssignBase&&) = default; MoveAssignBase& operator=(const MoveAssignBase&) = default; MoveAssignBase& operator=(MoveAssignBase&&) = delete; }; ABSL_ATTRIBUTE_NORETURN void ThrowBadStatusOrAccess(absl::Status status); } // namespace internal_statusor ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STATUS_INTERNAL_STATUSOR_INTERNAL_H_