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 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
194 template<typename DT>
196 {
198 friend constexpr DT operator | (DT l, DT r)
199 { return DT(l.raw | r.raw); }
200
202 friend constexpr DT operator & (DT l, DT r)
203 { return DT(l.raw & r.raw); }
204
206 friend constexpr DT operator - (DT l, DT r)
207 { return DT(l.raw & ~r.raw); }
208
210 friend constexpr bool operator == (DT l, DT r)
211 { return l.raw == r.raw; }
212
214 friend constexpr bool operator != (DT l, DT r)
215 { return l.raw != r.raw; }
216
218 constexpr DT &operator |= (DT const r)
219 {
220 static_cast<DT *>(this)->raw |= r.raw;
221 return *static_cast<DT *>(this);
222 }
223
225 constexpr DT &operator &= (DT const r)
226 {
227 static_cast<DT *>(this)->raw &= r.raw;
228 return *static_cast<DT *>(this);
229 }
230
232 constexpr DT &operator -= (DT const r)
233 {
234 static_cast<DT *>(this)->raw &= ~r.raw;
235 return *static_cast<DT *>(this);
236 }
237
239 explicit constexpr operator bool () const
240 { return static_cast<DT const *>(this)->raw != 0; }
241
243 constexpr DT operator ~ () const
244 { return DT(~static_cast<DT const *>(this)->raw); }
245 };
246
255 template<typename DT, typename T>
256 struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
257 {
261 Flags_t() = default;
263 explicit constexpr Flags_t(T f) : raw(f) {}
264 };
265
267 template<typename...> using Void = void;
268
270 template<typename T, typename = void> struct __Add_rvalue_reference_helper
271 { using type = T; };
272
275 { using type = T &&; };
276
278 template<typename T> struct Add_rvalue_reference
279 { using type = typename __Add_rvalue_reference_helper<T>::type; };
280
282 template<typename T> using Add_rvalue_reference_t
283 = typename Add_rvalue_reference<T>::type;
284
293 template<typename T> Add_rvalue_reference_t<T> declval() noexcept;
294
299 */
300 template< bool V > struct Bool
302 typedef Bool<V> type;
303 enum { value = V };
304 };
305
308 struct False : Bool<false> {};
309
312 struct True : Bool<true> {};
313
315 template<typename T, T Value>
316 struct Integral_constant
317 {
318 static T const value = Value;
319
320 typedef T value_type;
321 typedef Integral_constant<T, Value> type;
322 };
323
335 template<typename T>
336 struct Is_enum : Integral_constant<bool, __is_enum(T)> {};
337
349 template<typename T, bool = Is_enum<T>::value>
350 struct __Underlying_type_helper { using type = __underlying_type(T); };
351
353 template<typename T> struct __Underlying_type_helper<T, false> {};
354
355 /// Get an underlying type of an enumeration type.
356 template<typename T> struct Underlying_type
357 : public __Underlying_type_helper<T> {};
358
360 template<typename T> using Underlying_type_t
361 = typename Underlying_type<T>::type;
362
363 /*********************/
372 template<typename A, typename B>
373 struct Same : False {};
374
375 template<typename A>
376 struct Same<A, A> : True {};
377
378 template<bool, typename = void> struct Enable_if {};
379 template<typename T> struct Enable_if<true, T> { typedef T type; };
380
382 template<bool Condition, typename T = void>
383 using Enable_if_t = typename Enable_if<Condition, T>::type;
384
385 template<typename T1, typename T2, typename T = void>
386 struct Enable_if_same : Enable_if<Same<T1, T2>::value, T> {};
387
388 template<typename T> struct Remove_const { typedef T type; };
389 template<typename T> struct Remove_const<T const> { typedef T type; };
390 template<typename T> struct Remove_volatile { typedef T type; };
391 template<typename T> struct Remove_volatile<T volatile> { typedef T type; };
392 template<typename T> struct Remove_cv
393 { typedef typename Remove_const<typename Remove_volatile<T>::type>::type type; };
394
395 template<typename T> struct Remove_pointer { typedef T type; };
396 template<typename T> struct Remove_pointer<T*> { typedef T type; };
397 template<typename T> struct Remove_reference { typedef T type; };
398 template<typename T> struct Remove_reference<T&> { typedef T type; };
399 template<typename T> struct Remove_pr { typedef T type; };
400 template<typename T> struct Remove_pr<T&> { typedef T type; };
401 template<typename T> struct Remove_pr<T*> { typedef T type; };
402} // Types
403
404} // L4
405
417namespace Enum_bitops {
418 using namespace L4::Types;
419
421 template<typename, typename = void> struct Has_marker : False {};
422
424 template<typename T>
425 struct Has_marker<T, Void<decltype(enum_bitops_enable(declval<T>()))>>
426 : True {};
427
429 template<typename T>
430 struct Enable
431 : Integral_constant<bool, Is_enum<T>::value && Has_marker<T>::value> {};
432} // Enum_bitops
433
445inline namespace Enum_bitops_impl {
447 template<typename T,
449 constexpr L4::Types::Underlying_type_t<T> to_underlying(T const arg) noexcept
450 { return static_cast<L4::Types::Underlying_type_t<T>>(arg); }
451
453 template<typename T,
454 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
455 constexpr T operator ~ (T const a) noexcept
456 { return static_cast<T>(~to_underlying(a)); }
457
459 template<typename T,
460 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
461 constexpr T operator & (T l, T r) noexcept
462 { return static_cast<T>(to_underlying(l) & to_underlying(r)); }
463
465 template<typename T,
466 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
467 constexpr T &operator &= (T &a, T const b) noexcept
468 {
469 a = a & b;
470 return a;
471 }
472
474 template<typename T,
475 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
476 constexpr T operator | (T l, T r) noexcept
477 { return static_cast<T>(to_underlying(l) | to_underlying(r)); }
478
480 template<typename T,
481 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
482 constexpr T &operator |= (T &a, T const b) noexcept
483 {
484 a = a | b;
485 return a;
486 }
487
489 template<typename T,
490 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
491 constexpr T operator - (T l, T r) noexcept
492 { return static_cast<T>(to_underlying(l) & ~to_underlying(r)); }
493
495 template<typename T,
496 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
497 constexpr T &operator -= (T &a, T const b) noexcept
498 {
499 a = a - b;
500 return a;
501 }
502} // Enum_bitops_impl
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
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
Bitwise operators on enumeration types.
Definition types:445
constexpr T & operator|=(T &a, T const b) noexcept
Union and assign enum values.
Definition types:482
constexpr T operator-(T l, T r) noexcept
Difference (intersect with negation, clear bits) enum values.
Definition types:491
constexpr T operator~(T const a) noexcept
Negate enum value.
Definition types:455
constexpr T operator|(T l, T r) noexcept
Union enum values.
Definition types:476
constexpr L4::Types::Underlying_type_t< T > to_underlying(T const arg) noexcept
Convert enum value to its underlying type value.
Definition types:449
constexpr T operator&(T l, T r) noexcept
Intersect enum values.
Definition types:461
constexpr T & operator&=(T &a, T const b) noexcept
Intersect and assign enum values.
Definition types:467
constexpr T & operator-=(T &a, T const b) noexcept
Difference (intersect with negation, clear bits) and assign enum values.
Definition types:497
Mechanism to opt-in for enum bitwise operators.
Definition types:417
L4 basic type helpers for C++.
Definition types:20
typename Underlying_type< T >::type Underlying_type_t
Helper type for Underlying_type.
Definition types:359
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:267
typename Enable_if< Condition, T >::type Enable_if_t
Helper type for Enable_if.
Definition types:381
typename Add_rvalue_reference< T >::type Add_rvalue_reference_t
Helper type for the Add_rvalue_reference.
Definition types:282
L4 low-level kernel interface.
Check whether the given enum type opts in for the bitwise operators.
Definition types:431
Marker for the opt-in ADL function.
Definition types:421
Create an rvalue reference of the given type.
Definition types:279
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:196
constexpr DT & operator|=(DT const r)
bitwise or assignment for DT
Definition types:218
friend constexpr DT operator-(DT l, DT r)
Bitwise difference (clear bits) for DT.
Definition types:206
friend constexpr bool operator!=(DT l, DT r)
inequality for DT
Definition types:214
constexpr DT operator~() const
bitwise negation for DT
Definition types:243
constexpr DT & operator&=(DT const r)
bitwise and assignment for DT
Definition types:225
friend constexpr DT operator&(DT l, DT r)
bitwise and for DT
Definition types:202
constexpr DT & operator-=(DT const r)
Bitwise difference (clear bits) assignment for DT.
Definition types:232
friend constexpr DT operator|(DT l, DT r)
bitwise or for DT
Definition types:198
friend constexpr bool operator==(DT l, DT r)
equality for DT
Definition types:210
Flags_t()=default
Default (uninitializing) constructor.
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:263
T raw
Raw integral value.
Definition types:259
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
Wrapper for a static constant of the given type.
Definition types:316
Check whether the given type is an enumeration type.
Definition types:335
Compare two data types for equality.
Definition types:371
True meta value.
Definition types:311
Get an underlying type of an enumeration type.
Definition types:356
Helper template for Add_rvalue_reference.
Definition types:271
Helper template for Underlying_type.
Definition types:349