bad_lexical_cast.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2019.
  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 <typeinfo>
  24. #include <exception>
  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(__BORLANDC__) && BOOST_WORKAROUND( __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. virtual const char *what() const BOOST_NOEXCEPT_OR_NOTHROW {
  48. return "bad lexical cast: "
  49. "source type value could not be interpreted as target";
  50. }
  51. virtual ~bad_lexical_cast() BOOST_NOEXCEPT_OR_NOTHROW
  52. {}
  53. #ifndef BOOST_NO_TYPEID
  54. bad_lexical_cast(
  55. const std::type_info &source_type_arg,
  56. const std::type_info &target_type_arg) BOOST_NOEXCEPT
  57. : source(&source_type_arg), target(&target_type_arg)
  58. {}
  59. const std::type_info &source_type() const BOOST_NOEXCEPT {
  60. return *source;
  61. }
  62. const std::type_info &target_type() const BOOST_NOEXCEPT {
  63. return *target;
  64. }
  65. private:
  66. const std::type_info *source;
  67. const std::type_info *target;
  68. #endif
  69. };
  70. namespace conversion { namespace detail {
  71. #ifdef BOOST_NO_TYPEID
  72. template <class S, class T>
  73. inline void throw_bad_cast() {
  74. boost::throw_exception(bad_lexical_cast());
  75. }
  76. #else
  77. template <class S, class T>
  78. inline void throw_bad_cast() {
  79. boost::throw_exception(bad_lexical_cast(typeid(S), typeid(T)));
  80. }
  81. #endif
  82. }} // namespace conversion::detail
  83. } // namespace boost
  84. #endif // BOOST_LEXICAL_CAST_BAD_LEXICAL_CAST_HPP