]> 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 idendity { 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
57
58 template< typename, typename >
59 struct is_same : public false_type {};
60
61 template< typename T >
62 struct is_same<T, T> : public true_type {};
63
64 template< typename T >
65 struct remove_reference { typedef T type; };
66
67 template< typename T >
68 struct remove_reference<T &> { typedef T type; };
69
70 template< typename T >
71 struct remove_reference<T &&> { typedef T type; };
72
73
74 template< typename T > struct remove_const { typedef T type; };
75 template< typename T > struct remove_const<T const> { typedef T type; };
76
77 template< typename T > struct remove_volatile { typedef T type; };
78 template< typename T > struct remove_volatile<T volatile> { typedef T type; };
79
80 template< typename T >
81 struct remove_cv { typedef typename remove_const<typename remove_volatile<T>::type>::type type; };
82
83 template< typename T, typename >
84 struct __remove_pointer_h { typedef T type; };
85
86 template< typename T, typename I >
87 struct __remove_pointer_h<T, I*> { typedef I type; };
88
89 template< typename  T >
90 struct remove_pointer : public __remove_pointer_h<T, typename remove_cv<T>::type> {};
91
92 #if FIASCO_GCC_VERSION >= 405
93
94 template< typename T >
95 inline T &&
96 forward(typename cxx::remove_reference<T>::type &t)
97 { return static_cast<T &&>(t); }
98
99 template< typename T >
100 inline T &&
101 forward(typename cxx::remove_reference<T>::type &&t)
102 { return static_cast<T &&>(t); }
103
104 #else
105
106 template< typename T >
107 inline T &&
108 forward(typename cxx::idendity<T>::type &&t)
109 { return t; }
110
111 #endif
112
113 template< typename T >
114 inline typename cxx::remove_reference<T>::type &&
115 move(T &t) { return static_cast<typename cxx::remove_reference<T>::type &&>(t); }
116
117 template< bool, typename T = void >
118 struct enable_if {};
119
120 template< typename T >
121 struct enable_if<true, T> { typedef T type; };
122
123 template< typename T >
124 struct is_const : public false_type {};
125
126 template< typename T >
127 struct is_const<T const> : public true_type {};
128
129 template< bool, typename, typename >
130 struct conditional;
131
132 template< bool C, typename T_TRUE, typename T_FALSE >
133 struct conditional { typedef T_TRUE type; };
134
135 template< typename T_TRUE, typename T_FALSE >
136 struct conditional< false, T_TRUE, T_FALSE > { typedef T_FALSE type; };
137
138 template<typename T>
139 struct is_enum : integral_constant<bool, __is_enum(T)> {};
140
141
142 template< typename T > struct is_integral : public false_type {};
143
144 template<> struct is_integral<bool> : public true_type {};
145
146 template<> struct is_integral<char> : public true_type {};
147 template<> struct is_integral<signed char> : public true_type {};
148 template<> struct is_integral<unsigned char> : public true_type {};
149 template<> struct is_integral<short> : public true_type {};
150 template<> struct is_integral<unsigned short> : public true_type {};
151 template<> struct is_integral<int> : public true_type {};
152 template<> struct is_integral<unsigned int> : public true_type {};
153 template<> struct is_integral<long> : public true_type {};
154 template<> struct is_integral<unsigned long> : public true_type {};
155 template<> struct is_integral<long long> : public true_type {};
156 template<> struct is_integral<unsigned long long> : public true_type {};
157
158 template< typename T, bool = is_integral<T>::value || is_enum<T>::value >
159 struct __is_signed_helper : integral_constant<bool, static_cast<bool>(T(-1) < T(0))> {};
160
161 template< typename T >
162 struct __is_signed_helper<T, false> : integral_constant<bool, false> {};
163
164 template< typename T >
165 struct is_signed : __is_signed_helper<T> {};
166
167
168 template< int SIZE, bool SIGN = false, bool = true > struct int_type_for_size;
169
170 template<> struct int_type_for_size<sizeof(char), true, true>
171 { typedef signed char type; };
172
173 template<> struct int_type_for_size<sizeof(char), false, true>
174 { typedef unsigned char type; };
175
176 template<> struct int_type_for_size<sizeof(short), true, (sizeof(short) > sizeof(char))>
177 { typedef short type; };
178
179 template<> struct int_type_for_size<sizeof(short), false, (sizeof(short) > sizeof(char))>
180 { typedef unsigned short type; };
181
182 template<> struct int_type_for_size<sizeof(int), true, (sizeof(int) > sizeof(short))>
183 { typedef int type; };
184
185 template<> struct int_type_for_size<sizeof(int), false, (sizeof(int) > sizeof(short))>
186 { typedef unsigned int type; };
187
188 template<> struct int_type_for_size<sizeof(long), true, (sizeof(long) > sizeof(int))>
189 { typedef long type; };
190
191 template<> struct int_type_for_size<sizeof(long), false, (sizeof(long) > sizeof(int))>
192 { typedef unsigned long type; };
193
194 template<> struct int_type_for_size<sizeof(long long), true, (sizeof(long long) > sizeof(long))>
195 { typedef long long type; };
196
197 template<> struct int_type_for_size<sizeof(long long), false, (sizeof(long long) > sizeof(long))>
198 { typedef unsigned long long type; };
199
200 template< typename T, class Enable = void > struct underlying_type {};
201
202 template< typename T >
203 struct underlying_type<T, typename enable_if<is_enum<T>::value>::type >
204 {
205   typedef typename int_type_for_size<sizeof(T), is_signed<T>::value>::type type;
206 };
207
208
209 }
210