L4Re – L4 Runtime Environment
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  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction. Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License. This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 
20 
21 #pragma once
22 
23 // very simple type traits for basic L4 functions, for a more complete set
24 // use <l4/cxx/type_traits> or the standard <type_traits>.
25 
26 namespace L4 {
27 
31 namespace Types {
32 
62  template<typename BITS_ENUM, typename UNDERLYING = unsigned long>
63  class Flags
64  {
65  public:
67  typedef UNDERLYING value_type;
69  typedef BITS_ENUM bits_enum_type;
72 
73  private:
74  struct Private_bool;
75  value_type _v;
76  explicit Flags(value_type v) : _v(v) {}
77 
78  public:
80  enum None_type { None };
81 
90  Flags(None_type) : _v(0) {}
91 
93  Flags() : _v(0) {}
94 
103  Flags(BITS_ENUM e) : _v(((value_type)1) << e) {}
104 
110  static type from_raw(value_type v) { return type(v); }
111 
113  operator Private_bool * () const
114  { return _v != 0 ? (Private_bool *)1 : 0; }
115 
117  bool operator ! () const { return _v == 0; }
118 
120  friend type operator | (type lhs, type rhs)
121  { return type(lhs._v | rhs._v); }
122 
125  { return lhs | type(rhs); }
126 
128  friend type operator & (type lhs, type rhs)
129  { return type(lhs._v & rhs._v); }
130 
133  { return lhs & type(rhs); }
134 
136  type &operator |= (type rhs) { _v |= rhs._v; return *this; }
139 
141  type &operator &= (type rhs) { _v &= rhs._v; return *this; }
144 
146  type operator ~ () const { return type(~_v); }
147 
154  type &clear(bits_enum_type flag) { return operator &= (~type(flag)); }
155 
157  value_type as_value() const { return _v; }
158  };
159 
165  template<unsigned SIZE, bool = true> struct Int_for_size;
166 
167  template<> struct Int_for_size<sizeof(unsigned char), true>
168  { typedef unsigned char type; };
169 
170  template<> struct Int_for_size<sizeof(unsigned short),
171  (sizeof(unsigned short) > sizeof(unsigned char))>
172  { typedef unsigned short type; };
173 
174  template<> struct Int_for_size<sizeof(unsigned),
175  (sizeof(unsigned) > sizeof(unsigned short))>
176  { typedef unsigned type; };
177 
178  template<> struct Int_for_size<sizeof(unsigned long),
179  (sizeof(unsigned long) > sizeof(unsigned))>
180  { typedef unsigned long type; };
181 
182  template<> struct Int_for_size<sizeof(unsigned long long),
183  (sizeof(unsigned long long) > sizeof(unsigned long))>
184  { typedef unsigned long long type; };
185 
192  template<typename T> struct Int_for_type
193  {
197  typedef typename Int_for_size<sizeof(T)>::type type;
198  };
199 
207 #define L4_TYPES_FLAGS_OPS_DEF(T) \
208  friend constexpr T operator ~ (T f) \
209  { \
210  return T(~((typename L4::Types::Int_for_type<T>::type)f)); \
211  } \
212  \
213  friend constexpr T operator | (T l, T r) \
214  { \
215  return T(((typename L4::Types::Int_for_type<T>::type)l) \
216  | ((typename L4::Types::Int_for_type<T>::type)r)); \
217  } \
218  \
219  friend constexpr T operator & (T l, T r) \
220  { \
221  return T(((typename L4::Types::Int_for_type<T>::type)l) \
222  & ((typename L4::Types::Int_for_type<T>::type)r)); \
223  }
224 
231  template<typename DT>
232  struct Flags_ops_t
233  {
235  friend constexpr DT operator | (DT l, DT r)
236  { return DT(l.raw | r.raw); }
237 
239  friend constexpr DT operator & (DT l, DT r)
240  { return DT(l.raw & r.raw); }
241 
243  friend constexpr bool operator == (DT l, DT r)
244  { return l.raw == r.raw; }
245 
247  friend constexpr bool operator != (DT l, DT r)
248  { return l.raw != r.raw; }
249 
251  DT operator |= (DT r)
252  {
253  static_cast<DT *>(this)->raw |= r.raw;
254  return *static_cast<DT *>(this);
255  }
256 
258  DT operator &= (DT r)
259  {
260  static_cast<DT *>(this)->raw &= r.raw;
261  return *static_cast<DT *>(this);
262  }
263 
265  explicit constexpr operator bool () const
266  {
267  return static_cast<DT const *>(this)->raw != 0;
268  }
269 
271  constexpr DT operator ~ () const
272  { return DT(~static_cast<DT const *>(this)->raw); }
273  };
274 
283  template<typename DT, typename T>
284  struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
285  {
287  T raw;
289  Flags_t() = default;
291  explicit constexpr Flags_t(T f) : raw(f) {}
292  };
293 
294 
300  template< bool V > struct Bool
301  {
302  typedef Bool<V> type;
303  enum { value = V };
304  };
305 
308  struct False : Bool<false> {};
309 
312  struct True : Bool<true> {};
313 
314  /*********************/
323  template<typename A, typename B>
324  struct Same : False {};
325 
326  template<typename A>
327  struct Same<A, A> : True {};
328 
329  template<bool EXP, typename T = void> struct Enable_if {};
330  template<typename T> struct Enable_if<true, T> { typedef T type; };
331 
332  template<typename T1, typename T2, typename T = void>
333  struct Enable_if_same : Enable_if<Same<T1, T2>::value, T> {};
334 
335  template<typename T> struct Remove_const { typedef T type; };
336  template<typename T> struct Remove_const<T const> { typedef T type; };
337  template<typename T> struct Remove_volatile { typedef T type; };
338  template<typename T> struct Remove_volatile<T volatile> { typedef T type; };
339  template<typename T> struct Remove_cv
340  { typedef typename Remove_const<typename Remove_volatile<T>::type>::type type; };
341 
342  template<typename T> struct Remove_pointer { typedef T type; };
343  template<typename T> struct Remove_pointer<T*> { typedef T type; };
344  template<typename T> struct Remove_reference { typedef T type; };
345  template<typename T> struct Remove_reference<T&> { typedef T type; };
346  template<typename T> struct Remove_pr { typedef T type; };
347  template<typename T> struct Remove_pr<T&> { typedef T type; };
348  template<typename T> struct Remove_pr<T*> { typedef T type; };
349 } // Types
350 } // L4
Template for defining typical Flags bitmaps.
Definition: types:64
value_type as_value() const
Get the underlying value.
Definition: types:157
static type from_raw(value_type v)
Make flags from a raw value of value_type.
Definition: types:110
type operator~() const
Support ~ for Flags types.
Definition: types:146
type & clear(bits_enum_type flag)
Clear the given flag.
Definition: types:154
type & operator&=(type rhs)
Support &= of two compatible Flags types.
Definition: types:141
UNDERLYING value_type
type of the underlying value
Definition: types:67
Flags()
Make default Flags.
Definition: types:93
friend type operator|(type lhs, type rhs)
Support | of two compatible Flags types.
Definition: types:120
friend type operator&(type lhs, type rhs)
Support & of two compatible Flags types.
Definition: types:128
None_type
The none type to get an empty bitmap.
Definition: types:80
@ None
Use this to get an empty bitmap.
Definition: types:80
BITS_ENUM bits_enum_type
enum type defining a name for each bit
Definition: types:69
type & operator|=(type rhs)
Support |= of two compatible Flags types.
Definition: types:136
bool operator!() const
Support for if (!flags) syntax (test for empty flags).
Definition: types:117
Flags(None_type)
Make an empty bitmap.
Definition: types:90
Flags< BITS_ENUM, UNDERLYING > type
the Flags<> type itself
Definition: types:71
Flags(BITS_ENUM e)
Make flags from bit name.
Definition: types:103
L4 low-level kernel interface.
Boolean meta type.
Definition: types:301
Bool< V > type
The meta type itself.
Definition: types:302
False meta value.
Definition: types:308
Mixin class to define a set of friend bitwise operators on DT.
Definition: types:233
DT operator|=(DT r)
bitwise or assignment for DT
Definition: types:251
constexpr DT operator~() const
bitwise negation for DT
Definition: types:271
constexpr friend bool operator!=(DT l, DT r)
inequality for DT
Definition: types:247
constexpr friend DT operator|(DT l, DT r)
bitwise or for DT
Definition: types:235
DT operator&=(DT r)
bitwise and assignment for DT
Definition: types:258
constexpr friend bool operator==(DT l, DT r)
equality for DT
Definition: types:243
constexpr friend DT operator&(DT l, DT r)
bitwise and for DT
Definition: types:239
Template type to define a flags type with bitwise operations.
Definition: types:285
Flags_t()=default
Default (uninitializing) costructor.
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition: types:291
T raw
Raw integral value.
Definition: types:287
Metafunction to get an unsigned integral type for the given size.
Definition: types:165
Metafunction to get an integral type of the same size as T.
Definition: types:193
Int_for_size< sizeof(T)>::type type
The resulting unsigned integer type with the size like T.
Definition: types:197
Compare two data types for equality.
Definition: types:324
True meta value.
Definition: types:312