L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
types
Go to the documentation of this file.
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2014 Alexander Warg <alexander.warg@kernkonzept.com>
4 *
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
8 *
9 * As a special exception, you may use this file as part of a free software
10 * library without restriction. Specifically, if other files instantiate
11 * templates or use macros or inline functions from this file, or you compile
12 * this file and link it with other files to produce an executable, this
13 * file does not by itself cause the resulting executable to be covered by
14 * the GNU General Public License. This exception does not however
15 * invalidate any other reasons why the executable file might be covered by
16 * the GNU General Public License.
17 */
18
20
21#pragma once
22
23// very simple type traits for basic L4 functions, for a more complete set
24// use <l4/cxx/type_traits> or the standard <type_traits>.
25
26namespace L4 {
27
31namespace Types {
32
62 template<typename BITS_ENUM, typename UNDERLYING = unsigned long>
63 class Flags
64 {
65 public:
67 typedef UNDERLYING value_type;
69 typedef BITS_ENUM bits_enum_type;
72
73 private:
74 value_type _v;
75 explicit Flags(value_type v) : _v(v) {}
76
77 public:
79 enum None_type { None };
80
89 Flags(None_type) : _v(0) {}
90
92 Flags() : _v(0) {}
93
102 Flags(BITS_ENUM e) : _v((value_type{1}) << e) {}
103
109 static type from_raw(value_type v) { return type(v); }
110
112 explicit operator bool () const
113 { return _v != 0; }
114
116 bool operator ! () const { return _v == 0; }
117
119 friend type operator | (type lhs, type rhs)
120 { return type(lhs._v | rhs._v); }
121
124 { return lhs | type(rhs); }
125
127 friend type operator & (type lhs, type rhs)
128 { return type(lhs._v & rhs._v); }
129
132 { return lhs & type(rhs); }
133
135 type &operator |= (type rhs) { _v |= rhs._v; return *this; }
138
140 type &operator &= (type rhs) { _v &= rhs._v; return *this; }
143
145 type operator ~ () const { return type(~_v); }
146
153 type &clear(bits_enum_type flag) { return operator &= (~type(flag)); }
154
156 value_type as_value() const { return _v; }
157 };
158
164 template<unsigned SIZE, bool = true> struct Int_for_size;
165
166 template<> struct Int_for_size<sizeof(unsigned char), true>
167 { typedef unsigned char type; };
168
169 template<> struct Int_for_size<sizeof(unsigned short),
170 (sizeof(unsigned short) > sizeof(unsigned char))>
171 { typedef unsigned short type; };
172
173 template<> struct Int_for_size<sizeof(unsigned),
174 (sizeof(unsigned) > sizeof(unsigned short))>
175 { typedef unsigned type; };
176
177 template<> struct Int_for_size<sizeof(unsigned long),
178 (sizeof(unsigned long) > sizeof(unsigned))>
179 { typedef unsigned long type; };
180
181 template<> struct Int_for_size<sizeof(unsigned long long),
182 (sizeof(unsigned long long) > sizeof(unsigned long))>
183 { typedef unsigned long long type; };
184
191 template<typename T> struct Int_for_type
192 {
196 typedef typename Int_for_size<sizeof(T)>::type type;
197 };
198
206#define L4_TYPES_FLAGS_OPS_DEF(T) \
207 friend constexpr T operator ~ (T f) \
208 { \
209 return T(~static_cast<typename L4::Types::Int_for_type<T>::type>(f)); \
210 } \
211 \
212 friend constexpr T operator | (T l, T r) \
213 { \
214 return T(static_cast<typename L4::Types::Int_for_type<T>::type>(l) \
215 | static_cast<typename L4::Types::Int_for_type<T>::type>(r)); \
216 } \
217 \
218 friend constexpr T operator & (T l, T r) \
219 { \
220 return T(static_cast<typename L4::Types::Int_for_type<T>::type>(l) \
221 & static_cast<typename L4::Types::Int_for_type<T>::type>(r)); \
222 }
223
230 template<typename DT>
232 {
234 friend constexpr DT operator | (DT l, DT r)
235 { return DT(l.raw | r.raw); }
236
238 friend constexpr DT operator & (DT l, DT r)
239 { return DT(l.raw & r.raw); }
240
242 friend constexpr bool operator == (DT l, DT r)
243 { return l.raw == r.raw; }
244
246 friend constexpr bool operator != (DT l, DT r)
247 { return l.raw != r.raw; }
248
251 {
252 static_cast<DT *>(this)->raw |= r.raw;
253 return *static_cast<DT *>(this);
254 }
255
258 {
259 static_cast<DT *>(this)->raw &= r.raw;
260 return *static_cast<DT *>(this);
261 }
262
264 explicit constexpr operator bool () const
265 {
266 return static_cast<DT const *>(this)->raw != 0;
267 }
268
270 constexpr DT operator ~ () const
271 { return DT(~static_cast<DT const *>(this)->raw); }
272 };
273
282 template<typename DT, typename T>
283 struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
284 {
288 Flags_t() = default;
290 explicit constexpr Flags_t(T f) : raw(f) {}
291 };
292
293
299 template< bool V > struct Bool
300 {
301 typedef Bool<V> type;
302 enum { value = V };
303 };
304
307 struct False : Bool<false> {};
308
311 struct True : Bool<true> {};
312
313 /*********************/
322 template<typename A, typename B>
323 struct Same : False {};
324
325 template<typename A>
326 struct Same<A, A> : True {};
327
328 template<bool EXP, typename T = void> struct Enable_if {};
329 template<typename T> struct Enable_if<true, T> { typedef T type; };
330
331 template<typename T1, typename T2, typename T = void>
332 struct Enable_if_same : Enable_if<Same<T1, T2>::value, T> {};
333
334 template<typename T> struct Remove_const { typedef T type; };
335 template<typename T> struct Remove_const<T const> { typedef T type; };
336 template<typename T> struct Remove_volatile { typedef T type; };
337 template<typename T> struct Remove_volatile<T volatile> { typedef T type; };
338 template<typename T> struct Remove_cv
339 { typedef typename Remove_const<typename Remove_volatile<T>::type>::type type; };
340
341 template<typename T> struct Remove_pointer { typedef T type; };
342 template<typename T> struct Remove_pointer<T*> { typedef T type; };
343 template<typename T> struct Remove_reference { typedef T type; };
344 template<typename T> struct Remove_reference<T&> { typedef T type; };
345 template<typename T> struct Remove_pr { typedef T type; };
346 template<typename T> struct Remove_pr<T&> { typedef T type; };
347 template<typename T> struct Remove_pr<T*> { typedef T type; };
348} // Types
349} // L4
Template for defining typical Flags bitmaps.
Definition types:64
value_type as_value() const
Get the underlying value.
Definition types:156
static type from_raw(value_type v)
Make flags from a raw value of value_type.
Definition types:109
type & clear(bits_enum_type flag)
Clear the given flag.
Definition types:153
type operator~() const
Support ~ for Flags types.
Definition types:145
UNDERLYING value_type
type of the underlying value
Definition types:67
type & operator|=(type rhs)
Support |= of two compatible Flags types.
Definition types:135
Flags()
Make default Flags.
Definition types:92
friend type operator|(type lhs, type rhs)
Support | of two compatible Flags types.
Definition types:119
friend type operator&(type lhs, type rhs)
Support & of two compatible Flags types.
Definition types:127
None_type
The none type to get an empty bitmap.
Definition types:79
@ None
Use this to get an empty bitmap.
Definition types:79
BITS_ENUM bits_enum_type
enum type defining a name for each bit
Definition types:69
bool operator!() const
Support for if (!flags) syntax (test for empty flags).
Definition types:116
Flags(None_type)
Make an empty bitmap.
Definition types:89
Flags< BITS_ENUM, UNDERLYING > type
the Flags<> type itself
Definition types:71
Flags(BITS_ENUM e)
Make flags from bit name.
Definition types:102
type & operator&=(type rhs)
Support &= of two compatible Flags types.
Definition types:140
L4 low-level kernel interface.
Definition io_regblock.h:19
Boolean meta type.
Definition types:300
Bool< V > type
The meta type itself.
Definition types:301
False meta value.
Definition types:307
Mixin class to define a set of friend bitwise operators on DT.
Definition types:232
DT operator|=(DT r)
bitwise or assignment for DT
Definition types:250
friend constexpr bool operator!=(DT l, DT r)
inequality for DT
Definition types:246
constexpr DT operator~() const
bitwise negation for DT
Definition types:270
friend constexpr DT operator&(DT l, DT r)
bitwise and for DT
Definition types:238
DT operator&=(DT r)
bitwise and assignment for DT
Definition types:257
friend constexpr DT operator|(DT l, DT r)
bitwise or for DT
Definition types:234
friend constexpr bool operator==(DT l, DT r)
equality for DT
Definition types:242
Template type to define a flags type with bitwise operations.
Definition types:284
Flags_t()=default
Default (uninitializing) costructor.
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:290
T raw
Raw integral value.
Definition types:286
Metafunction to get an unsigned integral type for the given size.
Definition types:164
Metafunction to get an integral type of the same size as T.
Definition types:192
Int_for_size< sizeof(T)>::type type
The resulting unsigned integer type with the size like T.
Definition types:196
Compare two data types for equality.
Definition types:323
True meta value.
Definition types:311