L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7
9
10#pragma once
11
12// very simple type traits for basic L4 functions, for a more complete set
13// use <l4/cxx/type_traits> or the standard <type_traits>.
14
15namespace L4 {
16
20namespace Types {
21
51 template<typename BITS_ENUM, typename UNDERLYING = unsigned long>
52 class Flags
53 {
54 public:
56 typedef UNDERLYING value_type;
58 typedef BITS_ENUM bits_enum_type;
61
62 private:
63 value_type _v;
64 explicit Flags(value_type v) : _v(v) {}
65
66 public:
68 enum None_type { None };
69
78 Flags(None_type) : _v(0) {}
79
81 Flags() : _v(0) {}
82
91 Flags(BITS_ENUM e) : _v((value_type{1}) << e) {}
92
98 static type from_raw(value_type v) { return type(v); }
99
101 explicit operator bool () const
102 { return _v != 0; }
103
105 bool operator ! () const { return _v == 0; }
106
108 friend type operator | (type lhs, type rhs)
109 { return type(lhs._v | rhs._v); }
110
113 { return lhs | type(rhs); }
114
116 friend type operator & (type lhs, type rhs)
117 { return type(lhs._v & rhs._v); }
118
121 { return lhs & type(rhs); }
122
124 type &operator |= (type rhs) { _v |= rhs._v; return *this; }
127
129 type &operator &= (type rhs) { _v &= rhs._v; return *this; }
132
134 type operator ~ () const { return type(~_v); }
135
142 type &clear(bits_enum_type flag) { return operator &= (~type(flag)); }
143
145 value_type as_value() const { return _v; }
146 };
147
153 template<unsigned SIZE, bool = true> struct Int_for_size;
154
155 template<> struct Int_for_size<sizeof(unsigned char), true>
156 { typedef unsigned char type; };
157
158 template<> struct Int_for_size<sizeof(unsigned short),
159 (sizeof(unsigned short) > sizeof(unsigned char))>
160 { typedef unsigned short type; };
161
162 template<> struct Int_for_size<sizeof(unsigned),
163 (sizeof(unsigned) > sizeof(unsigned short))>
164 { typedef unsigned type; };
165
166 template<> struct Int_for_size<sizeof(unsigned long),
167 (sizeof(unsigned long) > sizeof(unsigned))>
168 { typedef unsigned long type; };
169
170 template<> struct Int_for_size<sizeof(unsigned long long),
171 (sizeof(unsigned long long) > sizeof(unsigned long))>
172 { typedef unsigned long long type; };
173
180 template<typename T> struct Int_for_type
181 {
185 typedef typename Int_for_size<sizeof(T)>::type type;
186 };
187
195#define L4_TYPES_FLAGS_OPS_DEF(T) \
196 friend constexpr T operator ~ (T f) \
197 { \
198 return T(~static_cast<typename L4::Types::Int_for_type<T>::type>(f)); \
199 } \
200 \
201 friend constexpr T operator | (T l, T r) \
202 { \
203 return T(static_cast<typename L4::Types::Int_for_type<T>::type>(l) \
204 | static_cast<typename L4::Types::Int_for_type<T>::type>(r)); \
205 } \
206 \
207 friend constexpr T operator & (T l, T r) \
208 { \
209 return T(static_cast<typename L4::Types::Int_for_type<T>::type>(l) \
210 & static_cast<typename L4::Types::Int_for_type<T>::type>(r)); \
211 }
212
219 template<typename DT>
221 {
223 friend constexpr DT operator | (DT l, DT r)
224 { return DT(l.raw | r.raw); }
225
227 friend constexpr DT operator & (DT l, DT r)
228 { return DT(l.raw & r.raw); }
229
231 friend constexpr bool operator == (DT l, DT r)
232 { return l.raw == r.raw; }
233
235 friend constexpr bool operator != (DT l, DT r)
236 { return l.raw != r.raw; }
237
240 {
241 static_cast<DT *>(this)->raw |= r.raw;
242 return *static_cast<DT *>(this);
243 }
244
247 {
248 static_cast<DT *>(this)->raw &= r.raw;
249 return *static_cast<DT *>(this);
250 }
251
253 explicit constexpr operator bool () const
254 {
255 return static_cast<DT const *>(this)->raw != 0;
256 }
257
259 constexpr DT operator ~ () const
260 { return DT(~static_cast<DT const *>(this)->raw); }
261 };
262
271 template<typename DT, typename T>
272 struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
273 {
277 Flags_t() = default;
279 explicit constexpr Flags_t(T f) : raw(f) {}
280 };
281
282
288 template< bool V > struct Bool
289 {
290 typedef Bool<V> type;
291 enum { value = V };
292 };
293
296 struct False : Bool<false> {};
297
300 struct True : Bool<true> {};
301
302 /*********************/
311 template<typename A, typename B>
312 struct Same : False {};
313
314 template<typename A>
315 struct Same<A, A> : True {};
316
317 template<bool EXP, typename T = void> struct Enable_if {};
318 template<typename T> struct Enable_if<true, T> { typedef T type; };
319
320 template<typename T1, typename T2, typename T = void>
321 struct Enable_if_same : Enable_if<Same<T1, T2>::value, T> {};
322
323 template<typename T> struct Remove_const { typedef T type; };
324 template<typename T> struct Remove_const<T const> { typedef T type; };
325 template<typename T> struct Remove_volatile { typedef T type; };
326 template<typename T> struct Remove_volatile<T volatile> { typedef T type; };
327 template<typename T> struct Remove_cv
328 { typedef typename Remove_const<typename Remove_volatile<T>::type>::type type; };
329
330 template<typename T> struct Remove_pointer { typedef T type; };
331 template<typename T> struct Remove_pointer<T*> { typedef T type; };
332 template<typename T> struct Remove_reference { typedef T type; };
333 template<typename T> struct Remove_reference<T&> { typedef T type; };
334 template<typename T> struct Remove_pr { typedef T type; };
335 template<typename T> struct Remove_pr<T&> { typedef T type; };
336 template<typename T> struct Remove_pr<T*> { typedef T type; };
337} // Types
338} // L4
Template for defining typical Flags bitmaps.
Definition types:53
value_type as_value() const
Get the underlying value.
Definition types:145
static type from_raw(value_type v)
Make flags from a raw value of value_type.
Definition types:98
type & clear(bits_enum_type flag)
Clear the given flag.
Definition types:142
type operator~() const
Support ~ for Flags types.
Definition types:134
UNDERLYING value_type
type of the underlying value
Definition types:56
type & operator|=(type rhs)
Support |= of two compatible Flags types.
Definition types:124
Flags()
Make default Flags.
Definition types:81
friend type operator|(type lhs, type rhs)
Support | of two compatible Flags types.
Definition types:108
friend type operator&(type lhs, type rhs)
Support & of two compatible Flags types.
Definition types:116
None_type
The none type to get an empty bitmap.
Definition types:68
@ None
Use this to get an empty bitmap.
Definition types:68
BITS_ENUM bits_enum_type
enum type defining a name for each bit
Definition types:58
bool operator!() const
Support for if (!flags) syntax (test for empty flags).
Definition types:105
Flags(None_type)
Make an empty bitmap.
Definition types:78
Flags< BITS_ENUM, UNDERLYING > type
the Flags<> type itself
Definition types:60
Flags(BITS_ENUM e)
Make flags from bit name.
Definition types:91
type & operator&=(type rhs)
Support &= of two compatible Flags types.
Definition types:129
L4 low-level kernel interface.
Boolean meta type.
Definition types:289
Bool< V > type
The meta type itself.
Definition types:290
False meta value.
Definition types:296
Mixin class to define a set of friend bitwise operators on DT.
Definition types:221
DT operator|=(DT r)
bitwise or assignment for DT
Definition types:239
friend constexpr bool operator!=(DT l, DT r)
inequality for DT
Definition types:235
constexpr DT operator~() const
bitwise negation for DT
Definition types:259
friend constexpr DT operator&(DT l, DT r)
bitwise and for DT
Definition types:227
DT operator&=(DT r)
bitwise and assignment for DT
Definition types:246
friend constexpr DT operator|(DT l, DT r)
bitwise or for DT
Definition types:223
friend constexpr bool operator==(DT l, DT r)
equality for DT
Definition types:231
Template type to define a flags type with bitwise operations.
Definition types:273
Flags_t()=default
Default (uninitializing) costructor.
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:279
T raw
Raw integral value.
Definition types:275
Metafunction to get an unsigned integral type for the given size.
Definition types:153
Metafunction to get an integral type of the same size as T.
Definition types:181
Int_for_size< sizeof(T)>::type type
The resulting unsigned integer type with the size like T.
Definition types:185
Compare two data types for equality.
Definition types:312
True meta value.
Definition types:300