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-2015 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   template<unsigned _Base, unsigned long long _Pow, char _Dig>
00201     struct _Number_help<_Base, _Pow, _Dig>
00202     {
00203       //static_assert(_Pow == 1U, "power should be one");
00204       using type = __ull_constant<_Digit<_Base, _Dig>::value>;
00205     };
00206 
00207   template<unsigned _Base, char... _Digs>
00208     struct _Number
00209     : _Number_help<_Base, _Power<_Base, _Digs...>::value, _Digs...>::type
00210     { };
00211 
00212   template<unsigned _Base>
00213     struct _Number<_Base>
00214     : __ull_constant<0>
00215     { };
00216 
00217 //------------------------------------------------------------------------------
00218 
00219   template<char... _Digs>
00220     struct _Parse_int;
00221 
00222   template<char... _Digs>
00223     struct _Parse_int<'0', 'b', _Digs...>
00224     : _Number<2U, _Digs...>::type
00225     { };
00226 
00227   template<char... _Digs>
00228     struct _Parse_int<'0', 'B', _Digs...>
00229     : _Number<2U, _Digs...>::type
00230     { };
00231 
00232   template<char... _Digs>
00233     struct _Parse_int<'0', 'x', _Digs...>
00234     : _Number<16U, _Digs...>::type
00235     { };
00236 
00237   template<char... _Digs>
00238     struct _Parse_int<'0', 'X', _Digs...>
00239     : _Number<16U, _Digs...>::type
00240     { };
00241 
00242   template<char... _Digs>
00243     struct _Parse_int<'0', _Digs...>
00244     : _Number<8U, _Digs...>::type
00245     { };
00246 
00247   template<char... _Digs>
00248     struct _Parse_int
00249     : _Number<10U, _Digs...>::type
00250     { };
00251 
00252 } // namespace __parse_int
00253 
00254 
00255 namespace __select_int
00256 {
00257   template<unsigned long long _Val, typename... _Ints>
00258     struct _Select_int_base;
00259 
00260   template<unsigned long long _Val, typename _IntType, typename... _Ints>
00261     struct _Select_int_base<_Val, _IntType, _Ints...>
00262     : conditional_t<(_Val <= std::numeric_limits<_IntType>::max()),
00263                     integral_constant<_IntType, _Val>,
00264                     _Select_int_base<_Val, _Ints...>>
00265     { };
00266 
00267   template<unsigned long long _Val>
00268     struct _Select_int_base<_Val>
00269     { };
00270 
00271   template<char... _Digs>
00272     using _Select_int = typename _Select_int_base<
00273         __parse_int::_Parse_int<_Digs...>::value,
00274         unsigned char,
00275         unsigned short,
00276         unsigned int,
00277         unsigned long,
00278         unsigned long long
00279       >::type;
00280 
00281 } // namespace __select_int
00282 
00283 _GLIBCXX_END_NAMESPACE_VERSION
00284 } // namespace std
00285 
00286 #endif // __cplusplus > 201103L
00287 
00288 #endif // _GLIBCXX_PARSE_NUMBERS_H