]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/lexical_cast/try_lexical_convert.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / lexical_cast / try_lexical_convert.hpp
1 // Copyright Kevlin Henney, 2000-2005.
2 // Copyright Alexander Nasonov, 2006-2010.
3 // Copyright Antony Polukhin, 2011-2016.
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 #if defined(__clang__) || (defined(__GNUC__) && \
27 !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && \
28 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wuninitialized"
31 #endif
32
33 #include <string>
34 #include <boost/mpl/bool.hpp>
35 #include <boost/mpl/identity.hpp>
36 #include <boost/mpl/if.hpp>
37 #include <boost/type_traits/is_same.hpp>
38 #include <boost/type_traits/is_arithmetic.hpp>
39
40 #include <boost/lexical_cast/detail/is_character.hpp>
41 #include <boost/lexical_cast/detail/converter_numeric.hpp>
42 #include <boost/lexical_cast/detail/converter_lexical.hpp>
43
44 #include <boost/range/iterator_range_core.hpp>
45 #include <boost/container/container_fwd.hpp>
46
47 namespace boost {
48 namespace detail
49 {
50 template<typename T>
51 struct is_stdstring
52 : boost::false_type
53 {};
54
55 template<typename CharT, typename Traits, typename Alloc>
56 struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
57 : boost::true_type
58 {};
59
60 // Sun Studio has problem with partial specialization of templates differing only in namespace.
61 // We workaround that by making `is_booststring` trait, instead of specializing `is_stdstring` for `boost::container::basic_string`.
62 template<typename T>
63 struct is_booststring
64 : boost::false_type
65 {};
66
67 template<typename CharT, typename Traits, typename Alloc>
68 struct is_booststring< boost::container::basic_string<CharT, Traits, Alloc> >
69 : boost::true_type
70 {};
71
72 template<typename Target, typename Source>
73 struct is_arithmetic_and_not_xchars
74 {
75 typedef boost::mpl::bool_<
76 !(boost::detail::is_character<Target>::value) &&
77 !(boost::detail::is_character<Source>::value) &&
78 boost::is_arithmetic<Source>::value &&
79 boost::is_arithmetic<Target>::value
80 > type;
81
82 BOOST_STATIC_CONSTANT(bool, value = (
83 type::value
84 ));
85 };
86
87 /*
88 * is_xchar_to_xchar<Target, Source>::value is true,
89 * Target and Souce are char types of the same size 1 (char, signed char, unsigned char).
90 */
91 template<typename Target, typename Source>
92 struct is_xchar_to_xchar
93 {
94 typedef boost::mpl::bool_<
95 sizeof(Source) == sizeof(Target) &&
96 sizeof(Source) == sizeof(char) &&
97 boost::detail::is_character<Target>::value &&
98 boost::detail::is_character<Source>::value
99 > type;
100
101 BOOST_STATIC_CONSTANT(bool, value = (
102 type::value
103 ));
104 };
105
106 template<typename Target, typename Source>
107 struct is_char_array_to_stdstring
108 : boost::false_type
109 {};
110
111 template<typename CharT, typename Traits, typename Alloc>
112 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
113 : boost::true_type
114 {};
115
116 template<typename CharT, typename Traits, typename Alloc>
117 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
118 : boost::true_type
119 {};
120
121 // Sun Studio has problem with partial specialization of templates differing only in namespace.
122 // We workaround that by making `is_char_array_to_booststring` trait, instead of specializing `is_char_array_to_stdstring` for `boost::container::basic_string`.
123 template<typename Target, typename Source>
124 struct is_char_array_to_booststring
125 : boost::false_type
126 {};
127
128 template<typename CharT, typename Traits, typename Alloc>
129 struct is_char_array_to_booststring< boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
130 : boost::true_type
131 {};
132
133 template<typename CharT, typename Traits, typename Alloc>
134 struct is_char_array_to_booststring< boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
135 : boost::true_type
136 {};
137
138 template <typename Target, typename Source>
139 struct copy_converter_impl
140 {
141 // MSVC fail to forward an array (DevDiv#555157 "SILENT BAD CODEGEN triggered by perfect forwarding",
142 // fixed in 2013 RTM).
143 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined(BOOST_MSVC) || BOOST_MSVC >= 1800)
144 template <class T>
145 static inline bool try_convert(T&& arg, Target& result) {
146 result = static_cast<T&&>(arg); // eqaul to `result = std::forward<T>(arg);`
147 return true;
148 }
149 #else
150 static inline bool try_convert(const Source& arg, Target& result) {
151 result = arg;
152 return true;
153 }
154 #endif
155 };
156 }
157
158 namespace conversion { namespace detail {
159
160 template <typename Target, typename Source>
161 inline bool try_lexical_convert(const Source& arg, Target& result)
162 {
163 typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay<Source>::type src;
164
165 typedef boost::mpl::bool_<
166 boost::detail::is_xchar_to_xchar<Target, src >::value ||
167 boost::detail::is_char_array_to_stdstring<Target, src >::value ||
168 boost::detail::is_char_array_to_booststring<Target, src >::value ||
169 (
170 boost::is_same<Target, src >::value &&
171 (boost::detail::is_stdstring<Target >::value || boost::detail::is_booststring<Target >::value)
172 ) ||
173 (
174 boost::is_same<Target, src >::value &&
175 boost::detail::is_character<Target >::value
176 )
177 > shall_we_copy_t;
178
179 typedef boost::detail::is_arithmetic_and_not_xchars<Target, src >
180 shall_we_copy_with_dynamic_check_t;
181
182 // We do evaluate second `if_` lazily to avoid unnecessary instantiations
183 // of `shall_we_copy_with_dynamic_check_t` and improve compilation times.
184 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
185 shall_we_copy_t::value,
186 boost::mpl::identity<boost::detail::copy_converter_impl<Target, src > >,
187 boost::mpl::if_<
188 shall_we_copy_with_dynamic_check_t,
189 boost::detail::dynamic_num_converter_impl<Target, src >,
190 boost::detail::lexical_converter_impl<Target, src >
191 >
192 >::type caster_type_lazy;
193
194 typedef BOOST_DEDUCED_TYPENAME caster_type_lazy::type caster_type;
195
196 return caster_type::try_convert(arg, result);
197 }
198
199 template <typename Target, typename CharacterT>
200 inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result)
201 {
202 BOOST_STATIC_ASSERT_MSG(
203 boost::detail::is_character<CharacterT>::value,
204 "This overload of try_lexical_convert is meant to be used only with arrays of characters."
205 );
206 return ::boost::conversion::detail::try_lexical_convert(
207 ::boost::iterator_range<const CharacterT*>(chars, chars + count), result
208 );
209 }
210
211 }} // namespace conversion::detail
212
213 namespace conversion {
214 // ADL barrier
215 using ::boost::conversion::detail::try_lexical_convert;
216 }
217
218 } // namespace boost
219
220 #if defined(__clang__) || (defined(__GNUC__) && \
221 !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && \
222 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
223 #pragma GCC diagnostic pop
224 #endif
225
226 #endif // BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP
227