type_traits.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 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/type_traits.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _EXT_TYPE_TRAITS
00030 #define _EXT_TYPE_TRAITS 1
00031 
00032 #pragma GCC system_header
00033 
00034 #include <bits/c++config.h>
00035 #include <bits/cpp_type_traits.h>
00036 
00037 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00038 
00039   // Define a nested type if some predicate holds.
00040   template<bool, typename>
00041     struct __enable_if 
00042     { };
00043 
00044   template<typename _Tp>
00045     struct __enable_if<true, _Tp>
00046     { typedef _Tp __type; };
00047 
00048 
00049   // Conditional expression for types. If true, first, if false, second.
00050   template<bool _Cond, typename _Iftrue, typename _Iffalse>
00051     struct __conditional_type
00052     { typedef _Iftrue __type; };
00053 
00054   template<typename _Iftrue, typename _Iffalse>
00055     struct __conditional_type<false, _Iftrue, _Iffalse>
00056     { typedef _Iffalse __type; };
00057 
00058 
00059   // Given an integral builtin type, return the corresponding unsigned type.
00060   template<typename _Tp>
00061     struct __add_unsigned
00062     { 
00063     private:
00064       typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
00065       
00066     public:
00067       typedef typename __if_type::__type __type; 
00068     };
00069 
00070   template<>
00071     struct __add_unsigned<char>
00072     { typedef unsigned char __type; };
00073 
00074   template<>
00075     struct __add_unsigned<signed char>
00076     { typedef unsigned char __type; };
00077 
00078   template<>
00079     struct __add_unsigned<short>
00080     { typedef unsigned short __type; };
00081 
00082   template<>
00083     struct __add_unsigned<int>
00084     { typedef unsigned int __type; };
00085 
00086   template<>
00087     struct __add_unsigned<long>
00088     { typedef unsigned long __type; };
00089 
00090   template<>
00091     struct __add_unsigned<long long>
00092     { typedef unsigned long long __type; };
00093 
00094   // Declare but don't define.
00095   template<>
00096     struct __add_unsigned<bool>;
00097 
00098   template<>
00099     struct __add_unsigned<wchar_t>;
00100 
00101 
00102   // Given an integral builtin type, return the corresponding signed type.
00103   template<typename _Tp>
00104     struct __remove_unsigned
00105     { 
00106     private:
00107       typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
00108       
00109     public:
00110       typedef typename __if_type::__type __type; 
00111     };
00112 
00113   template<>
00114     struct __remove_unsigned<char>
00115     { typedef signed char __type; };
00116 
00117   template<>
00118     struct __remove_unsigned<unsigned char>
00119     { typedef signed char __type; };
00120 
00121   template<>
00122     struct __remove_unsigned<unsigned short>
00123     { typedef short __type; };
00124 
00125   template<>
00126     struct __remove_unsigned<unsigned int>
00127     { typedef int __type; };
00128 
00129   template<>
00130     struct __remove_unsigned<unsigned long>
00131     { typedef long __type; };
00132 
00133   template<>
00134     struct __remove_unsigned<unsigned long long>
00135     { typedef long long __type; };
00136 
00137   // Declare but don't define.
00138   template<>
00139     struct __remove_unsigned<bool>;
00140 
00141   template<>
00142     struct __remove_unsigned<wchar_t>;
00143 
00144 
00145   // For use in string and vstring.
00146   template<typename _Type>
00147     inline bool
00148     __is_null_pointer(_Type* __ptr)
00149     { return __ptr == 0; }
00150 
00151   template<typename _Type>
00152     inline bool
00153     __is_null_pointer(_Type)
00154     { return false; }
00155 
00156 
00157   // For complex and cmath
00158   template<typename _Tp, bool = std::__is_integer<_Tp>::__value>
00159     struct __promote
00160     { typedef double __type; };
00161 
00162   template<typename _Tp>
00163     struct __promote<_Tp, false>
00164     { typedef _Tp __type; };
00165 
00166   template<typename _Tp, typename _Up>
00167     struct __promote_2
00168     {
00169     private:
00170       typedef typename __promote<_Tp>::__type __type1;
00171       typedef typename __promote<_Up>::__type __type2;
00172 
00173     public:
00174       typedef __typeof__(__type1() + __type2()) __type;
00175     };
00176 
00177   template<typename _Tp, typename _Up, typename _Vp>
00178     struct __promote_3
00179     {
00180     private:
00181       typedef typename __promote<_Tp>::__type __type1;
00182       typedef typename __promote<_Up>::__type __type2;
00183       typedef typename __promote<_Vp>::__type __type3;
00184 
00185     public:
00186       typedef __typeof__(__type1() + __type2() + __type3()) __type;
00187     };
00188 
00189   template<typename _Tp, typename _Up, typename _Vp, typename _Wp>
00190     struct __promote_4
00191     {
00192     private:
00193       typedef typename __promote<_Tp>::__type __type1;
00194       typedef typename __promote<_Up>::__type __type2;
00195       typedef typename __promote<_Vp>::__type __type3;
00196       typedef typename __promote<_Wp>::__type __type4;
00197 
00198     public:
00199       typedef __typeof__(__type1() + __type2() + __type3() + __type4()) __type;
00200     };
00201 
00202 _GLIBCXX_END_NAMESPACE
00203 
00204 #endif 

Generated on Tue Apr 21 13:13:35 2009 for libstdc++ by  doxygen 1.5.8