1 | // Copyright 2018 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 | // ----------------------------------------------------------------------------- |
16 | // bad_variant_access.h |
17 | // ----------------------------------------------------------------------------- |
18 | // |
19 | // This header file defines the `absl::bad_variant_access` type. |
20 | |
21 | #ifndef ABSL_TYPES_BAD_VARIANT_ACCESS_H_ |
22 | #define ABSL_TYPES_BAD_VARIANT_ACCESS_H_ |
23 | |
24 | #include <stdexcept> |
25 | |
26 | #include "absl/base/config.h" |
27 | |
28 | #ifdef ABSL_HAVE_STD_VARIANT |
29 | |
30 | #include <variant> |
31 | |
32 | namespace absl { |
33 | using std::bad_variant_access; |
34 | } // namespace absl |
35 | |
36 | #else // ABSL_HAVE_STD_VARIANT |
37 | |
38 | namespace absl { |
39 | |
40 | // ----------------------------------------------------------------------------- |
41 | // bad_variant_access |
42 | // ----------------------------------------------------------------------------- |
43 | // |
44 | // An `absl::bad_variant_access` type is an exception type that is thrown in |
45 | // the following cases: |
46 | // |
47 | // * Calling `absl::get(absl::variant) with an index or type that does not |
48 | // match the currently selected alternative type |
49 | // * Calling `absl::visit on an `absl::variant` that is in the |
50 | // `variant::valueless_by_exception` state. |
51 | // |
52 | // Example: |
53 | // |
54 | // absl::variant<int, std::string> v; |
55 | // v = 1; |
56 | // try { |
57 | // absl::get<std::string>(v); |
58 | // } catch(const absl::bad_variant_access& e) { |
59 | // std::cout << "Bad variant access: " << e.what() << '\n'; |
60 | // } |
61 | class bad_variant_access : public std::exception { |
62 | public: |
63 | bad_variant_access() noexcept = default; |
64 | ~bad_variant_access() override; |
65 | const char* what() const noexcept override; |
66 | }; |
67 | |
68 | namespace variant_internal { |
69 | |
70 | [[noreturn]] void ThrowBadVariantAccess(); |
71 | [[noreturn]] void Rethrow(); |
72 | |
73 | } // namespace variant_internal |
74 | } // namespace absl |
75 | |
76 | #endif // ABSL_HAVE_STD_VARIANT |
77 | |
78 | #endif // ABSL_TYPES_BAD_VARIANT_ACCESS_H_ |
79 | |