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 <limits>
00041 #include <bits/cpp_type_traits.h>
00042 #include <ext/type_traits.h>
00043 
00044 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00045 
00046   // Compile time constants for builtin types.
00047   // Sadly std::numeric_limits member functions cannot be used for this.
00048 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
00049 #define __glibcxx_digits(_Tp) \
00050   (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
00051 
00052 #define __glibcxx_min(_Tp) \
00053   (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
00054 
00055 #define __glibcxx_max(_Tp) \
00056   (__glibcxx_signed(_Tp) ? \
00057    (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
00058 
00059   template<typename _Value>
00060     struct __numeric_traits_integer
00061     {
00062       // Only integers for initialization of member constant.
00063       static const _Value __min = __glibcxx_min(_Value);
00064       static const _Value __max = __glibcxx_max(_Value);
00065     };
00066 
00067   template<typename _Value>
00068     const _Value __numeric_traits_integer<_Value>::__min;
00069 
00070   template<typename _Value>
00071     const _Value __numeric_traits_integer<_Value>::__max;
00072 
00073   template<typename _Value>
00074     struct __numeric_traits_floating
00075     {
00076       // Only floating point types. See N1822. 
00077       static const int __max_digits10 =
00078     2 + std::numeric_limits<_Value>::digits * 3010/10000;
00079     };
00080 
00081   template<typename _Value>
00082     const int __numeric_traits_floating<_Value>::__max_digits10;
00083 
00084   template<typename _Value>
00085     struct __numeric_traits
00086     : public __conditional_type<std::__is_integer<_Value>::__value,
00087                 __numeric_traits_integer<_Value>,
00088                 __numeric_traits_floating<_Value> >::__type
00089     { };
00090 
00091 _GLIBCXX_END_NAMESPACE
00092 
00093 #undef __glibcxx_signed
00094 #undef __glibcxx_min
00095 #undef __glibcxx_max
00096 #undef __glibcxx_digits
00097 
00098 #endif 

Generated on Thu Nov 1 13:12:19 2007 for libstdc++ by  doxygen 1.5.1