]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/cxx/lib/tl/include/type_traits
update
[l4.git] / l4 / pkg / cxx / lib / tl / include / 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 #include "bits/type_traits.h"
26
27
28 #define CXX_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
29
30
31 namespace cxx {
32
33 template< typename T, T V >
34 struct integral_constant
35 {
36   static T const value = V;
37   typedef T value_type;
38   typedef integral_constant<T, V> type;
39 };
40
41 typedef integral_constant<bool, true> true_type;
42 typedef integral_constant<bool, false> false_type;
43
44 template< typename T > struct remove_reference;
45
46 template< typename T > struct idendity { typedef T type; };
47
48 template< typename T1, typename T2 > struct is_same;
49
50 template< typename T > struct remove_const;
51
52 template< typename T > struct remove_volatile;
53
54 template< typename T > struct remove_cv;
55
56 template< typename T > struct remove_pointer;
57
58
59
60 template< typename, typename >
61 struct is_same : public false_type {};
62
63 template< typename T >
64 struct is_same<T, T> : public true_type {};
65
66 template< typename T >
67 struct remove_reference { typedef T type; };
68
69 template< typename T >
70 struct remove_reference<T &> { typedef T type; };
71
72 #ifdef __GXX_EXPERIMENTAL_CXX0X__
73 template< typename T >
74 struct remove_reference<T &&> { typedef T type; };
75 #endif
76
77 template< typename T > struct remove_const { typedef T type; };
78 template< typename T > struct remove_const<T const> { typedef T type; };
79
80 template< typename T > struct remove_volatile { typedef T type; };
81 template< typename T > struct remove_volatile<T volatile> { typedef T type; };
82
83 template< typename T >
84 struct remove_cv { typedef typename remove_const<typename remove_volatile<T>::type>::type type; };
85
86 template< typename T, typename >
87 struct __remove_pointer_h { typedef T type; };
88
89 template< typename T, typename I >
90 struct __remove_pointer_h<T, I*> { typedef I type; };
91
92 template< typename  T >
93 struct remove_pointer : public __remove_pointer_h<T, typename remove_cv<T>::type> {};
94
95 #ifdef __GXX_EXPERIMENTAL_CXX0X__
96 #if CXX_GCC_VERSION >= 405
97
98 template< typename T >
99 inline T &&
100 forward(typename cxx::remove_reference<T>::type &t)
101 { return static_cast<T &&>(t); }
102
103 template< typename T >
104 inline T &&
105 forward(typename cxx::remove_reference<T>::type &&t)
106 { return static_cast<T &&>(t); }
107
108 #else
109
110 template< typename T >
111 inline T &&
112 forward(typename cxx::idendity<T>::type &&t)
113 { return t; }
114
115 #endif
116
117 template< typename T >
118 inline typename cxx::remove_reference<T>::type &&
119 move(T &t) { return static_cast<typename cxx::remove_reference<T>::type &&>(t); }
120 #endif
121
122 template< bool, typename T = void >
123 struct enable_if {};
124
125 template< typename T >
126 struct enable_if<true, T> { typedef T type; };
127
128 template< typename T >
129 struct is_const : public false_type {};
130
131 template< typename T >
132 struct is_const<T const> : public true_type {};
133
134 template< bool, typename, typename >
135 struct conditional;
136
137 template< bool C, typename T_TRUE, typename T_FALSE >
138 struct conditional { typedef T_TRUE type; };
139
140 template< typename T_TRUE, typename T_FALSE >
141 struct conditional< false, T_TRUE, T_FALSE > { typedef T_FALSE type; };
142
143 template<typename T>
144 struct is_enum : integral_constant<bool, __is_enum(T)> {};
145
146
147 template< typename T > struct is_integral : public false_type {};
148
149 template<> struct is_integral<bool> : public true_type {};
150
151 template<> struct is_integral<char> : public true_type {};
152 template<> struct is_integral<signed char> : public true_type {};
153 template<> struct is_integral<unsigned char> : public true_type {};
154 template<> struct is_integral<short> : public true_type {};
155 template<> struct is_integral<unsigned short> : public true_type {};
156 template<> struct is_integral<int> : public true_type {};
157 template<> struct is_integral<unsigned int> : public true_type {};
158 template<> struct is_integral<long> : public true_type {};
159 template<> struct is_integral<unsigned long> : public true_type {};
160 template<> struct is_integral<long long> : public true_type {};
161 template<> struct is_integral<unsigned long long> : public true_type {};
162
163 template< typename T, bool = is_integral<T>::value || is_enum<T>::value >
164 struct __is_signed_helper : integral_constant<bool, static_cast<bool>(T(-1) < T(0))> {};
165
166 template< typename T >
167 struct __is_signed_helper<T, false> : integral_constant<bool, false> {};
168
169 template< typename T >
170 struct is_signed : __is_signed_helper<T> {};
171
172
173 template< int SIZE, bool SIGN = false, bool = true > struct int_type_for_size;
174
175 template<> struct int_type_for_size<sizeof(char), true, true>
176 { typedef signed char type; };
177
178 template<> struct int_type_for_size<sizeof(char), false, true>
179 { typedef unsigned char type; };
180
181 template<> struct int_type_for_size<sizeof(short), true, (sizeof(short) > sizeof(char))>
182 { typedef short type; };
183
184 template<> struct int_type_for_size<sizeof(short), false, (sizeof(short) > sizeof(char))>
185 { typedef unsigned short type; };
186
187 template<> struct int_type_for_size<sizeof(int), true, (sizeof(int) > sizeof(short))>
188 { typedef int type; };
189
190 template<> struct int_type_for_size<sizeof(int), false, (sizeof(int) > sizeof(short))>
191 { typedef unsigned int type; };
192
193 template<> struct int_type_for_size<sizeof(long), true, (sizeof(long) > sizeof(int))>
194 { typedef long type; };
195
196 template<> struct int_type_for_size<sizeof(long), false, (sizeof(long) > sizeof(int))>
197 { typedef unsigned long type; };
198
199 template<> struct int_type_for_size<sizeof(long long), true, (sizeof(long long) > sizeof(long))>
200 { typedef long long type; };
201
202 template<> struct int_type_for_size<sizeof(long long), false, (sizeof(long long) > sizeof(long))>
203 { typedef unsigned long long type; };
204
205 template< typename T, class Enable = void > struct underlying_type {};
206
207 template< typename T >
208 struct underlying_type<T, typename enable_if<is_enum<T>::value>::type >
209 {
210   typedef typename int_type_for_size<sizeof(T), is_signed<T>::value>::type type;
211 };
212
213
214 }
215