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