/src/abseil-cpp/absl/status/statusor.cc
Line | Count | Source |
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 | 0 | : status_(std::move(status)) {} |
32 | | |
33 | | BadStatusOrAccess::BadStatusOrAccess(const BadStatusOrAccess& other) |
34 | 0 | : status_(other.status_) {} |
35 | | |
36 | | BadStatusOrAccess& BadStatusOrAccess::operator=( |
37 | 0 | const BadStatusOrAccess& other) { |
38 | | // Ensure assignment is correct regardless of whether this->InitWhat() has |
39 | | // already been called. |
40 | 0 | other.InitWhat(); |
41 | 0 | status_ = other.status_; |
42 | 0 | what_ = other.what_; |
43 | 0 | return *this; |
44 | 0 | } |
45 | | |
46 | 0 | BadStatusOrAccess& BadStatusOrAccess::operator=(BadStatusOrAccess&& other) { |
47 | | // Ensure assignment is correct regardless of whether this->InitWhat() has |
48 | | // already been called. |
49 | 0 | other.InitWhat(); |
50 | 0 | status_ = std::move(other.status_); |
51 | 0 | what_ = std::move(other.what_); |
52 | 0 | return *this; |
53 | 0 | } |
54 | | |
55 | | BadStatusOrAccess::BadStatusOrAccess(BadStatusOrAccess&& other) |
56 | 0 | : status_(std::move(other.status_)) {} |
57 | | |
58 | 0 | const char* absl_nonnull BadStatusOrAccess::what() const noexcept { |
59 | 0 | InitWhat(); |
60 | 0 | return what_.c_str(); |
61 | 0 | } |
62 | | |
63 | 0 | const absl::Status& BadStatusOrAccess::status() const { return status_; } |
64 | | |
65 | 0 | void BadStatusOrAccess::InitWhat() const { |
66 | 0 | absl::call_once(init_what_, [this] { |
67 | 0 | what_ = absl::StrCat("Bad StatusOr access: ", status_.ToString()); |
68 | 0 | }); |
69 | 0 | } |
70 | | |
71 | | namespace internal_statusor { |
72 | | |
73 | 0 | void Helper::HandleInvalidStatusCtorArg(absl::Status* absl_nonnull status) { |
74 | 0 | const char* kMessage = |
75 | 0 | "An OK status is not a valid constructor argument to StatusOr<T>"; |
76 | | #ifdef NDEBUG |
77 | | ABSL_INTERNAL_LOG(ERROR, kMessage); |
78 | | #else |
79 | 0 | ABSL_INTERNAL_LOG(FATAL, kMessage); |
80 | 0 | #endif |
81 | | // In optimized builds, we will fall back to InternalError. |
82 | 0 | *status = absl::InternalError(kMessage); |
83 | 0 | } |
84 | | |
85 | 0 | void Helper::Crash(const absl::Status& status) { |
86 | 0 | ABSL_INTERNAL_LOG( |
87 | 0 | FATAL, |
88 | 0 | absl::StrCat("Attempting to fetch value instead of handling error ", |
89 | 0 | status.ToString())); |
90 | 0 | } |
91 | | |
92 | 0 | void ThrowBadStatusOrAccess(absl::Status status) { |
93 | 0 | #ifdef ABSL_HAVE_EXCEPTIONS |
94 | 0 | 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 | 0 | } |
103 | | |
104 | | } // namespace internal_statusor |
105 | | ABSL_NAMESPACE_END |
106 | | } // namespace absl |