1 // Copyright Kevlin Henney, 2000-2005.
2 // Copyright Alexander Nasonov, 2006-2010.
3 // Copyright Antony Polukhin, 2011-2014.
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)
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
18 #ifndef BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP
19 #define BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP
21 #include <boost/config.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/identity.hpp>
29 #include <boost/mpl/if.hpp>
30 #include <boost/type_traits/is_same.hpp>
31 #include <boost/type_traits/is_arithmetic.hpp>
33 #include <boost/lexical_cast/detail/is_character.hpp>
34 #include <boost/lexical_cast/detail/converter_numeric.hpp>
35 #include <boost/lexical_cast/detail/converter_lexical.hpp>
37 #include <boost/range/iterator_range_core.hpp>
38 #include <boost/container/container_fwd.hpp>
48 template<typename CharT, typename Traits, typename Alloc>
49 struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
53 template<typename CharT, typename Traits, typename Alloc>
54 struct is_stdstring< boost::container::basic_string<CharT, Traits, Alloc> >
58 template<typename Target, typename Source>
59 struct is_arithmetic_and_not_xchars
61 typedef boost::mpl::bool_<
62 !(boost::detail::is_character<Target>::value) &&
63 !(boost::detail::is_character<Source>::value) &&
64 boost::is_arithmetic<Source>::value &&
65 boost::is_arithmetic<Target>::value
68 BOOST_STATIC_CONSTANT(bool, value = (
74 * is_xchar_to_xchar<Target, Source>::value is true,
75 * Target and Souce are char types of the same size 1 (char, signed char, unsigned char).
77 template<typename Target, typename Source>
78 struct is_xchar_to_xchar
80 typedef boost::mpl::bool_<
81 sizeof(Source) == sizeof(Target) &&
82 sizeof(Source) == sizeof(char) &&
83 boost::detail::is_character<Target>::value &&
84 boost::detail::is_character<Source>::value
87 BOOST_STATIC_CONSTANT(bool, value = (
92 template<typename Target, typename Source>
93 struct is_char_array_to_stdstring
97 template<typename CharT, typename Traits, typename Alloc>
98 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
102 template<typename CharT, typename Traits, typename Alloc>
103 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
107 template<typename CharT, typename Traits, typename Alloc>
108 struct is_char_array_to_stdstring< boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
112 template<typename CharT, typename Traits, typename Alloc>
113 struct is_char_array_to_stdstring< boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
117 template <typename Target, typename Source>
118 struct copy_converter_impl
120 // MSVC fail to forward an array (DevDiv#555157 "SILENT BAD CODEGEN triggered by perfect forwarding",
121 // fixed in 2013 RTM).
122 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined(BOOST_MSVC) || BOOST_MSVC >= 1800)
124 static inline bool try_convert(T&& arg, Target& result) {
125 result = static_cast<T&&>(arg); // eqaul to `result = std::forward<T>(arg);`
129 static inline bool try_convert(const Source& arg, Target& result) {
137 namespace conversion { namespace detail {
139 template <typename Target, typename Source>
140 inline bool try_lexical_convert(const Source& arg, Target& result)
142 typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay<Source>::type src;
144 typedef boost::mpl::bool_<
145 boost::detail::is_xchar_to_xchar<Target, src >::value ||
146 boost::detail::is_char_array_to_stdstring<Target, src >::value ||
148 boost::is_same<Target, src >::value &&
149 boost::detail::is_stdstring<Target >::value
152 boost::is_same<Target, src >::value &&
153 boost::detail::is_character<Target >::value
157 typedef boost::detail::is_arithmetic_and_not_xchars<Target, src >
158 shall_we_copy_with_dynamic_check_t;
160 // We do evaluate second `if_` lazily to avoid unnecessary instantiations
161 // of `shall_we_copy_with_dynamic_check_t` and improve compilation times.
162 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
163 shall_we_copy_t::value,
164 boost::mpl::identity<boost::detail::copy_converter_impl<Target, src > >,
166 shall_we_copy_with_dynamic_check_t,
167 boost::detail::dynamic_num_converter_impl<Target, src >,
168 boost::detail::lexical_converter_impl<Target, src >
170 >::type caster_type_lazy;
172 typedef BOOST_DEDUCED_TYPENAME caster_type_lazy::type caster_type;
174 return caster_type::try_convert(arg, result);
177 template <typename Target, typename CharacterT>
178 inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result)
180 BOOST_STATIC_ASSERT_MSG(
181 boost::detail::is_character<CharacterT>::value,
182 "This overload of try_lexical_convert is meant to be used only with arrays of characters."
184 return ::boost::conversion::detail::try_lexical_convert(
185 ::boost::iterator_range<const CharacterT*>(chars, chars + count), result
189 }} // namespace conversion::detail
191 namespace conversion {
193 using ::boost::conversion::detail::try_lexical_convert;
198 #endif // BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP