bad_lexical_cast.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2024.
  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. #ifndef BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP
  18. #define BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #include <exception>
  24. #include <typeinfo>
  25. #include <boost/throw_exception.hpp>
  26. namespace boost
  27. {
  28. // exception used to indicate runtime lexical_cast failure
  29. class BOOST_SYMBOL_VISIBLE bad_lexical_cast :
  30. // workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0
  31. #if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
  32. public std::exception
  33. #else
  34. public std::bad_cast
  35. #endif
  36. {
  37. public:
  38. bad_lexical_cast() noexcept
  39. #ifndef BOOST_NO_TYPEID
  40. : source(&typeid(void)), target(&typeid(void))
  41. #endif
  42. {}
  43. const char *what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE {
  44. return "bad lexical cast: "
  45. "source type value could not be interpreted as target";
  46. }
  47. bad_lexical_cast(const bad_lexical_cast&) = default;
  48. bad_lexical_cast& operator=(const bad_lexical_cast&) = default;
  49. #ifndef BOOST_NO_TYPEID
  50. private:
  51. #ifdef BOOST_NO_STD_TYPEINFO
  52. typedef ::type_info type_info_t;
  53. #else
  54. typedef ::std::type_info type_info_t;
  55. #endif
  56. public:
  57. bad_lexical_cast(
  58. const type_info_t &source_type_arg,
  59. const type_info_t &target_type_arg) noexcept
  60. : source(&source_type_arg), target(&target_type_arg)
  61. {}
  62. const type_info_t &source_type() const noexcept {
  63. return *source;
  64. }
  65. const type_info_t &target_type() const noexcept {
  66. return *target;
  67. }
  68. private:
  69. const type_info_t *source;
  70. const type_info_t *target;
  71. #endif
  72. };
  73. namespace conversion { namespace detail {
  74. #ifdef BOOST_NO_TYPEID
  75. template <class S, class T>
  76. inline void throw_bad_cast() {
  77. boost::throw_exception(bad_lexical_cast());
  78. }
  79. #else
  80. template <class S, class T>
  81. inline void throw_bad_cast() {
  82. boost::throw_exception(bad_lexical_cast(typeid(S), typeid(T)));
  83. }
  84. #endif
  85. }} // namespace conversion::detail
  86. } // namespace boost
  87. #endif // BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP