1 // Copyright Kevlin Henney, 2000-2005.
2 // Copyright Alexander Nasonov, 2006-2010.
3 // Copyright Antony Polukhin, 2011-2016.
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
26 #if defined(__clang__) || (defined(__GNUC__) && \
27 !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && \
28 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wuninitialized"
34 #include <boost/mpl/bool.hpp>
35 #include <boost/mpl/identity.hpp>
36 #include <boost/mpl/if.hpp>
37 #include <boost/type_traits/is_same.hpp>
38 #include <boost/type_traits/is_arithmetic.hpp>
40 #include <boost/lexical_cast/detail/is_character.hpp>
41 #include <boost/lexical_cast/detail/converter_numeric.hpp>
42 #include <boost/lexical_cast/detail/converter_lexical.hpp>
44 #include <boost/range/iterator_range_core.hpp>
45 #include <boost/container/container_fwd.hpp>
55 template<typename CharT, typename Traits, typename Alloc>
56 struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
60 // Sun Studio has problem with partial specialization of templates differing only in namespace.
61 // We workaround that by making `is_booststring` trait, instead of specializing `is_stdstring` for `boost::container::basic_string`.
67 template<typename CharT, typename Traits, typename Alloc>
68 struct is_booststring< boost::container::basic_string<CharT, Traits, Alloc> >
72 template<typename Target, typename Source>
73 struct is_arithmetic_and_not_xchars
75 typedef boost::mpl::bool_<
76 !(boost::detail::is_character<Target>::value) &&
77 !(boost::detail::is_character<Source>::value) &&
78 boost::is_arithmetic<Source>::value &&
79 boost::is_arithmetic<Target>::value
82 BOOST_STATIC_CONSTANT(bool, value = (
88 * is_xchar_to_xchar<Target, Source>::value is true,
89 * Target and Souce are char types of the same size 1 (char, signed char, unsigned char).
91 template<typename Target, typename Source>
92 struct is_xchar_to_xchar
94 typedef boost::mpl::bool_<
95 sizeof(Source) == sizeof(Target) &&
96 sizeof(Source) == sizeof(char) &&
97 boost::detail::is_character<Target>::value &&
98 boost::detail::is_character<Source>::value
101 BOOST_STATIC_CONSTANT(bool, value = (
106 template<typename Target, typename Source>
107 struct is_char_array_to_stdstring
111 template<typename CharT, typename Traits, typename Alloc>
112 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
116 template<typename CharT, typename Traits, typename Alloc>
117 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
121 // Sun Studio has problem with partial specialization of templates differing only in namespace.
122 // We workaround that by making `is_char_array_to_booststring` trait, instead of specializing `is_char_array_to_stdstring` for `boost::container::basic_string`.
123 template<typename Target, typename Source>
124 struct is_char_array_to_booststring
128 template<typename CharT, typename Traits, typename Alloc>
129 struct is_char_array_to_booststring< boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
133 template<typename CharT, typename Traits, typename Alloc>
134 struct is_char_array_to_booststring< boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
138 template <typename Target, typename Source>
139 struct copy_converter_impl
141 // MSVC fail to forward an array (DevDiv#555157 "SILENT BAD CODEGEN triggered by perfect forwarding",
142 // fixed in 2013 RTM).
143 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined(BOOST_MSVC) || BOOST_MSVC >= 1800)
145 static inline bool try_convert(T&& arg, Target& result) {
146 result = static_cast<T&&>(arg); // eqaul to `result = std::forward<T>(arg);`
150 static inline bool try_convert(const Source& arg, Target& result) {
158 namespace conversion { namespace detail {
160 template <typename Target, typename Source>
161 inline bool try_lexical_convert(const Source& arg, Target& result)
163 typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay<Source>::type src;
165 typedef boost::mpl::bool_<
166 boost::detail::is_xchar_to_xchar<Target, src >::value ||
167 boost::detail::is_char_array_to_stdstring<Target, src >::value ||
168 boost::detail::is_char_array_to_booststring<Target, src >::value ||
170 boost::is_same<Target, src >::value &&
171 (boost::detail::is_stdstring<Target >::value || boost::detail::is_booststring<Target >::value)
174 boost::is_same<Target, src >::value &&
175 boost::detail::is_character<Target >::value
179 typedef boost::detail::is_arithmetic_and_not_xchars<Target, src >
180 shall_we_copy_with_dynamic_check_t;
182 // We do evaluate second `if_` lazily to avoid unnecessary instantiations
183 // of `shall_we_copy_with_dynamic_check_t` and improve compilation times.
184 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
185 shall_we_copy_t::value,
186 boost::mpl::identity<boost::detail::copy_converter_impl<Target, src > >,
188 shall_we_copy_with_dynamic_check_t,
189 boost::detail::dynamic_num_converter_impl<Target, src >,
190 boost::detail::lexical_converter_impl<Target, src >
192 >::type caster_type_lazy;
194 typedef BOOST_DEDUCED_TYPENAME caster_type_lazy::type caster_type;
196 return caster_type::try_convert(arg, result);
199 template <typename Target, typename CharacterT>
200 inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result)
202 BOOST_STATIC_ASSERT_MSG(
203 boost::detail::is_character<CharacterT>::value,
204 "This overload of try_lexical_convert is meant to be used only with arrays of characters."
206 return ::boost::conversion::detail::try_lexical_convert(
207 ::boost::iterator_range<const CharacterT*>(chars, chars + count), result
211 }} // namespace conversion::detail
213 namespace conversion {
215 using ::boost::conversion::detail::try_lexical_convert;
220 #if defined(__clang__) || (defined(__GNUC__) && \
221 !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && \
222 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
223 #pragma GCC diagnostic pop
226 #endif // BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP