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