libstdc++
parse_numbers.h
Go to the documentation of this file.
00001 // Components for compile-time parsing of numbers -*- C++ -*-
00002 
00003 // Copyright (C) 2013-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/parse_numbers.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{chrono}
00028  */
00029 
00030 #ifndef _GLIBCXX_PARSE_NUMBERS_H
00031 #define _GLIBCXX_PARSE_NUMBERS_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 // From n3642.pdf except I added binary literals and digit separator '\''.
00036 
00037 #if __cplusplus > 201103L
00038 
00039 #include <limits>
00040 
00041 namespace std _GLIBCXX_VISIBILITY(default)
00042 {
00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00044 
00045 namespace __parse_int
00046 {
00047   template<unsigned _Base, char _Dig>
00048     struct _Digit;
00049 
00050   template<unsigned _Base>
00051     struct _Digit<_Base, '0'> : integral_constant<unsigned, 0>
00052     {
00053       using __valid = true_type;
00054     };
00055 
00056   template<unsigned _Base>
00057     struct _Digit<_Base, '1'> : integral_constant<unsigned, 1>
00058     {
00059       using __valid = true_type;
00060     };
00061 
00062   template<unsigned _Base, unsigned _Val>
00063     struct _Digit_impl : integral_constant<unsigned, _Val>
00064     {
00065       static_assert(_Base > _Val, "invalid digit");
00066       using __valid = true_type;
00067     };
00068 
00069   template<unsigned _Base>
00070     struct _Digit<_Base, '2'> : _Digit_impl<_Base, 2>
00071     { };
00072 
00073   template<unsigned _Base>
00074     struct _Digit<_Base, '3'> : _Digit_impl<_Base, 3>
00075     { };
00076 
00077   template<unsigned _Base>
00078     struct _Digit<_Base, '4'> : _Digit_impl<_Base, 4>
00079     { };
00080 
00081   template<unsigned _Base>
00082     struct _Digit<_Base, '5'> : _Digit_impl<_Base, 5>
00083     { };
00084 
00085   template<unsigned _Base>
00086     struct _Digit<_Base, '6'> : _Digit_impl<_Base, 6>
00087     { };
00088 
00089   template<unsigned _Base>
00090     struct _Digit<_Base, '7'> : _Digit_impl<_Base, 7>
00091     { };
00092 
00093   template<unsigned _Base>
00094     struct _Digit<_Base, '8'> : _Digit_impl<_Base, 8>
00095     { };
00096 
00097   template<unsigned _Base>
00098     struct _Digit<_Base, '9'> : _Digit_impl<_Base, 9>
00099     { };
00100 
00101   template<unsigned _Base>
00102     struct _Digit<_Base, 'a'> : _Digit_impl<_Base, 0xa>
00103     { };
00104 
00105   template<unsigned _Base>
00106     struct _Digit<_Base, 'A'> : _Digit_impl<_Base, 0xa>
00107     { };
00108 
00109   template<unsigned _Base>
00110     struct _Digit<_Base, 'b'> : _Digit_impl<_Base, 0xb>
00111     { };
00112 
00113   template<unsigned _Base>
00114     struct _Digit<_Base, 'B'> : _Digit_impl<_Base, 0xb>
00115     { };
00116 
00117   template<unsigned _Base>
00118     struct _Digit<_Base, 'c'> : _Digit_impl<_Base, 0xc>
00119     { };
00120 
00121   template<unsigned _Base>
00122     struct _Digit<_Base, 'C'> : _Digit_impl<_Base, 0xc>
00123     { };
00124 
00125   template<unsigned _Base>
00126     struct _Digit<_Base, 'd'> : _Digit_impl<_Base, 0xd>
00127     { };
00128 
00129   template<unsigned _Base>
00130     struct _Digit<_Base, 'D'> : _Digit_impl<_Base, 0xd>
00131     { };
00132 
00133   template<unsigned _Base>
00134     struct _Digit<_Base, 'e'> : _Digit_impl<_Base, 0xe>
00135     { };
00136 
00137   template<unsigned _Base>
00138     struct _Digit<_Base, 'E'> : _Digit_impl<_Base, 0xe>
00139     { };
00140 
00141   template<unsigned _Base>
00142     struct _Digit<_Base, 'f'> : _Digit_impl<_Base, 0xf>
00143     { };
00144 
00145   template<unsigned _Base>
00146     struct _Digit<_Base, 'F'> : _Digit_impl<_Base, 0xf>
00147     { };
00148 
00149   //  Digit separator
00150   template<unsigned _Base>
00151     struct _Digit<_Base, '\''> : integral_constant<unsigned, 0>
00152     {
00153       using __valid = false_type;
00154     };
00155 
00156 //------------------------------------------------------------------------------
00157 
00158   template<unsigned long long _Val>
00159     using __ull_constant = integral_constant<unsigned long long, _Val>;
00160 
00161   template<unsigned _Base, char _Dig, char... _Digs>
00162     struct _Power_help
00163     {
00164       using __next = typename _Power_help<_Base, _Digs...>::type;
00165       using __valid_digit = typename _Digit<_Base, _Dig>::__valid;
00166       using type
00167         = __ull_constant<__next::value * (__valid_digit{} ? _Base : 1ULL)>;
00168     };
00169 
00170   template<unsigned _Base, char _Dig>
00171     struct _Power_help<_Base, _Dig>
00172     {
00173       using __valid_digit = typename _Digit<_Base, _Dig>::__valid;
00174       using type = __ull_constant<__valid_digit::value>;
00175     };
00176 
00177   template<unsigned _Base, char... _Digs>
00178     struct _Power : _Power_help<_Base, _Digs...>::type
00179     { };
00180 
00181   template<unsigned _Base>
00182     struct _Power<_Base> : __ull_constant<0>
00183     { };
00184 
00185 //------------------------------------------------------------------------------
00186 
00187   template<unsigned _Base, unsigned long long _Pow, char _Dig, char... _Digs>
00188     struct _Number_help
00189     {
00190       using __digit = _Digit<_Base, _Dig>;
00191       using __valid_digit = typename __digit::__valid;
00192       using __next = _Number_help<_Base,
00193                                   __valid_digit::value ? _Pow / _Base : _Pow,
00194                                   _Digs...>;
00195       using type = __ull_constant<_Pow * __digit::value + __next::type::value>;
00196       static_assert((type::value / _Pow) == __digit::value,
00197                     "integer literal does not fit in unsigned long long");
00198     };
00199 
00200   // Skip past digit separators:
00201   template<unsigned _Base, unsigned long long _Pow, char _Dig, char..._Digs>
00202     struct _Number_help<_Base, _Pow, '\'', _Dig, _Digs...>
00203     : _Number_help<_Base, _Pow, _Dig, _Digs...>
00204     { };
00205 
00206   // Terminating case for recursion:
00207   template<unsigned _Base, char _Dig>
00208     struct _Number_help<_Base, 1ULL, _Dig>
00209     {
00210       using type = __ull_constant<_Digit<_Base, _Dig>::value>;
00211     };
00212 
00213   template<unsigned _Base, char... _Digs>
00214     struct _Number
00215     : _Number_help<_Base, _Power<_Base, _Digs...>::value, _Digs...>::type
00216     { };
00217 
00218   template<unsigned _Base>
00219     struct _Number<_Base>
00220     : __ull_constant<0>
00221     { };
00222 
00223 //------------------------------------------------------------------------------
00224 
00225   template<char... _Digs>
00226     struct _Parse_int;
00227 
00228   template<char... _Digs>
00229     struct _Parse_int<'0', 'b', _Digs...>
00230     : _Number<2U, _Digs...>::type
00231     { };
00232 
00233   template<char... _Digs>
00234     struct _Parse_int<'0', 'B', _Digs...>
00235     : _Number<2U, _Digs...>::type
00236     { };
00237 
00238   template<char... _Digs>
00239     struct _Parse_int<'0', 'x', _Digs...>
00240     : _Number<16U, _Digs...>::type
00241     { };
00242 
00243   template<char... _Digs>
00244     struct _Parse_int<'0', 'X', _Digs...>
00245     : _Number<16U, _Digs...>::type
00246     { };
00247 
00248   template<char... _Digs>
00249     struct _Parse_int<'0', _Digs...>
00250     : _Number<8U, _Digs...>::type
00251     { };
00252 
00253   template<char... _Digs>
00254     struct _Parse_int
00255     : _Number<10U, _Digs...>::type
00256     { };
00257 
00258 } // namespace __parse_int
00259 
00260 
00261 namespace __select_int
00262 {
00263   template<unsigned long long _Val, typename... _Ints>
00264     struct _Select_int_base;
00265 
00266   template<unsigned long long _Val, typename _IntType, typename... _Ints>
00267     struct _Select_int_base<_Val, _IntType, _Ints...>
00268     : conditional_t<(_Val <= std::numeric_limits<_IntType>::max()),
00269                     integral_constant<_IntType, _Val>,
00270                     _Select_int_base<_Val, _Ints...>>
00271     { };
00272 
00273   template<unsigned long long _Val>
00274     struct _Select_int_base<_Val>
00275     { };
00276 
00277   template<char... _Digs>
00278     using _Select_int = typename _Select_int_base<
00279         __parse_int::_Parse_int<_Digs...>::value,
00280         unsigned char,
00281         unsigned short,
00282         unsigned int,
00283         unsigned long,
00284         unsigned long long
00285       >::type;
00286 
00287 } // namespace __select_int
00288 
00289 _GLIBCXX_END_NAMESPACE_VERSION
00290 } // namespace std
00291 
00292 #endif // __cplusplus > 201103L
00293 
00294 #endif // _GLIBCXX_PARSE_NUMBERS_H