4 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>,
5 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
6 * economic rights: Technische Universität Dresden (Germany)
8 * This file is part of TUD:OS and distributed under the terms of the
9 * GNU General Public License 2.
10 * Please see the COPYING-GPL-2 file for details.
12 * As a special exception, you may use this file as part of a free software
13 * library without restriction. Specifically, if other files instantiate
14 * templates or use macros or inline functions from this file, or you compile
15 * this file and link it with other files to produce an executable, this
16 * file does not by itself cause the resulting executable to be covered by
17 * the GNU General Public License. This exception does not however
18 * invalidate any other reasons why the executable file might be covered by
19 * the GNU General Public License.
25 #include "bits/type_traits.h"
28 #define CXX_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
33 template< typename T, T V >
34 struct integral_constant
36 static T const value = V;
38 typedef integral_constant<T, V> type;
41 typedef integral_constant<bool, true> true_type;
42 typedef integral_constant<bool, false> false_type;
44 template< typename T > struct remove_reference;
46 template< typename T > struct idendity { typedef T type; };
48 template< typename T1, typename T2 > struct is_same;
50 template< typename T > struct remove_const;
52 template< typename T > struct remove_volatile;
54 template< typename T > struct remove_cv;
56 template< typename T > struct remove_pointer;
60 template< typename, typename >
61 struct is_same : public false_type {};
63 template< typename T >
64 struct is_same<T, T> : public true_type {};
66 template< typename T >
67 struct remove_reference { typedef T type; };
69 template< typename T >
70 struct remove_reference<T &> { typedef T type; };
72 #ifdef __GXX_EXPERIMENTAL_CXX0X__
73 template< typename T >
74 struct remove_reference<T &&> { typedef T type; };
77 template< typename T > struct remove_const { typedef T type; };
78 template< typename T > struct remove_const<T const> { typedef T type; };
80 template< typename T > struct remove_volatile { typedef T type; };
81 template< typename T > struct remove_volatile<T volatile> { typedef T type; };
83 template< typename T >
84 struct remove_cv { typedef typename remove_const<typename remove_volatile<T>::type>::type type; };
86 template< typename T, typename >
87 struct __remove_pointer_h { typedef T type; };
89 template< typename T, typename I >
90 struct __remove_pointer_h<T, I*> { typedef I type; };
92 template< typename T >
93 struct remove_pointer : public __remove_pointer_h<T, typename remove_cv<T>::type> {};
95 #ifdef __GXX_EXPERIMENTAL_CXX0X__
96 #if CXX_GCC_VERSION >= 405
98 template< typename T >
100 forward(typename cxx::remove_reference<T>::type &t)
101 { return static_cast<T &&>(t); }
103 template< typename T >
105 forward(typename cxx::remove_reference<T>::type &&t)
106 { return static_cast<T &&>(t); }
110 template< typename T >
112 forward(typename cxx::idendity<T>::type &&t)
117 template< typename T >
118 inline typename cxx::remove_reference<T>::type &&
119 move(T &t) { return static_cast<typename cxx::remove_reference<T>::type &&>(t); }
122 template< bool, typename T = void >
125 template< typename T >
126 struct enable_if<true, T> { typedef T type; };
128 template< typename T >
129 struct is_const : public false_type {};
131 template< typename T >
132 struct is_const<T const> : public true_type {};
134 template< bool, typename, typename >
137 template< bool C, typename T_TRUE, typename T_FALSE >
138 struct conditional { typedef T_TRUE type; };
140 template< typename T_TRUE, typename T_FALSE >
141 struct conditional< false, T_TRUE, T_FALSE > { typedef T_FALSE type; };
144 struct is_enum : integral_constant<bool, __is_enum(T)> {};
147 template< typename T > struct is_integral : public false_type {};
149 template<> struct is_integral<bool> : public true_type {};
151 template<> struct is_integral<char> : public true_type {};
152 template<> struct is_integral<signed char> : public true_type {};
153 template<> struct is_integral<unsigned char> : public true_type {};
154 template<> struct is_integral<short> : public true_type {};
155 template<> struct is_integral<unsigned short> : public true_type {};
156 template<> struct is_integral<int> : public true_type {};
157 template<> struct is_integral<unsigned int> : public true_type {};
158 template<> struct is_integral<long> : public true_type {};
159 template<> struct is_integral<unsigned long> : public true_type {};
160 template<> struct is_integral<long long> : public true_type {};
161 template<> struct is_integral<unsigned long long> : public true_type {};
163 template< typename T, bool = is_integral<T>::value || is_enum<T>::value >
164 struct __is_signed_helper : integral_constant<bool, static_cast<bool>(T(-1) < T(0))> {};
166 template< typename T >
167 struct __is_signed_helper<T, false> : integral_constant<bool, false> {};
169 template< typename T >
170 struct is_signed : __is_signed_helper<T> {};
173 template< int SIZE, bool SIGN = false, bool = true > struct int_type_for_size;
175 template<> struct int_type_for_size<sizeof(char), true, true>
176 { typedef signed char type; };
178 template<> struct int_type_for_size<sizeof(char), false, true>
179 { typedef unsigned char type; };
181 template<> struct int_type_for_size<sizeof(short), true, (sizeof(short) > sizeof(char))>
182 { typedef short type; };
184 template<> struct int_type_for_size<sizeof(short), false, (sizeof(short) > sizeof(char))>
185 { typedef unsigned short type; };
187 template<> struct int_type_for_size<sizeof(int), true, (sizeof(int) > sizeof(short))>
188 { typedef int type; };
190 template<> struct int_type_for_size<sizeof(int), false, (sizeof(int) > sizeof(short))>
191 { typedef unsigned int type; };
193 template<> struct int_type_for_size<sizeof(long), true, (sizeof(long) > sizeof(int))>
194 { typedef long type; };
196 template<> struct int_type_for_size<sizeof(long), false, (sizeof(long) > sizeof(int))>
197 { typedef unsigned long type; };
199 template<> struct int_type_for_size<sizeof(long long), true, (sizeof(long long) > sizeof(long))>
200 { typedef long long type; };
202 template<> struct int_type_for_size<sizeof(long long), false, (sizeof(long long) > sizeof(long))>
203 { typedef unsigned long long type; };
205 template< typename T, class Enable = void > struct underlying_type {};
207 template< typename T >
208 struct underlying_type<T, typename enable_if<is_enum<T>::value>::type >
210 typedef typename int_type_for_size<sizeof(T), is_signed<T>::value>::type type;