| 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_any_cast.h |
| 17 | // ----------------------------------------------------------------------------- |
| 18 | // |
| 19 | // This header file defines the `absl::bad_any_cast` type. |
| 20 | |
| 21 | #ifndef ABSL_TYPES_BAD_ANY_CAST_H_ |
| 22 | #define ABSL_TYPES_BAD_ANY_CAST_H_ |
| 23 | |
| 24 | #include <typeinfo> |
| 25 | |
| 26 | #include "absl/base/config.h" |
| 27 | |
| 28 | #ifdef ABSL_HAVE_STD_ANY |
| 29 | |
| 30 | #include <any> |
| 31 | |
| 32 | namespace absl { |
| 33 | using std::bad_any_cast; |
| 34 | } // namespace absl |
| 35 | |
| 36 | #else // ABSL_HAVE_STD_ANY |
| 37 | |
| 38 | namespace absl { |
| 39 | |
| 40 | // ----------------------------------------------------------------------------- |
| 41 | // bad_any_cast |
| 42 | // ----------------------------------------------------------------------------- |
| 43 | // |
| 44 | // An `absl::bad_any_cast` type is an exception type that is thrown when |
| 45 | // failing to successfully cast the return value of an `absl::any` object. |
| 46 | // |
| 47 | // Example: |
| 48 | // |
| 49 | // auto a = absl::any(65); |
| 50 | // absl::any_cast<int>(a); // 65 |
| 51 | // try { |
| 52 | // absl::any_cast<char>(a); |
| 53 | // } catch(const absl::bad_any_cast& e) { |
| 54 | // std::cout << "Bad any cast: " << e.what() << '\n'; |
| 55 | // } |
| 56 | class bad_any_cast : public std::bad_cast { |
| 57 | public: |
| 58 | ~bad_any_cast() override; |
| 59 | const char* what() const noexcept override; |
| 60 | }; |
| 61 | |
| 62 | namespace any_internal { |
| 63 | |
| 64 | [[noreturn]] void ThrowBadAnyCast(); |
| 65 | |
| 66 | } // namespace any_internal |
| 67 | } // namespace absl |
| 68 | |
| 69 | #endif // ABSL_HAVE_STD_ANY |
| 70 | |
| 71 | #endif // ABSL_TYPES_BAD_ANY_CAST_H_ |
| 72 | |