]> git.sesse.net Git - casparcg/blob - dependencies64/boost/boost/lexical_cast/try_lexical_convert.hpp
Merge pull request #506 from dimitry-ishenko-casparcg/fixes-flags
[casparcg] / dependencies64 / 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-2014.
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 <string>
27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/identity.hpp>
29 #include <boost/mpl/if.hpp>
30 #include <boost/type_traits/is_same.hpp>
31 #include <boost/type_traits/is_arithmetic.hpp>
32
33 #include <boost/lexical_cast/detail/is_character.hpp>
34 #include <boost/lexical_cast/detail/converter_numeric.hpp>
35 #include <boost/lexical_cast/detail/converter_lexical.hpp>
36
37 #include <boost/range/iterator_range_core.hpp>
38 #include <boost/container/container_fwd.hpp>
39
40 namespace boost {
41     namespace detail
42     {
43         template<typename T>
44         struct is_stdstring
45             : boost::false_type
46         {};
47
48         template<typename CharT, typename Traits, typename Alloc>
49         struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
50             : boost::true_type
51         {};
52
53         template<typename CharT, typename Traits, typename Alloc>
54         struct is_stdstring< boost::container::basic_string<CharT, Traits, Alloc> >
55             : boost::true_type
56         {};
57
58         template<typename Target, typename Source>
59         struct is_arithmetic_and_not_xchars
60         {
61             typedef boost::mpl::bool_<
62                     !(boost::detail::is_character<Target>::value) &&
63                     !(boost::detail::is_character<Source>::value) &&
64                     boost::is_arithmetic<Source>::value &&
65                     boost::is_arithmetic<Target>::value
66                 > type;
67         
68             BOOST_STATIC_CONSTANT(bool, value = (
69                 type::value
70             ));
71         };
72
73         /*
74          * is_xchar_to_xchar<Target, Source>::value is true, 
75          * Target and Souce are char types of the same size 1 (char, signed char, unsigned char).
76          */
77         template<typename Target, typename Source>
78         struct is_xchar_to_xchar 
79         {
80             typedef boost::mpl::bool_<
81                      sizeof(Source) == sizeof(Target) &&
82                      sizeof(Source) == sizeof(char) &&
83                      boost::detail::is_character<Target>::value &&
84                      boost::detail::is_character<Source>::value
85                 > type;
86                 
87             BOOST_STATIC_CONSTANT(bool, value = (
88                 type::value
89             ));
90         };
91
92         template<typename Target, typename Source>
93         struct is_char_array_to_stdstring
94             : boost::false_type
95         {};
96
97         template<typename CharT, typename Traits, typename Alloc>
98         struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
99             : boost::true_type
100         {};
101
102         template<typename CharT, typename Traits, typename Alloc>
103         struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
104             : boost::true_type
105         {};
106
107         template<typename CharT, typename Traits, typename Alloc>
108         struct is_char_array_to_stdstring< boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
109             : boost::true_type
110         {};
111
112         template<typename CharT, typename Traits, typename Alloc>
113         struct is_char_array_to_stdstring< boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
114             : boost::true_type
115         {};
116
117         template <typename Target, typename Source>
118         struct copy_converter_impl
119         {
120 // MSVC fail to forward an array (DevDiv#555157 "SILENT BAD CODEGEN triggered by perfect forwarding",
121 // fixed in 2013 RTM).
122 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && (!defined(BOOST_MSVC) || BOOST_MSVC >= 1800)
123             template <class T>
124             static inline bool try_convert(T&& arg, Target& result) {
125                 result = static_cast<T&&>(arg); // eqaul to `result = std::forward<T>(arg);`
126                 return true;
127             }
128 #else
129             static inline bool try_convert(const Source& arg, Target& result) {
130                 result = arg;
131                 return true;
132             }
133 #endif
134         };
135     }
136
137     namespace conversion { namespace detail {
138
139         template <typename Target, typename Source>
140         inline bool try_lexical_convert(const Source& arg, Target& result)
141         {
142             typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay<Source>::type src;
143
144             typedef boost::mpl::bool_<
145                 boost::detail::is_xchar_to_xchar<Target, src >::value ||
146                 boost::detail::is_char_array_to_stdstring<Target, src >::value ||
147                 (
148                      boost::is_same<Target, src >::value &&
149                      boost::detail::is_stdstring<Target >::value
150                 ) ||
151                 (
152                      boost::is_same<Target, src >::value &&
153                      boost::detail::is_character<Target >::value
154                 )
155             > shall_we_copy_t;
156
157             typedef boost::detail::is_arithmetic_and_not_xchars<Target, src >
158                 shall_we_copy_with_dynamic_check_t;
159
160             // We do evaluate second `if_` lazily to avoid unnecessary instantiations
161             // of `shall_we_copy_with_dynamic_check_t` and improve compilation times.
162             typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
163                 shall_we_copy_t::value,
164                 boost::mpl::identity<boost::detail::copy_converter_impl<Target, src > >,
165                 boost::mpl::if_<
166                      shall_we_copy_with_dynamic_check_t,
167                      boost::detail::dynamic_num_converter_impl<Target, src >,
168                      boost::detail::lexical_converter_impl<Target, src >
169                 >
170             >::type caster_type_lazy;
171
172             typedef BOOST_DEDUCED_TYPENAME caster_type_lazy::type caster_type;
173
174             return caster_type::try_convert(arg, result);
175         }
176
177         template <typename Target, typename CharacterT>
178         inline bool try_lexical_convert(const CharacterT* chars, std::size_t count, Target& result)
179         {
180             BOOST_STATIC_ASSERT_MSG(
181                 boost::detail::is_character<CharacterT>::value,
182                 "This overload of try_lexical_convert is meant to be used only with arrays of characters."
183             );
184             return ::boost::conversion::detail::try_lexical_convert(
185                 ::boost::iterator_range<const CharacterT*>(chars, chars + count), result
186             );
187         }
188
189     }} // namespace conversion::detail
190
191     namespace conversion {
192         // ADL barrier
193         using ::boost::conversion::detail::try_lexical_convert;
194     }
195
196 } // namespace boost
197
198 #endif // BOOST_LEXICAL_CAST_TRY_LEXICAL_CONVERT_HPP
199