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 using value_type = UNDERLYING;
58 using bits_enum_type = BITS_ENUM;
60 using type = Flags<BITS_ENUM, UNDERLYING>;
61
62 private:
63 value_type _v;
64 explicit constexpr Flags(value_type v) noexcept : _v(v) {}
65
66 public:
68 enum None_type { None };
69
78 constexpr Flags(None_type) noexcept : _v(0) {}
79
81 constexpr Flags() noexcept : _v(0) {}
82
91 constexpr Flags(BITS_ENUM e) noexcept
92 : _v(static_cast<value_type>(1) << e)
93 {}
94
100 static constexpr type from_raw(value_type v) noexcept { return type(v); }
101
103 explicit constexpr operator bool () const noexcept
104 { return _v != 0; }
105
107 constexpr bool operator ! () const noexcept { return _v == 0; }
108
110 friend constexpr type operator | (type lhs, type rhs) noexcept
111 { return type(lhs._v | rhs._v); }
112
114 friend constexpr type operator | (type lhs, bits_enum_type rhs) noexcept
115 { return lhs | type(rhs); }
116
118 friend constexpr type operator & (type lhs, type rhs) noexcept
119 { return type(lhs._v & rhs._v); }
120
122 friend constexpr type operator & (type lhs, bits_enum_type rhs) noexcept
123 { return lhs & type(rhs); }
124
126 constexpr type &operator |= (type rhs) noexcept
127 { _v |= rhs._v; return *this; }
128
131 { return operator |= (type(rhs)); }
132
134 constexpr type &operator &= (type rhs) { _v &= rhs._v; return *this; }
135
138 { return operator &= (type(rhs)); }
139
141 constexpr type operator ~ () const noexcept { return type(~_v); }
142
149 constexpr type &clear(bits_enum_type flag) noexcept
150 { return operator &= (~type(flag)); }
151
153 constexpr value_type as_value() const noexcept { return _v; }
154 };
155
161 template<unsigned SIZE, bool = true> struct Int_for_size;
162
163 template<> struct Int_for_size<sizeof(unsigned char), true>
164 { using type = unsigned char; };
165
166 template<> struct Int_for_size<sizeof(unsigned short),
167 (sizeof(unsigned short) > sizeof(unsigned char))>
168 { using type = unsigned short; };
169
170 template<> struct Int_for_size<sizeof(unsigned),
171 (sizeof(unsigned) > sizeof(unsigned short))>
172 { using type = unsigned; };
173
174 template<> struct Int_for_size<sizeof(unsigned long),
175 (sizeof(unsigned long) > sizeof(unsigned))>
176 { using type = unsigned long; };
177
178 template<> struct Int_for_size<sizeof(unsigned long long),
179 (sizeof(unsigned long long) > sizeof(unsigned long))>
180 { using type = unsigned long long; };
181
188 template<typename T> struct Int_for_type
189 {
193 using type = typename Int_for_size<sizeof(T)>::type;
194 };
195
202 template<typename DT>
204 {
206 friend constexpr DT operator | (DT l, DT r)
207 { return DT(l.raw | r.raw); }
208
210 friend constexpr DT operator & (DT l, DT r)
211 { return DT(l.raw & r.raw); }
212
214 friend constexpr DT operator - (DT l, DT r)
215 { return DT(l.raw & ~r.raw); }
216
218 friend constexpr bool operator == (DT l, DT r)
219 { return l.raw == r.raw; }
220
222 friend constexpr bool operator != (DT l, DT r)
223 { return l.raw != r.raw; }
224
226 constexpr DT &operator |= (DT const r)
227 {
228 static_cast<DT *>(this)->raw |= r.raw;
229 return *static_cast<DT *>(this);
230 }
231
233 constexpr DT &operator &= (DT const r)
234 {
235 static_cast<DT *>(this)->raw &= r.raw;
236 return *static_cast<DT *>(this);
237 }
238
240 constexpr DT &operator -= (DT const r)
241 {
242 static_cast<DT *>(this)->raw &= ~r.raw;
243 return *static_cast<DT *>(this);
244 }
245
247 explicit constexpr operator bool () const
248 { return static_cast<DT const *>(this)->raw != 0; }
249
251 constexpr DT operator ~ () const
252 { return DT(~static_cast<DT const *>(this)->raw); }
253 };
254
263 template<typename DT, typename T>
264 struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
265 {
267 enum None_type { None };
268
270 T raw = 0;
272 constexpr Flags_t() = default;
274 constexpr Flags_t(None_type) {}
276 constexpr Flags_t(DT f) : raw(static_cast<T>(f)) {}
278 explicit constexpr Flags_t(T f) : raw(f) {}
279 };
280
282 template<typename...> using Void = void;
283
285 template<typename T, typename = void> struct __Add_rvalue_reference_helper
286 { using type = T; };
287
290 { using type = T &&; };
291
293 template<typename T> struct Add_rvalue_reference
294 { using type = typename __Add_rvalue_reference_helper<T>::type; };
295
297 template<typename T> using Add_rvalue_reference_t
298 = typename Add_rvalue_reference<T>::type;
299
308 template<typename T> Add_rvalue_reference_t<T> declval() noexcept;
309
314 */
315 template< bool V > struct Bool
317 using type = Bool<V>;
318 enum { value = V };
319 };
320
323 struct False : Bool<false> {};
324
327 struct True : Bool<true> {};
328
330 template<typename T, T Value>
331 struct Integral_constant
332 {
333 static T const value = Value;
334
335 typedef T value_type;
336 typedef Integral_constant<T, Value> type;
337 };
338
350 template<typename T>
351 struct Is_enum : Integral_constant<bool, __is_enum(T)> {};
352
361 template<typename T>
363 : Integral_constant<bool, __is_trivially_copyable(T)>
364 {};
365
366 template<typename T>
367 inline constexpr bool Is_trivially_copyable_v = __is_trivially_copyable(T);
368
380 template<typename T, bool = Is_enum<T>::value>
381 struct __Underlying_type_helper { using type = __underlying_type(T); };
382
384 template<typename T> struct __Underlying_type_helper<T, false> {};
385
386 /// Get an underlying type of an enumeration type.
387 template<typename T> struct Underlying_type
388 : public __Underlying_type_helper<T> {};
389
391 template<typename T> using Underlying_type_t
392 = typename Underlying_type<T>::type;
393
394 /*********************/
403 template<typename A, typename B>
404 struct Same : False {};
405
406 template<typename A>
407 struct Same<A, A> : True {};
408
409 template< typename T1, typename T2 >
410 inline constexpr bool Same_v = Same<T1, T2>::value;
411
413 template <typename T, template <typename...> typename Template>
414 struct Same_template : False {};
415
416 template <template <typename...> typename Template, typename... Args>
417 struct Same_template<Template<Args...>, Template> : True {};
418
419 template <typename T, template <typename...> typename Template>
420 inline constexpr bool Same_template_v = Same_template<T, Template>::value;
421
422 template<bool, typename = void> struct Enable_if {};
423 template<typename T> struct Enable_if<true, T> { using type = T; };
424
426 template<bool Condition, typename T = void>
427 using Enable_if_t = typename Enable_if<Condition, T>::type;
428
429 template<typename T1, typename T2, typename T = void>
430 struct Enable_if_same : Enable_if<Same_v<T1, T2>, T> {};
431
432 template<typename T> struct Remove_const { using type = T; };
433 template<typename T> struct Remove_const<T const> { using type = T; };
434 template<typename T> using Remove_const_t = typename Remove_const<T>::type;
435
436 template<typename T> struct Remove_volatile { using type = T; };
437 template<typename T> struct Remove_volatile<T volatile> { using type = T; };
438 template<typename T> using Remove_volatile_t = typename Remove_volatile<T>::type;
439
440 template<typename T> struct Remove_cv
441 { using type = Remove_const_t<Remove_volatile_t<T>>; };
442 template<typename T> using Remove_cv_t = typename Remove_cv<T>::type;
443
444 template<typename T> struct Remove_pointer { using type = T; };
445 template<typename T> struct Remove_pointer<T*> { using type = T; };
446 template<typename T> using Remove_pointer_t = typename Remove_pointer<T>::type;
447
448 template<typename T> struct Remove_reference { using type = T; };
449 template<typename T> struct Remove_reference<T&> { using type = T; };
450 template<typename T> using Remove_reference_t = typename Remove_reference<T>::type;
451
452 template<typename T> struct Remove_pr { using type = T; };
453 template<typename T> struct Remove_pr<T&> { using type = T; };
454 template<typename T> struct Remove_pr<T*> { using type = T; };
455 template<typename T> using Remove_pr_t = typename Remove_pr<T>::type;
456
457 template<typename T>
458 constexpr T &&
459 forward(Remove_reference_t<T> &t)
460 { return static_cast<T &&>(t); }
461
462 template<typename T>
463 constexpr T &&
464 forward(Remove_reference_t<T> &&t)
465 { return static_cast<T &&>(t); }
466
467 template<typename T>
468 constexpr Remove_reference_t<T> &&
469 move(T &&t) { return static_cast<Remove_reference_t<T> &&>(t); }
470
471 template< typename... > using Void_t = void;
472} // Types
473
474} // L4
475
487namespace Enum_bitops {
488 using namespace L4::Types;
489
491 template<typename, typename = void> struct Has_marker : False {};
492
494 template<typename T>
495 struct Has_marker<T, Void<decltype(enum_bitops_enable(declval<T>()))>>
496 : True {};
497
499 template<typename T>
500 struct Enable
501 : Integral_constant<bool, Is_enum<T>::value && Has_marker<T>::value> {};
502} // Enum_bitops
503
515inline namespace Enum_bitops_impl {
517 template<typename T,
519 constexpr L4::Types::Underlying_type_t<T> to_underlying(T const arg) noexcept
520 { return static_cast<L4::Types::Underlying_type_t<T>>(arg); }
521
523 template<typename T,
524 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
525 constexpr T operator ~ (T const a) noexcept
526 { return static_cast<T>(~to_underlying(a)); }
527
529 template<typename T,
530 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
531 constexpr T operator & (T l, T r) noexcept
532 { return static_cast<T>(to_underlying(l) & to_underlying(r)); }
533
535 template<typename T,
536 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
537 constexpr T &operator &= (T &a, T const b) noexcept
538 {
539 a = a & b;
540 return a;
541 }
542
544 template<typename T,
545 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
546 constexpr T operator | (T l, T r) noexcept
547 { return static_cast<T>(to_underlying(l) | to_underlying(r)); }
548
550 template<typename T,
551 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
552 constexpr T &operator |= (T &a, T const b) noexcept
553 {
554 a = a | b;
555 return a;
556 }
557
559 template<typename T,
560 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
561 constexpr T operator - (T l, T r) noexcept
562 { return static_cast<T>(to_underlying(l) & ~to_underlying(r)); }
563
565 template<typename T,
566 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
567 constexpr T &operator -= (T &a, T const b) noexcept
568 {
569 a = a - b;
570 return a;
571 }
572} // Enum_bitops_impl
constexpr Flags(None_type) noexcept
Make an empty bitmap.
Definition types:78
constexpr type & clear(bits_enum_type flag) noexcept
Clear the given flag.
Definition types:149
UNDERLYING value_type
type of the underlying value
Definition types:56
constexpr Flags() noexcept
Make default Flags.
Definition types:81
friend constexpr type operator&(type lhs, type rhs) noexcept
Support & of two compatible Flags types.
Definition types:118
Flags< BITS_ENUM, UNDERLYING > type
the Flags<> type itself
Definition types:60
BITS_ENUM bits_enum_type
enum type defining a name for each bit
Definition types:58
friend constexpr type operator|(type lhs, type rhs) noexcept
Support | of two compatible Flags types.
Definition types:110
constexpr type & operator&=(type rhs)
Support &= of two compatible Flags types.
Definition types:134
constexpr type & operator|=(type rhs) noexcept
Support |= of two compatible Flags types.
Definition types:126
constexpr Flags(BITS_ENUM e) noexcept
Make flags from bit name.
Definition types:91
constexpr bool operator!() const noexcept
Support for if (!flags) syntax (test for empty flags).
Definition types:107
constexpr type operator~() const noexcept
Support ~ for Flags types.
Definition types:141
static constexpr type from_raw(value_type v) noexcept
Make flags from a raw value of value_type.
Definition types:100
constexpr value_type as_value() const noexcept
Get the underlying value.
Definition types:153
Bitwise operators on enumeration types.
Definition types:515
constexpr T & operator|=(T &a, T const b) noexcept
Union and assign enum values.
Definition types:552
constexpr T operator-(T l, T r) noexcept
Difference (intersect with negation, clear bits) enum values.
Definition types:561
constexpr T operator~(T const a) noexcept
Negate enum value.
Definition types:525
constexpr T operator|(T l, T r) noexcept
Union enum values.
Definition types:546
constexpr L4::Types::Underlying_type_t< T > to_underlying(T const arg) noexcept
Convert enum value to its underlying type value.
Definition types:519
constexpr T operator&(T l, T r) noexcept
Intersect enum values.
Definition types:531
constexpr T & operator&=(T &a, T const b) noexcept
Intersect and assign enum values.
Definition types:537
constexpr T & operator-=(T &a, T const b) noexcept
Difference (intersect with negation, clear bits) and assign enum values.
Definition types:567
Mechanism to opt-in for enum bitwise operators.
Definition types:487
L4 basic type helpers for C++.
Definition limits:16
typename Underlying_type< T >::type Underlying_type_t
Helper type for Underlying_type.
Definition types:390
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:282
typename Enable_if< Condition, T >::type Enable_if_t
Helper type for Enable_if.
Definition types:425
typename Add_rvalue_reference< T >::type Add_rvalue_reference_t
Helper type for the Add_rvalue_reference.
Definition types:297
L4 low-level kernel interface.
Check whether the given enum type opts in for the bitwise operators.
Definition types:501
Marker for the opt-in ADL function.
Definition types:491
Create an rvalue reference of the given type.
Definition types:294
Boolean meta type.
Definition types:315
Bool< V > type
The meta type itself.
Definition types:316
False meta value.
Definition types:322
Mixin class to define a set of friend bitwise operators on DT.
Definition types:204
constexpr DT & operator|=(DT const r)
bitwise or assignment for DT
Definition types:226
friend constexpr DT operator-(DT l, DT r)
Bitwise difference (clear bits) for DT.
Definition types:214
friend constexpr bool operator!=(DT l, DT r)
inequality for DT
Definition types:222
constexpr DT operator~() const
bitwise negation for DT
Definition types:251
constexpr DT & operator&=(DT const r)
bitwise and assignment for DT
Definition types:233
friend constexpr DT operator&(DT l, DT r)
bitwise and for DT
Definition types:210
constexpr DT & operator-=(DT const r)
Bitwise difference (clear bits) assignment for DT.
Definition types:240
friend constexpr DT operator|(DT l, DT r)
bitwise or for DT
Definition types:206
friend constexpr bool operator==(DT l, DT r)
equality for DT
Definition types:218
constexpr Flags_t(None_type)
Constructor to create an empty value.
Definition types:274
constexpr Flags_t()=default
Default constructor with no flag set.
constexpr Flags_t(DT f)
Initialization from determinator type.
Definition types:276
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:278
Metafunction to get an unsigned integral type for the given size.
Definition types:161
Metafunction to get an integral type of the same size as T.
Definition types:189
typename Int_for_size< sizeof(T)>::type type
The resulting unsigned integer type with the size like T.
Definition types:193
Wrapper for a static constant of the given type.
Definition types:331
Check whether the given type is an enumeration type.
Definition types:350
Check whether the given type is trivially copyable.
Definition types:363
Check if a type T is an instantiation of a given template.
Definition types:412
Compare two data types for equality.
Definition types:402
True meta value.
Definition types:326
Get an underlying type of an enumeration type.
Definition types:387
Helper template for Add_rvalue_reference.
Definition types:286
Helper template for Underlying_type.
Definition types:380