bad_lexical_cast.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2021.
  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. #if defined(BOOST_BORLANDC) && BOOST_WORKAROUND( BOOST_BORLANDC, < 0x560 )
  37. // under bcc32 5.5.1 bad_cast doesn't derive from exception
  38. , public std::exception
  39. #endif
  40. {
  41. public:
  42. bad_lexical_cast() BOOST_NOEXCEPT
  43. #ifndef BOOST_NO_TYPEID
  44. : source(&typeid(void)), target(&typeid(void))
  45. #endif
  46. {}
  47. const char *what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE {
  48. return "bad lexical cast: "
  49. "source type value could not be interpreted as target";
  50. }
  51. ~bad_lexical_cast() BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
  52. {}
  53. #ifndef BOOST_NO_TYPEID
  54. private:
  55. #ifdef BOOST_NO_STD_TYPEINFO
  56. typedef ::type_info type_info_t;
  57. #else
  58. typedef ::std::type_info type_info_t;
  59. #endif
  60. public:
  61. bad_lexical_cast(
  62. const type_info_t &source_type_arg,
  63. const type_info_t &target_type_arg) BOOST_NOEXCEPT
  64. : source(&source_type_arg), target(&target_type_arg)
  65. {}
  66. const type_info_t &source_type() const BOOST_NOEXCEPT {
  67. return *source;
  68. }
  69. const type_info_t &target_type() const BOOST_NOEXCEPT {
  70. return *target;
  71. }
  72. private:
  73. const type_info_t *source;
  74. const type_info_t *target;
  75. #endif
  76. };
  77. namespace conversion { namespace detail {
  78. #ifdef BOOST_NO_TYPEID
  79. template <class S, class T>
  80. inline void throw_bad_cast() {
  81. boost::throw_exception(bad_lexical_cast());
  82. }
  83. #else
  84. template <class S, class T>
  85. inline void throw_bad_cast() {
  86. boost::throw_exception(bad_lexical_cast(typeid(S), typeid(T)));
  87. }
  88. #endif
  89. }} // namespace conversion::detail
  90. } // namespace boost
  91. #endif // BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP