L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
type_traits.h
1// vi:ft=cpp
2/*
3 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>,
4 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
5 * economic rights: Technische Universität Dresden (Germany)
6 *
7 * License: see LICENSE.spdx (in this directory or the directories above)
8 */
9
10#pragma once
11
12namespace cxx {
13
14class Null_type;
15
16template<bool flag, typename T, typename F>
17struct Select { using Type = T; };
18
19template<typename T, typename F>
20struct Select<false, T, F> { using Type = F; };
21
22template<typename T, typename U>
23class Conversion
24{
25 using S = char;
26
27 class B { char dummy[2]; };
28
29 static S test(U);
30 static B test(...);
31 static T make_T();
32
33public:
34 static constexpr bool exists = sizeof(test(make_T())) == sizeof(S);
35 static constexpr bool two_way = exists && Conversion<U, T>::exists;
36 static constexpr bool exists_2_way = two_way;
37 static constexpr bool same_type = false;
38};
39
40template<>
41class Conversion<void, void>
42{
43public:
44 static constexpr bool exists = true;
45 static constexpr bool two_way = true;
46 static constexpr bool exists_2_way = two_way;
47 static constexpr bool same_type = true;
48};
49
50template<typename T>
51class Conversion<T, T>
52{
53public:
54 static constexpr bool exists = true;
55 static constexpr bool two_way = true;
56 static constexpr bool exists_2_way = two_way;
57 static constexpr bool same_type = true;
58};
59
60template<typename T>
61class Conversion<void, T>
62{
63public:
64 static constexpr bool exists = false;
65 static constexpr bool two_way = false;
66 static constexpr bool exists_2_way = two_way;
67 static constexpr bool same_type = false;
68};
69
70template<typename T>
71class Conversion<T, void>
72{
73public:
74 static constexpr bool exists = false;
75 static constexpr bool two_way = false;
76 static constexpr bool exists_2_way = two_way;
77 static constexpr bool same_type = false;
78};
79
80namespace TT
81{
82 template<typename U>
83 struct Pointer_traits
84 {
85 using Pointee = Null_type;
86 static constexpr bool value = false;
87 };
88
89 template<typename U>
90 struct Pointer_traits<U *>
91 {
92 using Pointee = U;
93 static constexpr bool value = true;
94 };
95
96 template<typename U>
97 struct Ref_traits
98 {
99 using Referee = U;
100 static constexpr bool value = false;
101 };
102
103 template<typename U>
104 struct Ref_traits<U &>
105 {
106 using Referee = U;
107 static constexpr bool value = true;
108 };
109
110 template<typename U>
111 struct Add_ref { using Type = U &; };
112
113 template<typename U>
114 struct Add_ref<U &> { using Type = U; };
115
116 template<typename U>
117 struct PMF_traits
118 { static constexpr bool value = false; };
119
120 template<typename U, typename F>
121 struct PMF_traits<U F:: *>
122 { static constexpr bool value = true; };
123
124 template<typename U>
125 struct Is_unsigned
126 { static constexpr bool value = false; };
127
128 template<>
129 struct Is_unsigned<unsigned>
130 { static constexpr bool value = true; };
131
132 template<>
133 struct Is_unsigned<unsigned char>
134 { static constexpr bool value = true; };
135
136 template<>
137 struct Is_unsigned<unsigned short>
138 { static constexpr bool value = true; };
139
140 template<>
141 struct Is_unsigned<unsigned long>
142 { static constexpr bool value = true; };
143
144 template<>
145 struct Is_unsigned<unsigned long long>
146 { static constexpr bool value = true; };
147
148 template<typename U>
149 struct Is_signed
150 { static constexpr bool value = false; };
151
152 template<>
153 struct Is_signed<signed>
154 { static constexpr bool value = true; };
155
156 template<>
157 struct Is_signed<signed char>
158 { static constexpr bool value = true; };
159
160 template<>
161 struct Is_signed<signed short>
162 { static constexpr bool value = true; };
163
164 template<>
165 struct Is_signed<signed long>
166 { static constexpr bool value = true; };
167
168 template<>
169 struct Is_signed<signed long long>
170 { static constexpr bool value = true; };
171
172 template<typename U>
173 struct Is_int
174 { static constexpr bool value = false; };
175
176 template<>
177 struct Is_int<char>
178 { static constexpr bool value = true; };
179
180 template<>
181 struct Is_int<bool>
182 { static constexpr bool value = true; };
183
184 template<>
185 struct Is_int<wchar_t>
186 { static constexpr bool value = true; };
187
188 template<typename U>
189 struct Is_float
190 { static constexpr bool value = false; };
191
192 template<>
193 struct Is_float<float>
194 { static constexpr bool value = true; };
195
196 template<>
197 struct Is_float<double>
198 { static constexpr bool value = true; };
199
200 template<>
201 struct Is_float<long double>
202 { static constexpr bool value = true; };
203
204 template<typename T>
205 struct Const_traits
206 {
207 using Type = T;
208 using Const_type = const T;
209 static constexpr bool value = false;
210 };
211
212 template<typename T>
213 struct Const_traits<const T>
214 {
215 using Type = T;
216 using Const_type = const T;
217 static constexpr bool value = true;
218 };
219};
220
221template<typename T>
222struct Type_traits
223{
224 static constexpr bool is_unsigned = TT::Is_unsigned<T>::value;
225 static constexpr bool is_signed = TT::Is_signed<T>::value;
226
227 static constexpr bool is_int = TT::Is_int<T>::value;
228 static constexpr bool is_float = TT::Is_float<T>::value;
229
230 static constexpr bool is_pointer = TT::Pointer_traits<T>::value;
231 static constexpr bool is_pointer_to_member = TT::PMF_traits<T>::value;
232 static constexpr bool is_reference = TT::Ref_traits<T>::value;
233
234 static constexpr bool is_scalar = is_unsigned || is_signed || is_int
235 || is_pointer || is_pointer_to_member || is_reference;
236
237 static constexpr bool is_fundamental = is_unsigned || is_signed || is_float
238 || Conversion<T, void>::same_type;
239
240 static constexpr bool is_const = TT::Const_traits<T>::value;
241
253 static constexpr unsigned long align(unsigned long address)
254 { return (address + alignof(T) - 1UL) & ~(alignof(T) - 1UL); }
255
256 using Param_type = typename Select<is_scalar, T, typename TT::Add_ref<typename TT::Const_traits<T>::Const_type>::Type>::Type;
257
258 using Pointee_type = typename TT::Pointer_traits<T>::Pointee;
259 using Referee_type = typename TT::Ref_traits<T>::Referee;
260 using Non_const_type = typename TT::Const_traits<T>::Type;
261 using Const_type = typename TT::Const_traits<T>::Const_type;
262};
263
264};
Our C++ library.
Definition arith:11