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