type_traits.h

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

Generated on Wed Mar 26 00:43:19 2008 for libstdc++ by  doxygen 1.5.1