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