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 T raw = 0;
269 constexpr Flags_t() = default;
271 constexpr Flags_t(DT f) : raw(static_cast<T>(f)) {}
273 explicit constexpr Flags_t(T f) : raw(f) {}
274
276 static constexpr Flags_t None = Flags_t();
277 };
278
280 template<typename...> using Void = void;
281
283 template<typename T, typename = void> struct __Add_rvalue_reference_helper
284 { using type = T; };
285
288 { using type = T &&; };
289
291 template<typename T> struct Add_rvalue_reference
292 { using type = typename __Add_rvalue_reference_helper<T>::type; };
293
295 template<typename T> using Add_rvalue_reference_t
296 = typename Add_rvalue_reference<T>::type;
297
306 template<typename T> Add_rvalue_reference_t<T> declval() noexcept;
307
312 */
313 template< bool V > struct Bool
315 using type = Bool<V>;
316 enum { value = V };
317 };
318
321 struct False : Bool<false> {};
322
325 struct True : Bool<true> {};
326
328 template<typename T, T Value>
329 struct Integral_constant
330 {
331 static T const value = Value;
332
333 typedef T value_type;
334 typedef Integral_constant<T, Value> type;
335 };
336
348 template<typename T>
349 struct Is_enum : Integral_constant<bool, __is_enum(T)> {};
350
362 template<typename T, bool = Is_enum<T>::value>
363 struct __Underlying_type_helper { using type = __underlying_type(T); };
364
366 template<typename T> struct __Underlying_type_helper<T, false> {};
367
368 /// Get an underlying type of an enumeration type.
369 template<typename T> struct Underlying_type
370 : public __Underlying_type_helper<T> {};
371
373 template<typename T> using Underlying_type_t
374 = typename Underlying_type<T>::type;
375
376 /*********************/
385 template<typename A, typename B>
386 struct Same : False {};
387
388 template<typename A>
389 struct Same<A, A> : True {};
390
391 template< typename T1, typename T2 >
392 inline constexpr bool Same_v = Same<T1, T2>::value;
393
395 template <typename T, template <typename...> typename Template>
396 struct Same_template : False {};
397
398 template <template <typename...> typename Template, typename... Args>
399 struct Same_template<Template<Args...>, Template> : True {};
400
401 template <typename T, template <typename...> typename Template>
402 inline constexpr bool Same_template_v = Same_template<T, Template>::value;
403
404 template<bool, typename = void> struct Enable_if {};
405 template<typename T> struct Enable_if<true, T> { using type = T; };
406
408 template<bool Condition, typename T = void>
409 using Enable_if_t = typename Enable_if<Condition, T>::type;
410
411 template<typename T1, typename T2, typename T = void>
412 struct Enable_if_same : Enable_if<Same_v<T1, T2>, T> {};
413
414 template<typename T> struct Remove_const { using type = T; };
415 template<typename T> struct Remove_const<T const> { using type = T; };
416 template<typename T> using Remove_const_t = typename Remove_const<T>::type;
417
418 template<typename T> struct Remove_volatile { using type = T; };
419 template<typename T> struct Remove_volatile<T volatile> { using type = T; };
420 template<typename T> using Remove_volatile_t = typename Remove_volatile<T>::type;
421
422 template<typename T> struct Remove_cv
423 { using type = Remove_const_t<Remove_volatile_t<T>>; };
424 template<typename T> using Remove_cv_t = typename Remove_cv<T>::type;
425
426 template<typename T> struct Remove_pointer { using type = T; };
427 template<typename T> struct Remove_pointer<T*> { using type = T; };
428 template<typename T> using Remove_pointer_t = typename Remove_pointer<T>::type;
429
430 template<typename T> struct Remove_reference { using type = T; };
431 template<typename T> struct Remove_reference<T&> { using type = T; };
432 template<typename T> using Remove_reference_t = typename Remove_reference<T>::type;
433
434 template<typename T> struct Remove_pr { using type = T; };
435 template<typename T> struct Remove_pr<T&> { using type = T; };
436 template<typename T> struct Remove_pr<T*> { using type = T; };
437 template<typename T> using Remove_pr_t = typename Remove_pr<T>::type;
438
439 template<typename T>
440 constexpr T &&
441 forward(Remove_reference_t<T> &t)
442 { return static_cast<T &&>(t); }
443
444 template<typename T>
445 constexpr T &&
446 forward(Remove_reference_t<T> &&t)
447 { return static_cast<T &&>(t); }
448
449 template<typename T>
450 constexpr Remove_reference_t<T> &&
451 move(T &&t) { return static_cast<Remove_reference_t<T> &&>(t); }
452
453 template< typename... > using Void_t = void;
454} // Types
455
456} // L4
457
469namespace Enum_bitops {
470 using namespace L4::Types;
471
473 template<typename, typename = void> struct Has_marker : False {};
474
476 template<typename T>
477 struct Has_marker<T, Void<decltype(enum_bitops_enable(declval<T>()))>>
478 : True {};
479
481 template<typename T>
482 struct Enable
483 : Integral_constant<bool, Is_enum<T>::value && Has_marker<T>::value> {};
484} // Enum_bitops
485
497inline namespace Enum_bitops_impl {
499 template<typename T,
501 constexpr L4::Types::Underlying_type_t<T> to_underlying(T const arg) noexcept
502 { return static_cast<L4::Types::Underlying_type_t<T>>(arg); }
503
505 template<typename T,
506 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
507 constexpr T operator ~ (T const a) noexcept
508 { return static_cast<T>(~to_underlying(a)); }
509
511 template<typename T,
512 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
513 constexpr T operator & (T l, T r) noexcept
514 { return static_cast<T>(to_underlying(l) & to_underlying(r)); }
515
517 template<typename T,
518 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
519 constexpr T &operator &= (T &a, T const b) noexcept
520 {
521 a = a & b;
522 return a;
523 }
524
526 template<typename T,
527 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
528 constexpr T operator | (T l, T r) noexcept
529 { return static_cast<T>(to_underlying(l) | to_underlying(r)); }
530
532 template<typename T,
533 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
534 constexpr T &operator |= (T &a, T const b) noexcept
535 {
536 a = a | b;
537 return a;
538 }
539
541 template<typename T,
542 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
543 constexpr T operator - (T l, T r) noexcept
544 { return static_cast<T>(to_underlying(l) & ~to_underlying(r)); }
545
547 template<typename T,
548 typename = L4::Types::Enable_if_t<Enum_bitops::Enable<T>::value>>
549 constexpr T &operator -= (T &a, T const b) noexcept
550 {
551 a = a - b;
552 return a;
553 }
554} // 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:497
constexpr T & operator|=(T &a, T const b) noexcept
Union and assign enum values.
Definition types:534
constexpr T operator-(T l, T r) noexcept
Difference (intersect with negation, clear bits) enum values.
Definition types:543
constexpr T operator~(T const a) noexcept
Negate enum value.
Definition types:507
constexpr T operator|(T l, T r) noexcept
Union enum values.
Definition types:528
constexpr L4::Types::Underlying_type_t< T > to_underlying(T const arg) noexcept
Convert enum value to its underlying type value.
Definition types:501
constexpr T operator&(T l, T r) noexcept
Intersect enum values.
Definition types:513
constexpr T & operator&=(T &a, T const b) noexcept
Intersect and assign enum values.
Definition types:519
constexpr T & operator-=(T &a, T const b) noexcept
Difference (intersect with negation, clear bits) and assign enum values.
Definition types:549
Mechanism to opt-in for enum bitwise operators.
Definition types:469
L4 basic type helpers for C++.
Definition limits:16
typename Underlying_type< T >::type Underlying_type_t
Helper type for Underlying_type.
Definition types:372
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:280
typename Enable_if< Condition, T >::type Enable_if_t
Helper type for Enable_if.
Definition types:407
typename Add_rvalue_reference< T >::type Add_rvalue_reference_t
Helper type for the Add_rvalue_reference.
Definition types:295
L4 low-level kernel interface.
Check whether the given enum type opts in for the bitwise operators.
Definition types:483
Marker for the opt-in ADL function.
Definition types:473
Create an rvalue reference of the given type.
Definition types:292
Boolean meta type.
Definition types:313
Bool< V > type
The meta type itself.
Definition types:314
False meta value.
Definition types:320
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()=default
Default (uninitializing) constructor.
constexpr Flags_t(DT f)
Initialization from determinator type.
Definition types:271
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:273
T raw
Raw integral value.
Definition types:267
static constexpr Flags_t None
Empty flags literal.
Definition types:276
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:329
Check whether the given type is an enumeration type.
Definition types:348
Check if a type T is an instantiation of a given template.
Definition types:394
Compare two data types for equality.
Definition types:384
True meta value.
Definition types:324
Get an underlying type of an enumeration type.
Definition types:369
Helper template for Add_rvalue_reference.
Definition types:284
Helper template for Underlying_type.
Definition types:362