Line data Source code
1 : // Copyright Kevlin Henney, 2000-2005. 2 : // Copyright Alexander Nasonov, 2006-2010. 3 : // Copyright Antony Polukhin, 2011-2025. 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. (See 6 : // accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : // what: lexical_cast custom keyword cast 10 : // who: contributed by Kevlin Henney, 11 : // enhanced with contributions from Terje Slettebo, 12 : // with additional fixes and suggestions from Gennaro Prota, 13 : // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, 14 : // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, 15 : // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters 16 : // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014 17 : 18 : #ifndef BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP 19 : #define BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP 20 : 21 : #include <boost/config.hpp> 22 : #ifdef BOOST_HAS_PRAGMA_ONCE 23 : # pragma once 24 : #endif 25 : 26 : #include <exception> 27 : #include <typeinfo> 28 : #include <boost/throw_exception.hpp> 29 : 30 : namespace boost 31 : { 32 : // exception used to indicate runtime lexical_cast failure 33 : class BOOST_SYMBOL_VISIBLE bad_lexical_cast : 34 : // workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0 35 : #if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS 36 : public std::exception 37 : #else 38 : public std::bad_cast 39 : #endif 40 : { 41 : public: 42 : bad_lexical_cast() noexcept 43 : #ifndef BOOST_NO_TYPEID 44 : : source(&typeid(void)), target(&typeid(void)) 45 : #endif 46 : {} 47 : 48 0 : const char *what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE { 49 0 : return "bad lexical cast: " 50 : "source type value could not be interpreted as target"; 51 : } 52 : 53 0 : bad_lexical_cast(const bad_lexical_cast&) = default; 54 : bad_lexical_cast& operator=(const bad_lexical_cast&) = default; 55 : 56 : #ifndef BOOST_NO_TYPEID 57 : private: 58 : #ifdef BOOST_NO_STD_TYPEINFO 59 : typedef ::type_info type_info_t; 60 : #else 61 : typedef ::std::type_info type_info_t; 62 : #endif 63 : public: 64 0 : bad_lexical_cast( 65 : const type_info_t &source_type_arg, 66 : const type_info_t &target_type_arg) noexcept 67 0 : : source(&source_type_arg), target(&target_type_arg) 68 0 : {} 69 : 70 : const type_info_t &source_type() const noexcept { 71 : return *source; 72 : } 73 : 74 : const type_info_t &target_type() const noexcept { 75 : return *target; 76 : } 77 : 78 : private: 79 : const type_info_t *source; 80 : const type_info_t *target; 81 : #endif 82 : }; 83 : 84 : namespace conversion { namespace detail { 85 : #ifdef BOOST_NO_TYPEID 86 : template <class S, class T> 87 : inline void throw_bad_cast() { 88 : boost::throw_exception(bad_lexical_cast()); 89 : } 90 : #else 91 : template <class S, class T> 92 0 : inline void throw_bad_cast() { 93 0 : boost::throw_exception(bad_lexical_cast(typeid(S), typeid(T))); 94 0 : } 95 : #endif 96 : }} // namespace conversion::detail 97 : 98 : } // namespace boost 99 : 100 : #endif // BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP