L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
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 * License: see LICENSE.spdx (in this directory or the directories above)
9 */
10
11
12#pragma once
13
14#pragma GCC system_header
15
16#include <l4/sys/compiler.h>
17#include "bits/type_traits.h"
18
19namespace cxx {
20
21template< typename T, T V >
22struct integral_constant
23{
24 static T const value = V;
25 typedef T value_type;
26 typedef integral_constant<T, V> type;
27};
28
29typedef integral_constant<bool, true> true_type;
30typedef integral_constant<bool, false> false_type;
31
32template<typename...>
33using void_t = void;
34
35template< typename T > struct remove_reference;
36
37template< typename T > struct identity { typedef T type; };
38template< typename T > using identity_t = typename identity<T>::type;
39
40template< typename T1, typename T2 > struct is_same;
41
42template< typename T > struct remove_const;
43
44template< typename T > struct remove_volatile;
45
46template< typename T > struct remove_cv;
47
48template< typename T > struct remove_pointer;
49
50template< typename T > struct remove_extent;
51
52template< typename T > struct remove_all_extents;
53
54
55
56template< typename, typename >
57struct is_same : false_type {};
58
59template< typename T >
60struct is_same<T, T> : true_type {};
61
62template< typename T1, typename T2 >
63inline constexpr bool is_same_v = is_same<T1, T2>::value;
64
65template< typename T >
66struct remove_reference { typedef T type; };
67
68template< typename T >
69struct remove_reference<T &> { typedef T type; };
70
71template< typename T >
72struct remove_reference<T &&> { typedef T type; };
73
74template< typename T >
75using remove_reference_t = typename remove_reference<T>::type;
76
77template< typename T > struct remove_const { typedef T type; };
78template< typename T > struct remove_const<T const> { typedef T type; };
79template< typename T > using remove_const_t = typename remove_const<T>::type;
80
81template< typename T > struct remove_volatile { typedef T type; };
82template< typename T > struct remove_volatile<T volatile> { typedef T type; };
83template< typename T > using remove_volatile_t = typename remove_volatile<T>::type;
84
85template< typename T >
86struct remove_cv { typedef remove_const_t<remove_volatile_t<T>> type; };
87
88template< typename T >
89using remove_cv_t = typename remove_cv<T>::type;
90
91template<class T>
92struct remove_cvref { using type = remove_cv_t<remove_reference_t<T>>; };
93
94template< typename T >
95using remove_cvref_t = typename remove_cvref<T>::type;
96
97template< typename T, typename >
98struct __remove_pointer_h { typedef T type; };
99
100template< typename T, typename I >
101struct __remove_pointer_h<T, I*> { typedef I type; };
102
103template< typename T >
104struct remove_pointer : __remove_pointer_h<T, remove_cv_t<T>> {};
105
106template< typename T >
107using remove_pointer_t = typename remove_pointer<T>::type;
108
109
110template< typename T >
111struct remove_extent { typedef T type; };
112
113template< typename T >
114struct remove_extent<T[]> { typedef T type; };
115
116template< typename T, unsigned long N >
117struct remove_extent<T[N]> { typedef T type; };
118
119template< typename T >
120using remove_extent_t = typename remove_extent<T>::type;
121
122
123template< typename T >
124struct remove_all_extents { typedef T type; };
125
126template< typename T >
127struct remove_all_extents<T[]> { typedef typename remove_all_extents<T>::type type; };
128
129template< typename T, unsigned long N >
130struct remove_all_extents<T[N]> { typedef typename remove_all_extents<T>::type type; };
131
132template< typename T >
133using remove_all_extents_t = typename remove_all_extents<T>::type;
134
135template< typename T >
136constexpr T &&
137forward(cxx::remove_reference_t<T> &t)
138{ return static_cast<T &&>(t); }
139
140template< typename T >
141constexpr T &&
142forward(cxx::remove_reference_t<T> &&t)
143{ return static_cast<T &&>(t); }
144
145template< typename T >
146constexpr cxx::remove_reference_t<T> &&
147move(T &&t) { return static_cast<cxx::remove_reference_t<T> &&>(t); }
148
149template< bool, typename T = void >
150struct enable_if {};
151
152template< typename T >
153struct enable_if<true, T> { typedef T type; };
154
155template< bool C, typename T = void >
156using enable_if_t = typename enable_if<C, T>::type;
157
158template< typename T >
159struct is_const : false_type {};
160
161template< typename T >
162struct is_const<T const> : true_type {};
163
164template< typename T >
165inline constexpr bool is_const_v = is_const<T>::value;
166
167template< typename T >
168struct is_volatile : false_type {};
169
170template< typename T >
171struct is_volatile<T volatile> : true_type {};
172
173template< typename T >
174inline constexpr bool is_volatile_v = is_volatile<T>::value;
175
176template< typename T >
177struct is_pointer : false_type {};
178
179template< typename T >
180struct is_pointer<T *> : true_type {};
181
182template< typename T >
183inline constexpr bool is_pointer_v = is_pointer<T>::value;
184
185template<class T>
186inline constexpr bool is_null_pointer_v = is_same_v<decltype(nullptr), remove_cv_t<T>>;
187
188template< typename T >
189struct is_reference : false_type {};
190
191template< typename T >
192struct is_reference<T &> : true_type {};
193
194template< typename T >
195struct is_reference<T &&> : true_type {};
196
197template< typename T >
198inline constexpr bool is_reference_v = is_reference<T>::value;
199
200template< bool, typename, typename >
201struct conditional;
202
203template< bool C, typename T_TRUE, typename T_FALSE >
204struct conditional { typedef T_TRUE type; };
205
206template< typename T_TRUE, typename T_FALSE >
207struct conditional< false, T_TRUE, T_FALSE > { typedef T_FALSE type; };
208
209template< bool C, typename T_TRUE, typename T_FALSE >
210using conditional_t = typename conditional<C, T_TRUE, T_FALSE>::type;
211
212template<typename T>
213struct is_enum : integral_constant<bool, __is_enum(T)> {};
214
215template< typename T >
216inline constexpr bool is_enum_v = is_enum<T>::value;
217
218template<typename T>
219struct is_polymorphic : cxx::integral_constant<bool, __is_polymorphic(T)> {};
220
221template< typename T > struct is_integral : false_type {};
222
223template<> struct is_integral<bool> : true_type {};
224
225template<> struct is_integral<char> : true_type {};
226template<> struct is_integral<signed char> : true_type {};
227template<> struct is_integral<unsigned char> : true_type {};
228template<> struct is_integral<short> : true_type {};
229template<> struct is_integral<unsigned short> : true_type {};
230template<> struct is_integral<int> : true_type {};
231template<> struct is_integral<unsigned int> : true_type {};
232template<> struct is_integral<long> : true_type {};
233template<> struct is_integral<unsigned long> : true_type {};
234template<> struct is_integral<long long> : true_type {};
235template<> struct is_integral<unsigned long long> : true_type {};
236
237template< typename T >
238inline constexpr bool is_integral_v = is_integral<T>::value;
239
240template< typename T, bool = is_integral_v<T> || is_enum_v<T> >
241struct __is_signed_helper : integral_constant<bool, static_cast<bool>(T(-1) < T(0))> {};
242
243template< typename T >
244struct __is_signed_helper<T, false> : integral_constant<bool, false> {};
245
246template< typename T >
247struct is_signed : __is_signed_helper<T> {};
248
249template< typename T >
250inline constexpr bool is_signed_v = is_signed<T>::value;
251
252
253template< typename >
254struct is_array : false_type {};
255
256template< typename T >
257struct is_array<T[]> : true_type {};
258
259template< typename T, unsigned long N >
260struct is_array<T[N]> : true_type {};
261
262template< typename T >
263inline constexpr bool is_array_v = is_array<T>::value;
264
265template< typename T, unsigned N >
266constexpr unsigned array_size(T const (&)[N]) { return N; }
267
268template< int SIZE, bool SIGN = false, bool = true > struct int_type_for_size;
269
270template<> struct int_type_for_size<sizeof(char), true, true>
271{ typedef signed char type; };
272
273template<> struct int_type_for_size<sizeof(char), false, true>
274{ typedef unsigned char type; };
275
276template<> struct int_type_for_size<sizeof(short), true, (sizeof(short) > sizeof(char))>
277{ typedef short type; };
278
279template<> struct int_type_for_size<sizeof(short), false, (sizeof(short) > sizeof(char))>
280{ typedef unsigned short type; };
281
282template<> struct int_type_for_size<sizeof(int), true, (sizeof(int) > sizeof(short))>
283{ typedef int type; };
284
285template<> struct int_type_for_size<sizeof(int), false, (sizeof(int) > sizeof(short))>
286{ typedef unsigned int type; };
287
288template<> struct int_type_for_size<sizeof(long), true, (sizeof(long) > sizeof(int))>
289{ typedef long type; };
290
291template<> struct int_type_for_size<sizeof(long), false, (sizeof(long) > sizeof(int))>
292{ typedef unsigned long type; };
293
294template<> struct int_type_for_size<sizeof(long long), true, (sizeof(long long) > sizeof(long))>
295{ typedef long long type; };
296
297template<> struct int_type_for_size<sizeof(long long), false, (sizeof(long long) > sizeof(long))>
298{ typedef unsigned long long type; };
299
300template< int SIZE, bool SIGN = false>
301using int_type_for_size_t = typename int_type_for_size<SIZE, SIGN>::type;
302
303template< typename T, class Enable = void > struct underlying_type {};
304
305template< typename T >
306struct underlying_type<T, typename enable_if<is_enum_v<T>>::type >
307{
308 typedef int_type_for_size_t<sizeof(T), is_signed_v<T>> type;
309};
310
311template< typename T >
312using underlying_type_t = typename underlying_type<T>::type;
313
314template< typename T > struct make_signed;
315template<> struct make_signed<char> { typedef signed char type; };
316template<> struct make_signed<unsigned char> { typedef signed char type; };
317template<> struct make_signed<signed char> { typedef signed char type; };
318template<> struct make_signed<unsigned int> { typedef signed int type; };
319template<> struct make_signed<signed int> { typedef signed int type; };
320template<> struct make_signed<unsigned long int> { typedef signed long int type; };
321template<> struct make_signed<signed long int> { typedef signed long int type; };
322template<> struct make_signed<unsigned long long int> { typedef signed long long int type; };
323template<> struct make_signed<signed long long int> { typedef signed long long int type; };
324template< typename T > using make_signed_t = typename make_signed<T>::type;
325
326template< typename T > struct make_unsigned;
327template<> struct make_unsigned<char> { typedef unsigned char type; };
328template<> struct make_unsigned<unsigned char> { typedef unsigned char type; };
329template<> struct make_unsigned<signed char> { typedef unsigned char type; };
330template<> struct make_unsigned<unsigned int> { typedef unsigned int type; };
331template<> struct make_unsigned<signed int> { typedef unsigned int type; };
332template<> struct make_unsigned<unsigned long int> { typedef unsigned long int type; };
333template<> struct make_unsigned<signed long int> { typedef unsigned long int type; };
334template<> struct make_unsigned<unsigned long long int> { typedef unsigned long long int type; };
335template<> struct make_unsigned<signed long long int> { typedef unsigned long long int type; };
336template< typename T > using make_unsigned_t = typename make_unsigned<T>::type;
337
338
339template<typename From, typename To>
340struct is_convertible
341{
342private:
343 struct _true { char x[2]; };
344 struct _false {};
345
346 static _true _helper(To const *);
347 static _false _helper(...);
348public:
349 enum
350 {
351 value = sizeof(_true) == sizeof(_helper(static_cast<From*>(0)))
352 ? true : false
353 };
354
355 typedef bool value_type;
356};
357
358template<typename From, typename To>
359inline constexpr bool is_convertible_v = is_convertible<From, To>::value;
360
361template< typename T >
362struct is_empty : integral_constant<bool, __is_empty(T)> {};
363
364template< typename T >
365inline constexpr bool is_empty_v = is_empty<T>::value;
366
367
368#if L4_HAS_BUILTIN(__is_function)
369 template < typename T >
370 struct is_function : integral_constant<bool, __is_function(T)> {};
371#else
372 template < typename T >
373 struct is_function : integral_constant<bool, !is_reference_v<T>
374 && !is_const_v<const T>> {};
375#endif
376
377template< typename T >
378inline constexpr bool is_function_v = is_function<T>::value;
379
380}
381
L4 compiler related defines.
Our C++ library.
Definition arith:11