statusor.cc (3201B)
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 #include "absl/status/statusor.h" 15 16 #include <cstdlib> 17 #include <utility> 18 19 #include "absl/base/call_once.h" 20 #include "absl/base/config.h" 21 #include "absl/base/internal/raw_logging.h" 22 #include "absl/base/nullability.h" 23 #include "absl/status/internal/statusor_internal.h" 24 #include "absl/status/status.h" 25 #include "absl/strings/str_cat.h" 26 27 namespace absl { 28 ABSL_NAMESPACE_BEGIN 29 30 BadStatusOrAccess::BadStatusOrAccess(absl::Status status) 31 : status_(std::move(status)) {} 32 33 BadStatusOrAccess::BadStatusOrAccess(const BadStatusOrAccess& other) 34 : status_(other.status_) {} 35 36 BadStatusOrAccess& BadStatusOrAccess::operator=( 37 const BadStatusOrAccess& other) { 38 // Ensure assignment is correct regardless of whether this->InitWhat() has 39 // already been called. 40 other.InitWhat(); 41 status_ = other.status_; 42 what_ = other.what_; 43 return *this; 44 } 45 46 BadStatusOrAccess& BadStatusOrAccess::operator=(BadStatusOrAccess&& other) { 47 // Ensure assignment is correct regardless of whether this->InitWhat() has 48 // already been called. 49 other.InitWhat(); 50 status_ = std::move(other.status_); 51 what_ = std::move(other.what_); 52 return *this; 53 } 54 55 BadStatusOrAccess::BadStatusOrAccess(BadStatusOrAccess&& other) 56 : status_(std::move(other.status_)) {} 57 58 absl::Nonnull<const char*> BadStatusOrAccess::what() const noexcept { 59 InitWhat(); 60 return what_.c_str(); 61 } 62 63 const absl::Status& BadStatusOrAccess::status() const { return status_; } 64 65 void BadStatusOrAccess::InitWhat() const { 66 absl::call_once(init_what_, [this] { 67 what_ = absl::StrCat("Bad StatusOr access: ", status_.ToString()); 68 }); 69 } 70 71 namespace internal_statusor { 72 73 void Helper::HandleInvalidStatusCtorArg(absl::Nonnull<absl::Status*> status) { 74 const char* kMessage = 75 "An OK status is not a valid constructor argument to StatusOr<T>"; 76 #ifdef NDEBUG 77 ABSL_INTERNAL_LOG(ERROR, kMessage); 78 #else 79 ABSL_INTERNAL_LOG(FATAL, kMessage); 80 #endif 81 // In optimized builds, we will fall back to InternalError. 82 *status = absl::InternalError(kMessage); 83 } 84 85 void Helper::Crash(const absl::Status& status) { 86 ABSL_INTERNAL_LOG( 87 FATAL, 88 absl::StrCat("Attempting to fetch value instead of handling error ", 89 status.ToString())); 90 } 91 92 void ThrowBadStatusOrAccess(absl::Status status) { 93 #ifdef ABSL_HAVE_EXCEPTIONS 94 throw absl::BadStatusOrAccess(std::move(status)); 95 #else 96 ABSL_INTERNAL_LOG( 97 FATAL, 98 absl::StrCat("Attempting to fetch value instead of handling error ", 99 status.ToString())); 100 std::abort(); 101 #endif 102 } 103 104 } // namespace internal_statusor 105 ABSL_NAMESPACE_END 106 } // namespace absl