try_lexical_convert.hpp 3.4 KB

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