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