L4Re - L4 Runtime Environment
type_traits
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 
3 /*
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)
7  *
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.
11  *
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.
20  */
21 
22 
23 #pragma once
24 
25 #pragma GCC system_header
26 
27 #include "bits/type_traits.h"
28 
29 
30 #define CXX_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
31 
32 
33 namespace cxx {
34 
35 template< typename T, T V >
36 struct integral_constant
37 {
38  static T const value = V;
39  typedef T value_type;
40  typedef integral_constant<T, V> type;
41 };
42 
43 typedef integral_constant<bool, true> true_type;
44 typedef integral_constant<bool, false> false_type;
45 
46 template< typename T > struct remove_reference;
47 
48 template< typename T > struct idendity { typedef T type; };
49 
50 template< typename T1, typename T2 > struct is_same;
51 
52 template< typename T > struct remove_const;
53 
54 template< typename T > struct remove_volatile;
55 
56 template< typename T > struct remove_cv;
57 
58 template< typename T > struct remove_pointer;
59 
60 template< typename T > struct remove_extent;
61 
62 template< typename T > struct remove_all_extents;
63 
64 
65 
66 template< typename, typename >
67 struct is_same : false_type {};
68 
69 template< typename T >
70 struct is_same<T, T> : true_type {};
71 
72 template< typename T >
73 struct remove_reference { typedef T type; };
74 
75 template< typename T >
76 struct remove_reference<T &> { typedef T type; };
77 
78 #if __cplusplus >= 201103L
79 template< typename T >
80 struct remove_reference<T &&> { typedef T type; };
81 #endif
82 
83 template< typename T > struct remove_const { typedef T type; };
84 template< typename T > struct remove_const<T const> { typedef T type; };
85 
86 template< typename T > struct remove_volatile { typedef T type; };
87 template< typename T > struct remove_volatile<T volatile> { typedef T type; };
88 
89 template< typename T >
90 struct remove_cv { typedef typename remove_const<typename remove_volatile<T>::type>::type type; };
91 
92 template< typename T, typename >
93 struct __remove_pointer_h { typedef T type; };
94 
95 template< typename T, typename I >
96 struct __remove_pointer_h<T, I*> { typedef I type; };
97 
98 template< typename T >
99 struct remove_pointer : __remove_pointer_h<T, typename remove_cv<T>::type> {};
100 
101 
102 template< typename T >
103 struct remove_extent { typedef T type; };
104 
105 template< typename T >
106 struct remove_extent<T[]> { typedef T type; };
107 
108 template< typename T, unsigned long N >
109 struct remove_extent<T[N]> { typedef T type; };
110 
111 
112 template< typename T >
113 struct remove_all_extents { typedef T type; };
114 
115 template< typename T >
116 struct remove_all_extents<T[]> { typedef typename remove_all_extents<T>::type type; };
117 
118 template< typename T, unsigned long N >
119 struct remove_all_extents<T[N]> { typedef typename remove_all_extents<T>::type type; };
120 
121 #if __cplusplus >= 201103L
122 
123 template< typename T >
124 inline T &&
125 forward(typename cxx::remove_reference<T>::type &t)
126 { return static_cast<T &&>(t); }
127 
128 template< typename T >
129 inline T &&
130 forward(typename cxx::remove_reference<T>::type &&t)
131 { return static_cast<T &&>(t); }
132 
133 template< typename T >
134 inline typename cxx::remove_reference<T>::type &&
135 move(T &t) { return static_cast<typename cxx::remove_reference<T>::type &&>(t); }
136 #else
137 template< typename T >
138 inline T move(T t) { return t; }
139 #endif
140 
141 template< bool, typename T = void >
142 struct enable_if {};
143 
144 template< typename T >
145 struct enable_if<true, T> { typedef T type; };
146 
147 template< typename T >
148 struct is_const : false_type {};
149 
150 template< typename T >
151 struct is_const<T const> : true_type {};
152 
153 template< bool, typename, typename >
154 struct conditional;
155 
156 template< bool C, typename T_TRUE, typename T_FALSE >
157 struct conditional { typedef T_TRUE type; };
158 
159 template< typename T_TRUE, typename T_FALSE >
160 struct conditional< false, T_TRUE, T_FALSE > { typedef T_FALSE type; };
161 
162 template<typename T>
163 struct is_enum : integral_constant<bool, __is_enum(T)> {};
164 
165 template<typename T>
166 struct is_polymorphic : cxx::integral_constant<bool, __is_polymorphic(T)> {};
167 
168 template< typename T > struct is_integral : false_type {};
169 
170 template<> struct is_integral<bool> : true_type {};
171 
172 template<> struct is_integral<char> : true_type {};
173 template<> struct is_integral<signed char> : true_type {};
174 template<> struct is_integral<unsigned char> : true_type {};
175 template<> struct is_integral<short> : true_type {};
176 template<> struct is_integral<unsigned short> : true_type {};
177 template<> struct is_integral<int> : true_type {};
178 template<> struct is_integral<unsigned int> : true_type {};
179 template<> struct is_integral<long> : true_type {};
180 template<> struct is_integral<unsigned long> : true_type {};
181 template<> struct is_integral<long long> : true_type {};
182 template<> struct is_integral<unsigned long long> : true_type {};
183 
184 template< typename T, bool = is_integral<T>::value || is_enum<T>::value >
185 struct __is_signed_helper : integral_constant<bool, static_cast<bool>(T(-1) < T(0))> {};
186 
187 template< typename T >
188 struct __is_signed_helper<T, false> : integral_constant<bool, false> {};
189 
190 template< typename T >
191 struct is_signed : __is_signed_helper<T> {};
192 
193 
194 template< typename >
195 struct is_array : false_type {};
196 
197 template< typename T >
198 struct is_array<T[]> : true_type {};
199 
200 template< typename T, unsigned long N >
201 struct is_array<T[N]> : true_type {};
202 
203 template< typename T, unsigned N >
204 constexpr unsigned array_size(T const (&)[N]) { return N; }
205 
206 template< int SIZE, bool SIGN = false, bool = true > struct int_type_for_size;
207 
208 template<> struct int_type_for_size<sizeof(char), true, true>
209 { typedef signed char type; };
210 
211 template<> struct int_type_for_size<sizeof(char), false, true>
212 { typedef unsigned char type; };
213 
214 template<> struct int_type_for_size<sizeof(short), true, (sizeof(short) > sizeof(char))>
215 { typedef short type; };
216 
217 template<> struct int_type_for_size<sizeof(short), false, (sizeof(short) > sizeof(char))>
218 { typedef unsigned short type; };
219 
220 template<> struct int_type_for_size<sizeof(int), true, (sizeof(int) > sizeof(short))>
221 { typedef int type; };
222 
223 template<> struct int_type_for_size<sizeof(int), false, (sizeof(int) > sizeof(short))>
224 { typedef unsigned int type; };
225 
226 template<> struct int_type_for_size<sizeof(long), true, (sizeof(long) > sizeof(int))>
227 { typedef long type; };
228 
229 template<> struct int_type_for_size<sizeof(long), false, (sizeof(long) > sizeof(int))>
230 { typedef unsigned long type; };
231 
232 template<> struct int_type_for_size<sizeof(long long), true, (sizeof(long long) > sizeof(long))>
233 { typedef long long type; };
234 
235 template<> struct int_type_for_size<sizeof(long long), false, (sizeof(long long) > sizeof(long))>
236 { typedef unsigned long long type; };
237 
238 template< typename T, class Enable = void > struct underlying_type {};
239 
240 template< typename T >
241 struct underlying_type<T, typename enable_if<is_enum<T>::value>::type >
242 {
243  typedef typename int_type_for_size<sizeof(T), is_signed<T>::value>::type type;
244 };
245 
246 template< typename T > struct make_signed;
247 template<> struct make_signed<char> { typedef signed char type; };
248 template<> struct make_signed<unsigned char> { typedef signed char type; };
249 template<> struct make_signed<signed char> { typedef signed char type; };
250 template<> struct make_signed<unsigned int> { typedef signed int type; };
251 template<> struct make_signed<signed int> { typedef signed int type; };
252 template<> struct make_signed<unsigned long int> { typedef signed long int type; };
253 template<> struct make_signed<signed long int> { typedef signed long int type; };
254 template<> struct make_signed<unsigned long long int> { typedef signed long long int type; };
255 template<> struct make_signed<signed long long int> { typedef signed long long int type; };
256 
257 template< typename T > struct make_unsigned;
258 template<> struct make_unsigned<char> { typedef unsigned char type; };
259 template<> struct make_unsigned<unsigned char> { typedef unsigned char type; };
260 template<> struct make_unsigned<signed char> { typedef unsigned char type; };
261 template<> struct make_unsigned<unsigned int> { typedef unsigned int type; };
262 template<> struct make_unsigned<signed int> { typedef unsigned int type; };
263 template<> struct make_unsigned<unsigned long int> { typedef unsigned long int type; };
264 template<> struct make_unsigned<signed long int> { typedef unsigned long int type; };
265 template<> struct make_unsigned<unsigned long long int> { typedef unsigned long long int type; };
266 template<> struct make_unsigned<signed long long int> { typedef unsigned long long int type; };
267 
268 
269 template<typename From, typename To>
270 struct is_convertible
271 {
272 private:
273  struct _true { char x[2]; };
274  struct _false {};
275 
276  static _true _helper(To const *);
277  static _false _helper(...);
278 public:
279  enum
280  {
281  value = sizeof(_true) == sizeof(_helper(static_cast<From*>(0)))
282  ? true : false
283  };
284 
285  typedef bool value_type;
286 };
287 
288 }
289