// Copyright Kevlin Henney, 2000-2005. // Copyright Alexander Nasonov, 2006-2010. // Copyright Antony Polukhin, 2011-2018. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // what: lexical_cast custom keyword cast // who: contributed by Kevlin Henney, // enhanced with contributions from Terje Slettebo, // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014 #ifndef BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP #define BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP #include #ifdef BOOST_HAS_PRAGMA_ONCE # pragma once #endif #if defined(__clang__) || (defined(__GNUC__) && \ !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && \ (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wuninitialized" #pragma GCC diagnostic ignored "-Wsign-conversion" #endif #include #include #include #include #include #include #include #include #include #include #include namespace lslboost { namespace detail { template struct is_stdstring : lslboost::false_type {}; template struct is_stdstring< std::basic_string > : lslboost::true_type {}; // Sun Studio has problem with partial specialization of templates differing only in namespace. // We workaround that by making `is_booststring` trait, instead of specializing `is_stdstring` for `lslboost::container::basic_string`. template struct is_booststring : lslboost::false_type {}; template struct is_booststring< lslboost::container::basic_string > : lslboost::true_type {}; template struct is_arithmetic_and_not_xchars { typedef lslboost::integral_constant< bool, !(lslboost::detail::is_character::value) && !(lslboost::detail::is_character::value) && lslboost::is_arithmetic::value && lslboost::is_arithmetic::value > type; BOOST_STATIC_CONSTANT(bool, value = ( type::value )); }; /* * is_xchar_to_xchar::value is true, * Target and Souce are char types of the same size 1 (char, signed char, unsigned char). */ template struct is_xchar_to_xchar { typedef lslboost::integral_constant< bool, sizeof(Source) == sizeof(Target) && sizeof(Source) == sizeof(char) && lslboost::detail::is_character::value && lslboost::detail::is_character::value > type; BOOST_STATIC_CONSTANT(bool, value = ( type::value )); }; template struct is_char_array_to_stdstring : lslboost::false_type {}; template struct is_char_array_to_stdstring< std::basic_string, CharT* > : lslboost::true_type {}; template struct is_char_array_to_stdstring< std::basic_string, const CharT* > : lslboost::true_type {}; // Sun Studio has problem with partial specialization of templates differing only in namespace. // We workaround that by making `is_char_array_to_booststring` trait, instead of specializing `is_char_array_to_stdstring` for `lslboost::container::basic_string`. template struct is_char_array_to_booststring : lslboost::false_type {}; template struct is_char_array_to_booststring< lslboost::container::basic_string, CharT* > : lslboost::true_type {}; template struct is_char_array_to_booststring< lslboost::container::basic_string, const CharT* > : lslboost::true_type {}; template struct copy_converter_impl { // MSVC fail to forward an array (DevDiv#555157 "SILENT BAD CODEGEN triggered by perfect forwarding", // fixed in 2013 RTM). #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined(BOOST_MSVC) || BOOST_MSVC >= 1800) template static inline bool try_convert(T&& arg, Target& result) { result = static_cast(arg); // eqaul to `result = std::forward(arg);` return true; } #else static inline bool try_convert(const Source& arg, Target& result) { result = arg; return true; } #endif }; } namespace conversion { namespace detail { template inline bool try_lexical_convert(const Source& arg, Target& result) { typedef BOOST_DEDUCED_TYPENAME lslboost::detail::array_to_pointer_decay::type src; typedef lslboost::integral_constant< bool, lslboost::detail::is_xchar_to_xchar::value || lslboost::detail::is_char_array_to_stdstring::value || lslboost::detail::is_char_array_to_booststring::value || ( lslboost::is_same::value && (lslboost::detail::is_stdstring::value || lslboost::detail::is_booststring::value) ) || ( lslboost::is_same::value && lslboost::detail::is_character::value ) > shall_we_copy_t; typedef lslboost::detail::is_arithmetic_and_not_xchars shall_we_copy_with_dynamic_check_t; // We do evaluate second `if_` lazily to avoid unnecessary instantiations // of `shall_we_copy_with_dynamic_check_t` and improve compilation times. typedef BOOST_DEDUCED_TYPENAME lslboost::conditional< shall_we_copy_t::value, lslboost::type_identity >, lslboost::conditional< shall_we_copy_with_dynamic_check_t::value, lslboost::detail::dynamic_num_converter_impl, lslboost::detail::lexical_converter_impl > >::type caster_type_lazy; typedef BOOST_DEDUCED_TYPENAME caster_type_lazy::type caster_type; return caster_type::try_convert(arg, result); } template inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result) { BOOST_STATIC_ASSERT_MSG( lslboost::detail::is_character::value, "This overload of try_lexical_convert is meant to be used only with arrays of characters." ); return ::lslboost::conversion::detail::try_lexical_convert( ::lslboost::iterator_range(chars, chars + count), result ); } }} // namespace conversion::detail namespace conversion { // ADL barrier using ::lslboost::conversion::detail::try_lexical_convert; } } // namespace lslboost #if defined(__clang__) || (defined(__GNUC__) && \ !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && \ (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) #pragma GCC diagnostic pop #endif #endif // BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP