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 * 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;
60 typedef Flags<BITS_ENUM, UNDERLYING> type;
61
62 private:
63 value_type _v;
64 explicit constexpr Flags(value_type v) : _v(v) {}
65
66 public:
68 enum None_type { None };
69
78 constexpr Flags(None_type) : _v(0) {}
79
81 constexpr Flags() : _v(0) {}
82
91 constexpr Flags(BITS_ENUM e) : _v((value_type{1}) << e) {}
92
98 static constexpr type from_raw(value_type v) { return type(v); }
99
101 explicit constexpr operator bool () const
102 { return _v != 0; }
103
105 constexpr bool operator ! () const { return _v == 0; }
106
108 friend constexpr type operator | (type lhs, type rhs)
109 { return type(lhs._v | rhs._v); }
110
112 friend constexpr type operator | (type lhs, bits_enum_type rhs)
113 { return lhs | type(rhs); }
114
116 friend constexpr type operator & (type lhs, type rhs)
117 { return type(lhs._v & rhs._v); }
118
120 friend constexpr type operator & (type lhs, bits_enum_type rhs)
121 { return lhs & type(rhs); }
122
124 constexpr type &operator |= (type rhs) { _v |= rhs._v; return *this; }
125
128 { return operator |= (type(rhs)); }
129
131 constexpr type &operator &= (type rhs) { _v &= rhs._v; return *this; }
132
135 { return operator &= (type(rhs)); }
136
138 constexpr type operator ~ () const { return type(~_v); }
139
146 constexpr type &clear(bits_enum_type flag)
147 { return operator &= (~type(flag)); }
148
150 constexpr value_type as_value() const { return _v; }
151 };
152
158 template<unsigned SIZE, bool = true> struct Int_for_size;
159
160 template<> struct Int_for_size<sizeof(unsigned char), true>
161 { typedef unsigned char type; };
162
163 template<> struct Int_for_size<sizeof(unsigned short),
164 (sizeof(unsigned short) > sizeof(unsigned char))>
165 { typedef unsigned short type; };
166
167 template<> struct Int_for_size<sizeof(unsigned),
168 (sizeof(unsigned) > sizeof(unsigned short))>
169 { typedef unsigned type; };
170
171 template<> struct Int_for_size<sizeof(unsigned long),
172 (sizeof(unsigned long) > sizeof(unsigned))>
173 { typedef unsigned long type; };
174
175 template<> struct Int_for_size<sizeof(unsigned long long),
176 (sizeof(unsigned long long) > sizeof(unsigned long))>
177 { typedef unsigned long long type; };
178
185 template<typename T> struct Int_for_type
186 {
190 typedef typename Int_for_size<sizeof(T)>::type type;
191 };
192
199 template<typename DT>
201 {
203 friend constexpr DT operator | (DT l, DT r)
204 { return DT(l.raw | r.raw); }
205
207 friend constexpr DT operator & (DT l, DT r)
208 { return DT(l.raw & r.raw); }
209
211 friend constexpr DT operator - (DT l, DT r)
212 { return DT(l.raw & ~r.raw); }
213
215 friend constexpr bool operator == (DT l, DT r)
216 { return l.raw == r.raw; }
217
219 friend constexpr bool operator != (DT l, DT r)
220 { return l.raw != r.raw; }
221
223 constexpr DT &operator |= (DT const r)
224 {
225 static_cast<DT *>(this)->raw |= r.raw;
226 return *static_cast<DT *>(this);
227 }
228
230 constexpr DT &operator &= (DT const r)
231 {
232 static_cast<DT *>(this)->raw &= r.raw;
233 return *static_cast<DT *>(this);
234 }
235
237 constexpr DT &operator -= (DT const r)
238 {
239 static_cast<DT *>(this)->raw &= ~r.raw;
240 return *static_cast<DT *>(this);
241 }
242
244 explicit constexpr operator bool () const
245 { return static_cast<DT const *>(this)->raw != 0; }
246
248 constexpr DT operator ~ () const
249 { return DT(~static_cast<DT const *>(this)->raw); }
250 };
251
260 template<typename DT, typename T>
261 struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
262 {
264 T raw = 0;
266 constexpr Flags_t() = default;
268 constexpr Flags_t(DT f) : raw(static_cast<T>(f)) {}
270 explicit constexpr Flags_t(T f) : raw(f) {}
271
273 static constexpr Flags_t None = Flags_t();
274 };
275
277 template<typename...> using Void = void;
278
280 template<typename T, typename = void> struct __Add_rvalue_reference_helper
281 { using type = T; };
282
285 { using type = T &&; };
286
288 template<typename T> struct Add_rvalue_reference
289 { using type = typename __Add_rvalue_reference_helper<T>::type; };
290
292 template<typename T> using Add_rvalue_reference_t
293 = typename Add_rvalue_reference<T>::type;
294
303 template<typename T> Add_rvalue_reference_t<T> declval() noexcept;
304
309 */
310 template< bool V > struct Bool
312 typedef Bool<V> type;
313 enum { value = V };
314 };
315
318 struct False : Bool<false> {};
319
322 struct True : Bool<true> {};
323
325 template<typename T, T Value>
326 struct Integral_constant
327 {
328 static T const value = Value;
329
330 typedef T value_type;
331 typedef Integral_constant<T, Value> type;
332 };
333
345 template<typename T>
346 struct Is_enum : Integral_constant<bool, __is_enum(T)> {};
347
359 template<typename T, bool = Is_enum<T>::value>
360 struct __Underlying_type_helper { using type = __underlying_type(T); };
361
363 template<typename T> struct __Underlying_type_helper<T, false> {};
364
365 /// Get an underlying type of an enumeration type.
366 template<typename T> struct Underlying_type
367 : public __Underlying_type_helper<T> {};
368
370 template<typename T> using Underlying_type_t
371 = typename Underlying_type<T>::type;
372
373 /*********************/
382 template<typename A, typename B>
383 struct Same : False {};
384
385 template<typename A>
386 struct Same<A, A> : True {};
387
388 template<bool, typename = void> struct Enable_if {};
389 template<typename T> struct Enable_if<true, T> { typedef T type; };
390
392 template<bool Condition, typename T = void>
393 using Enable_if_t = typename Enable_if<Condition, T>::type;
394
395 template<typename T1, typename T2, typename T = void>
396 struct Enable_if_same : Enable_if<Same<T1, T2>::value, T> {};
397
398 template<typename T> struct Remove_const { typedef T type; };
399 template<typename T> struct Remove_const<T const> { typedef T type; };
400 template<typename T> struct Remove_volatile { typedef T type; };
401 template<typename T> struct Remove_volatile<T volatile> { typedef T type; };
402 template<typename T> struct Remove_cv
403 { typedef typename Remove_const<typename Remove_volatile<T>::type>::type type; };
404
405 template<typename T> struct Remove_pointer { typedef T type; };
406 template<typename T> struct Remove_pointer<T*> { typedef T type; };
407 template<typename T> struct Remove_reference { typedef T type; };
408 template<typename T> struct Remove_reference<T&> { typedef T type; };
409 template<typename T> struct Remove_pr { typedef T type; };
410 template<typename T> struct Remove_pr<T&> { typedef T type; };
411 template<typename T> struct Remove_pr<T*> { typedef T type; };
412} // Types
413
414} // L4
415
427namespace Enum_bitops {
428 using namespace L4::Types;
429
431 template<typename, typename = void> struct Has_marker : False {};
432
434 template<typename T>
435 struct Has_marker<T, Void<decltype(enum_bitops_enable(declval<T>()))>>
436 : True {};
437
439 template<typename T>
440 struct Enable
441 : Integral_constant<bool, Is_enum<T>::value && Has_marker<T>::value> {};
442} // Enum_bitops
443
455inline namespace Enum_bitops_impl {
457 template<typename T,
459 constexpr L4::Types::Underlying_type_t<T> to_underlying(T const arg) noexcept
460 { return static_cast<L4::Types::Underlying_type_t<T>>(arg); }
461
463 template<typename T,
464 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
465 constexpr T operator ~ (T const a) noexcept
466 { return static_cast<T>(~to_underlying(a)); }
467
469 template<typename T,
470 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
471 constexpr T operator & (T l, T r) noexcept
472 { return static_cast<T>(to_underlying(l) & to_underlying(r)); }
473
475 template<typename T,
476 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
477 constexpr T &operator &= (T &a, T const b) noexcept
478 {
479 a = a & b;
480 return a;
481 }
482
484 template<typename T,
485 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
486 constexpr T operator | (T l, T r) noexcept
487 { return static_cast<T>(to_underlying(l) | to_underlying(r)); }
488
490 template<typename T,
491 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
492 constexpr T &operator |= (T &a, T const b) noexcept
493 {
494 a = a | b;
495 return a;
496 }
497
499 template<typename T,
500 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
501 constexpr T operator - (T l, T r) noexcept
502 { return static_cast<T>(to_underlying(l) & ~to_underlying(r)); }
503
505 template<typename T,
506 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
507 constexpr T &operator -= (T &a, T const b) noexcept
508 {
509 a = a - b;
510 return a;
511 }
512} // Enum_bitops_impl
constexpr Flags()
Make default Flags.
Definition types:81
constexpr type operator~() const
Support ~ for Flags types.
Definition types:138
constexpr Flags(None_type)
Make an empty bitmap.
Definition types:78
friend constexpr type operator&(type lhs, type rhs)
Support & of two compatible Flags types.
Definition types:116
UNDERLYING value_type
type of the underlying value
Definition types:56
constexpr value_type as_value() const
Get the underlying value.
Definition types:150
constexpr type & operator&=(type rhs)
Support &= of two compatible Flags types.
Definition types:131
constexpr bool operator!() const
Support for if (!flags) syntax (test for empty flags).
Definition types:105
friend constexpr type operator|(type lhs, type rhs)
Support | of two compatible Flags types.
Definition types:108
constexpr type & clear(bits_enum_type flag)
Clear the given flag.
Definition types:146
constexpr type & operator|=(type rhs)
Support |= of two compatible Flags types.
Definition types:124
BITS_ENUM bits_enum_type
enum type defining a name for each bit
Definition types:58
static constexpr type from_raw(value_type v)
Make flags from a raw value of value_type.
Definition types:98
Flags< BITS_ENUM, UNDERLYING > type
the Flags<> type itself
Definition types:60
constexpr Flags(BITS_ENUM e)
Make flags from bit name.
Definition types:91
Bitwise operators on enumeration types.
Definition types:455
constexpr T & operator|=(T &a, T const b) noexcept
Union and assign enum values.
Definition types:492
constexpr T operator-(T l, T r) noexcept
Difference (intersect with negation, clear bits) enum values.
Definition types:501
constexpr T operator~(T const a) noexcept
Negate enum value.
Definition types:465
constexpr T operator|(T l, T r) noexcept
Union enum values.
Definition types:486
constexpr L4::Types::Underlying_type_t< T > to_underlying(T const arg) noexcept
Convert enum value to its underlying type value.
Definition types:459
constexpr T operator&(T l, T r) noexcept
Intersect enum values.
Definition types:471
constexpr T & operator&=(T &a, T const b) noexcept
Intersect and assign enum values.
Definition types:477
constexpr T & operator-=(T &a, T const b) noexcept
Difference (intersect with negation, clear bits) and assign enum values.
Definition types:507
Mechanism to opt-in for enum bitwise operators.
Definition types:427
L4 basic type helpers for C++.
Definition limits:16
typename Underlying_type< T >::type Underlying_type_t
Helper type for Underlying_type.
Definition types:369
Add_rvalue_reference_t< T > declval() noexcept
Template for writing typed expressions in unevaluated contexts.
void Void
Map a sequence of any types to the void type.
Definition types:277
typename Enable_if< Condition, T >::type Enable_if_t
Helper type for Enable_if.
Definition types:391
typename Add_rvalue_reference< T >::type Add_rvalue_reference_t
Helper type for the Add_rvalue_reference.
Definition types:292
L4 low-level kernel interface.
Check whether the given enum type opts in for the bitwise operators.
Definition types:441
Marker for the opt-in ADL function.
Definition types:431
Create an rvalue reference of the given type.
Definition types:289
Boolean meta type.
Definition types:310
Bool< V > type
The meta type itself.
Definition types:311
False meta value.
Definition types:317
Mixin class to define a set of friend bitwise operators on DT.
Definition types:201
constexpr DT & operator|=(DT const r)
bitwise or assignment for DT
Definition types:223
friend constexpr DT operator-(DT l, DT r)
Bitwise difference (clear bits) for DT.
Definition types:211
friend constexpr bool operator!=(DT l, DT r)
inequality for DT
Definition types:219
constexpr DT operator~() const
bitwise negation for DT
Definition types:248
constexpr DT & operator&=(DT const r)
bitwise and assignment for DT
Definition types:230
friend constexpr DT operator&(DT l, DT r)
bitwise and for DT
Definition types:207
constexpr DT & operator-=(DT const r)
Bitwise difference (clear bits) assignment for DT.
Definition types:237
friend constexpr DT operator|(DT l, DT r)
bitwise or for DT
Definition types:203
friend constexpr bool operator==(DT l, DT r)
equality for DT
Definition types:215
constexpr Flags_t()=default
Default (uninitializing) constructor.
constexpr Flags_t(DT f)
Initialization from determinator type.
Definition types:268
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:270
T raw
Raw integral value.
Definition types:264
static constexpr Flags_t None
Empty flags literal.
Definition types:273
Metafunction to get an unsigned integral type for the given size.
Definition types:158
Metafunction to get an integral type of the same size as T.
Definition types:186
Int_for_size< sizeof(T)>::type type
The resulting unsigned integer type with the size like T.
Definition types:190
Wrapper for a static constant of the given type.
Definition types:326
Check whether the given type is an enumeration type.
Definition types:345
Compare two data types for equality.
Definition types:381
True meta value.
Definition types:321
Get an underlying type of an enumeration type.
Definition types:366
Helper template for Add_rvalue_reference.
Definition types:281
Helper template for Underlying_type.
Definition types:359