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

source code of boost/libs/lexical_cast/include/boost/lexical_cast/try_lexical_convert.hpp