Line data Source code
1 : // Copyright Kevlin Henney, 2000-2005. 2 : // Copyright Alexander Nasonov, 2006-2010. 3 : // Copyright Antony Polukhin, 2011-2025. 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 : 18 : #ifndef BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP 19 : #define BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP 20 : 21 : #include <type_traits> 22 : 23 : #include <boost/config.hpp> 24 : #ifdef BOOST_HAS_PRAGMA_ONCE 25 : # pragma once 26 : #endif 27 : 28 : #include <boost/lexical_cast/detail/buffer_view.hpp> 29 : #include <boost/lexical_cast/detail/is_character.hpp> 30 : #include <boost/lexical_cast/detail/converter_numeric.hpp> 31 : #include <boost/lexical_cast/detail/converter_lexical.hpp> 32 : 33 : namespace boost { 34 : namespace detail 35 : { 36 : template<typename Target, typename Source> 37 : using is_arithmetic_and_not_xchars = std::integral_constant< 38 : bool, 39 : !(boost::detail::is_character<Target>::value) && 40 : !(boost::detail::is_character<Source>::value) && 41 : std::is_arithmetic<Source>::value && 42 : std::is_arithmetic<Target>::value 43 : >; 44 : } 45 : 46 : namespace conversion { namespace detail { 47 : 48 : template <typename Target, typename Source> 49 15000 : inline bool try_lexical_convert(const Source& arg, Target& result) 50 : { 51 : static_assert( 52 : !std::is_volatile<Source>::value, 53 : "Boost.LexicalCast does not support volatile input"); 54 : 55 : typedef typename boost::detail::array_to_pointer_decay<Source>::type src; 56 : 57 : typedef boost::detail::is_arithmetic_and_not_xchars<Target, src > 58 : shall_we_copy_with_dynamic_check_t; 59 : 60 : typedef typename std::conditional< 61 : shall_we_copy_with_dynamic_check_t::value, 62 : boost::detail::dynamic_num_converter_impl<Target, src >, 63 : boost::detail::lexical_converter_impl<Target, src > 64 : >::type caster_type; 65 : 66 15000 : return caster_type::try_convert(arg, result); 67 : } 68 : 69 : template <typename Target, typename CharacterT> 70 : inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result) 71 : { 72 : static_assert( 73 : boost::detail::is_character<CharacterT>::value, 74 : "This overload of try_lexical_convert is meant to be used only with arrays of characters." 75 : ); 76 : return ::boost::conversion::detail::try_lexical_convert( 77 : ::boost::conversion::detail::make_buffer_view(chars, chars + count), 78 : result 79 : ); 80 : } 81 : 82 : }} // namespace conversion::detail 83 : 84 : namespace conversion { 85 : // ADL barrier 86 : using ::boost::conversion::detail::try_lexical_convert; 87 : } 88 : 89 : } // namespace boost 90 : 91 : #endif // BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP 92 :