numeric_traits.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free
00022 // software library without restriction.  Specifically, if other files
00023 // instantiate templates or use macros or inline functions from this
00024 // file, or you compile this file and link it with other files to
00025 // produce an executable, this file does not by itself cause the
00026 // resulting executable to be covered by the GNU General Public
00027 // License.  This exception does not however invalidate any other
00028 // reasons why the executable file might be covered by the GNU General
00029 // Public License.
00030 
00031 /** @file ext/numeric_traits.h
00032  *  This file is a GNU extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _EXT_NUMERIC_TRAITS
00036 #define _EXT_NUMERIC_TRAITS 1
00037 
00038 #pragma GCC system_header
00039 
00040 #include <bits/cpp_type_traits.h>
00041 #include <ext/type_traits.h>
00042 
00043 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00044 
00045   // Compile time constants for builtin types.
00046   // Sadly std::numeric_limits member functions cannot be used for this.
00047 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
00048 #define __glibcxx_digits(_Tp) \
00049   (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
00050 
00051 #define __glibcxx_min(_Tp) \
00052   (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
00053 
00054 #define __glibcxx_max(_Tp) \
00055   (__glibcxx_signed(_Tp) ? \
00056    (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
00057 
00058   template<typename _Value>
00059     struct __numeric_traits_integer
00060     {
00061       // Only integers for initialization of member constant.
00062       static const _Value __min = __glibcxx_min(_Value);
00063       static const _Value __max = __glibcxx_max(_Value);
00064 
00065       // NB: these two also available in std::numeric_limits as compile
00066       // time constants, but <limits> is big and we avoid including it.
00067       static const bool __is_signed = __glibcxx_signed(_Value);
00068       static const int __digits = __glibcxx_digits(_Value);      
00069     };
00070 
00071   template<typename _Value>
00072     const _Value __numeric_traits_integer<_Value>::__min;
00073 
00074   template<typename _Value>
00075     const _Value __numeric_traits_integer<_Value>::__max;
00076 
00077   template<typename _Value>
00078     const bool __numeric_traits_integer<_Value>::__is_signed;
00079 
00080   template<typename _Value>
00081     const int __numeric_traits_integer<_Value>::__digits;
00082 
00083 #undef __glibcxx_signed
00084 #undef __glibcxx_digits
00085 #undef __glibcxx_min
00086 #undef __glibcxx_max
00087 
00088 #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
00089   (std::__are_same<_Tp, float>::__value ? _Fval \
00090    : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
00091 
00092 #define __glibcxx_max_digits10(_Tp) \
00093   (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
00094               __LDBL_MANT_DIG__) * 3010 / 10000)
00095 
00096 #define __glibcxx_digits10(_Tp) \
00097   __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
00098 
00099 #define __glibcxx_max_exponent10(_Tp) \
00100   __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
00101              __LDBL_MAX_10_EXP__)
00102 
00103   template<typename _Value>
00104     struct __numeric_traits_floating
00105     {
00106       // Only floating point types. See N1822. 
00107       static const int __max_digits10 = __glibcxx_max_digits10(_Value);
00108 
00109       // See above comment...
00110       static const bool __is_signed = true;
00111       static const int __digits10 = __glibcxx_digits10(_Value);
00112       static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
00113     };
00114 
00115   template<typename _Value>
00116     const int __numeric_traits_floating<_Value>::__max_digits10;
00117 
00118   template<typename _Value>
00119     const bool __numeric_traits_floating<_Value>::__is_signed;
00120 
00121   template<typename _Value>
00122     const int __numeric_traits_floating<_Value>::__digits10;
00123 
00124   template<typename _Value>
00125     const int __numeric_traits_floating<_Value>::__max_exponent10;
00126 
00127   template<typename _Value>
00128     struct __numeric_traits
00129     : public __conditional_type<std::__is_integer<_Value>::__value,
00130                 __numeric_traits_integer<_Value>,
00131                 __numeric_traits_floating<_Value> >::__type
00132     { };
00133 
00134 _GLIBCXX_END_NAMESPACE
00135 
00136 #undef __glibcxx_floating
00137 #undef __glibcxx_max_digits10
00138 #undef __glibcxx_digits10
00139 #undef __glibcxx_max_exponent10
00140 
00141 #endif 

Generated on Sat Oct 25 05:09:06 2008 for libstdc++ by  doxygen 1.5.6