try_lexical_convert.hpp 3.3 KB

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