1 | // Copyright 2017 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 | #include "absl/types/bad_variant_access.h" |
16 | |
17 | #ifndef ABSL_USES_STD_VARIANT |
18 | |
19 | #include <cstdlib> |
20 | #include <stdexcept> |
21 | |
22 | #include "absl/base/config.h" |
23 | #include "absl/base/internal/raw_logging.h" |
24 | |
25 | namespace absl { |
26 | ABSL_NAMESPACE_BEGIN |
27 | |
28 | ////////////////////////// |
29 | // [variant.bad.access] // |
30 | ////////////////////////// |
31 | |
32 | bad_variant_access::~bad_variant_access() = default; |
33 | |
34 | const char* bad_variant_access::what() const noexcept { |
35 | return "Bad variant access" ; |
36 | } |
37 | |
38 | namespace variant_internal { |
39 | |
40 | void ThrowBadVariantAccess() { |
41 | #ifdef ABSL_HAVE_EXCEPTIONS |
42 | throw bad_variant_access(); |
43 | #else |
44 | ABSL_RAW_LOG(FATAL, "Bad variant access" ); |
45 | abort(); // TODO(calabrese) Remove once RAW_LOG FATAL is noreturn. |
46 | #endif |
47 | } |
48 | |
49 | void Rethrow() { |
50 | #ifdef ABSL_HAVE_EXCEPTIONS |
51 | throw; |
52 | #else |
53 | ABSL_RAW_LOG(FATAL, |
54 | "Internal error in absl::variant implementation. Attempted to " |
55 | "rethrow an exception when building with exceptions disabled." ); |
56 | abort(); // TODO(calabrese) Remove once RAW_LOG FATAL is noreturn. |
57 | #endif |
58 | } |
59 | |
60 | } // namespace variant_internal |
61 | ABSL_NAMESPACE_END |
62 | } // namespace absl |
63 | |
64 | #endif // ABSL_USES_STD_VARIANT |
65 | |