numeric_traits.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2009 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 terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // 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 ext/numeric_traits.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _EXT_NUMERIC_TRAITS
00030 #define _EXT_NUMERIC_TRAITS 1
00031 
00032 #pragma GCC system_header
00033 
00034 #include <bits/cpp_type_traits.h>
00035 #include <ext/type_traits.h>
00036 
00037 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00038 
00039   // Compile time constants for builtin types.
00040   // Sadly std::numeric_limits member functions cannot be used for this.
00041 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
00042 #define __glibcxx_digits(_Tp) \
00043   (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
00044 
00045 #define __glibcxx_min(_Tp) \
00046   (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
00047 
00048 #define __glibcxx_max(_Tp) \
00049   (__glibcxx_signed(_Tp) ? \
00050    (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
00051 
00052   template<typename _Value>
00053     struct __numeric_traits_integer
00054     {
00055       // Only integers for initialization of member constant.
00056       static const _Value __min = __glibcxx_min(_Value);
00057       static const _Value __max = __glibcxx_max(_Value);
00058 
00059       // NB: these two also available in std::numeric_limits as compile
00060       // time constants, but <limits> is big and we avoid including it.
00061       static const bool __is_signed = __glibcxx_signed(_Value);
00062       static const int __digits = __glibcxx_digits(_Value);      
00063     };
00064 
00065   template<typename _Value>
00066     const _Value __numeric_traits_integer<_Value>::__min;
00067 
00068   template<typename _Value>
00069     const _Value __numeric_traits_integer<_Value>::__max;
00070 
00071   template<typename _Value>
00072     const bool __numeric_traits_integer<_Value>::__is_signed;
00073 
00074   template<typename _Value>
00075     const int __numeric_traits_integer<_Value>::__digits;
00076 
00077 #undef __glibcxx_signed
00078 #undef __glibcxx_digits
00079 #undef __glibcxx_min
00080 #undef __glibcxx_max
00081 
00082 #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
00083   (std::__are_same<_Tp, float>::__value ? _Fval \
00084    : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
00085 
00086 #define __glibcxx_max_digits10(_Tp) \
00087   (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
00088               __LDBL_MANT_DIG__) * 3010 / 10000)
00089 
00090 #define __glibcxx_digits10(_Tp) \
00091   __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
00092 
00093 #define __glibcxx_max_exponent10(_Tp) \
00094   __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
00095              __LDBL_MAX_10_EXP__)
00096 
00097   template<typename _Value>
00098     struct __numeric_traits_floating
00099     {
00100       // Only floating point types. See N1822. 
00101       static const int __max_digits10 = __glibcxx_max_digits10(_Value);
00102 
00103       // See above comment...
00104       static const bool __is_signed = true;
00105       static const int __digits10 = __glibcxx_digits10(_Value);
00106       static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
00107     };
00108 
00109   template<typename _Value>
00110     const int __numeric_traits_floating<_Value>::__max_digits10;
00111 
00112   template<typename _Value>
00113     const bool __numeric_traits_floating<_Value>::__is_signed;
00114 
00115   template<typename _Value>
00116     const int __numeric_traits_floating<_Value>::__digits10;
00117 
00118   template<typename _Value>
00119     const int __numeric_traits_floating<_Value>::__max_exponent10;
00120 
00121   template<typename _Value>
00122     struct __numeric_traits
00123     : public __conditional_type<std::__is_integer<_Value>::__value,
00124                 __numeric_traits_integer<_Value>,
00125                 __numeric_traits_floating<_Value> >::__type
00126     { };
00127 
00128 _GLIBCXX_END_NAMESPACE
00129 
00130 #undef __glibcxx_floating
00131 #undef __glibcxx_max_digits10
00132 #undef __glibcxx_digits10
00133 #undef __glibcxx_max_exponent10
00134 
00135 #endif 

Generated on Thu Jul 23 21:16:12 2009 for libstdc++ by  doxygen 1.5.8