Loading...
Searching...
No Matches
types
Go to the documentation of this file.
// vi:set ft=cpp: -*- Mode: C++ -*-
/*
* (c) 2014 Alexander Warg <alexander.warg@kernkonzept.com>
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
// very simple type traits for basic L4 functions, for a more complete set
// use <l4/cxx/type_traits> or the standard <type_traits>.
namespace L4 {
namespace Types {
template<typename BITS_ENUM, typename UNDERLYING = unsigned long>
class Flags
{
public:
typedef UNDERLYING value_type;
typedef BITS_ENUM bits_enum_type;
typedef Flags<BITS_ENUM, UNDERLYING> type;
private:
value_type _v;
explicit Flags(value_type v) : _v(v) {}
public:
enum None_type { None };
Flags(None_type) : _v(0) {}
Flags() : _v(0) {}
Flags(BITS_ENUM e) : _v((value_type{1}) << e) {}
static type from_raw(value_type v) { return type(v); }
explicit operator bool () const
{ return _v != 0; }
bool operator ! () const { return _v == 0; }
friend type operator | (type lhs, type rhs)
{ return type(lhs._v | rhs._v); }
friend type operator | (type lhs, bits_enum_type rhs)
{ return lhs | type(rhs); }
friend type operator & (type lhs, type rhs)
{ return type(lhs._v & rhs._v); }
friend type operator & (type lhs, bits_enum_type rhs)
{ return lhs & type(rhs); }
type &operator |= (type rhs) { _v |= rhs._v; return *this; }
type &operator |= (bits_enum_type rhs) { return operator |= (type(rhs)); }
type &operator &= (type rhs) { _v &= rhs._v; return *this; }
type &operator &= (bits_enum_type rhs) { return operator &= (type(rhs)); }
type operator ~ () const { return type(~_v); }
type &clear(bits_enum_type flag) { return operator &= (~type(flag)); }
value_type as_value() const { return _v; }
};
template<unsigned SIZE, bool = true> struct Int_for_size;
template<> struct Int_for_size<sizeof(unsigned char), true>
{ typedef unsigned char type; };
template<> struct Int_for_size<sizeof(unsigned short),
(sizeof(unsigned short) > sizeof(unsigned char))>
{ typedef unsigned short type; };
template<> struct Int_for_size<sizeof(unsigned),
(sizeof(unsigned) > sizeof(unsigned short))>
{ typedef unsigned type; };
template<> struct Int_for_size<sizeof(unsigned long),
(sizeof(unsigned long) > sizeof(unsigned))>
{ typedef unsigned long type; };
template<> struct Int_for_size<sizeof(unsigned long long),
(sizeof(unsigned long long) > sizeof(unsigned long))>
{ typedef unsigned long long type; };
template<typename T> struct Int_for_type
{
typedef typename Int_for_size<sizeof(T)>::type type;
};
#define L4_TYPES_FLAGS_OPS_DEF(T) \
friend constexpr T operator ~ (T f) \
{ \
return T(~static_cast<typename L4::Types::Int_for_type<T>::type>(f)); \
} \
\
friend constexpr T operator | (T l, T r) \
{ \
return T(static_cast<typename L4::Types::Int_for_type<T>::type>(l) \
| static_cast<typename L4::Types::Int_for_type<T>::type>(r)); \
} \
\
friend constexpr T operator & (T l, T r) \
{ \
return T(static_cast<typename L4::Types::Int_for_type<T>::type>(l) \
& static_cast<typename L4::Types::Int_for_type<T>::type>(r)); \
}
template<typename DT>
struct Flags_ops_t
{
friend constexpr DT operator | (DT l, DT r)
{ return DT(l.raw | r.raw); }
friend constexpr DT operator & (DT l, DT r)
{ return DT(l.raw & r.raw); }
friend constexpr bool operator == (DT l, DT r)
{ return l.raw == r.raw; }
friend constexpr bool operator != (DT l, DT r)
{ return l.raw != r.raw; }
DT operator |= (DT r)
{
static_cast<DT *>(this)->raw |= r.raw;
return *static_cast<DT *>(this);
}
DT operator &= (DT r)
{
static_cast<DT *>(this)->raw &= r.raw;
return *static_cast<DT *>(this);
}
explicit constexpr operator bool () const
{
return static_cast<DT const *>(this)->raw != 0;
}
constexpr DT operator ~ () const
{ return DT(~static_cast<DT const *>(this)->raw); }
};
template<typename DT, typename T>
struct Flags_t : Flags_ops_t<Flags_t<DT, T>>
{
T raw;
Flags_t() = default;
explicit constexpr Flags_t(T f) : raw(f) {}
};
template< bool V > struct Bool
{
typedef Bool<V> type;
enum { value = V };
};
struct False : Bool<false> {};
struct True : Bool<true> {};
/*********************/
template<typename A, typename B>
struct Same : False {};
template<typename A>
struct Same<A, A> : True {};
template<bool EXP, typename T = void> struct Enable_if {};
template<typename T> struct Enable_if<true, T> { typedef T type; };
template<typename T1, typename T2, typename T = void>
struct Enable_if_same : Enable_if<Same<T1, T2>::value, T> {};
template<typename T> struct Remove_const { typedef T type; };
template<typename T> struct Remove_const<T const> { typedef T type; };
template<typename T> struct Remove_volatile { typedef T type; };
template<typename T> struct Remove_volatile<T volatile> { typedef T type; };
template<typename T> struct Remove_cv
{ typedef typename Remove_const<typename Remove_volatile<T>::type>::type type; };
template<typename T> struct Remove_pointer { typedef T type; };
template<typename T> struct Remove_pointer<T*> { typedef T type; };
template<typename T> struct Remove_reference { typedef T type; };
template<typename T> struct Remove_reference<T&> { typedef T type; };
template<typename T> struct Remove_pr { typedef T type; };
template<typename T> struct Remove_pr<T&> { typedef T type; };
template<typename T> struct Remove_pr<T*> { typedef T type; };
} // Types
} // L4
L4::Types::Flags::as_value
value_type as_value() const
Get the underlying value.
Definition types:145
L4::Types::Flags::from_raw
static type from_raw(value_type v)
Make flags from a raw value of value_type.
Definition types:98
L4::Types::Flags::clear
type & clear(bits_enum_type flag)
Clear the given flag.
Definition types:142
L4::Types::Flags::operator~
type operator~() const
Support ~ for Flags types.
Definition types:134
L4::Types::Flags::value_type
UNDERLYING value_type
type of the underlying value
Definition types:56
L4::Types::Flags::operator|=
type & operator|=(type rhs)
Support |= of two compatible Flags types.
Definition types:124
L4::Types::Flags::Flags
Flags()
Make default Flags.
Definition types:81
L4::Types::Flags::operator|
friend type operator|(type lhs, type rhs)
Support | of two compatible Flags types.
Definition types:108
L4::Types::Flags::operator&
friend type operator&(type lhs, type rhs)
Support & of two compatible Flags types.
Definition types:116
L4::Types::Flags< BITS_ENUM, UNDERLYING >::None_type
None_type
Definition types:68
L4::Types::Flags< BITS_ENUM, UNDERLYING >::None
@ None
Definition types:68
L4::Types::Flags::bits_enum_type
BITS_ENUM bits_enum_type
enum type defining a name for each bit
Definition types:58
L4::Types::Flags::operator!
bool operator!() const
Support for if (!flags) syntax (test for empty flags).
Definition types:105
L4::Types::Flags::Flags
Flags(None_type)
Make an empty bitmap.
Definition types:78
L4::Types::Flags::type
Flags< BITS_ENUM, UNDERLYING > type
the Flags<> type itself
Definition types:60
L4::Types::Flags::Flags
Flags(BITS_ENUM e)
Make flags from bit name.
Definition types:91
L4::Types::Flags::operator&=
type & operator&=(type rhs)
Support &= of two compatible Flags types.
Definition types:129
L4::Types
L4 basic type helpers for C++.
Definition types:20
L4
L4 low-level kernel interface.
Definition l4sys-l4f-groups.dox:50
L4::Types::Bool
Boolean meta type.
Definition types:289
L4::Types::Bool::type
Bool< V > type
The meta type itself.
Definition types:290
L4::Types::False
False meta value.
Definition types:296
L4::Types::Flags_ops_t
Mixin class to define a set of friend bitwise operators on DT.
Definition types:221
L4::Types::Flags_ops_t::operator|=
DT operator|=(DT r)
bitwise or assignment for DT
Definition types:239
L4::Types::Flags_ops_t::operator!=
friend constexpr bool operator!=(DT l, DT r)
inequality for DT
Definition types:235
L4::Types::Flags_ops_t::operator~
constexpr DT operator~() const
bitwise negation for DT
Definition types:259
L4::Types::Flags_ops_t::operator&
friend constexpr DT operator&(DT l, DT r)
bitwise and for DT
Definition types:227
L4::Types::Flags_ops_t::operator&=
DT operator&=(DT r)
bitwise and assignment for DT
Definition types:246
L4::Types::Flags_ops_t::operator|
friend constexpr DT operator|(DT l, DT r)
bitwise or for DT
Definition types:223
L4::Types::Flags_ops_t::operator==
friend constexpr bool operator==(DT l, DT r)
equality for DT
Definition types:231
L4::Types::Flags_t::Flags_t
Flags_t()=default
Default (uninitializing) costructor.
L4::Types::Flags_t::Flags_t
constexpr Flags_t(T f)
Explicit initialization from the underlying type.
Definition types:279
L4::Types::Flags_t::raw
T raw
Raw integral value.
Definition types:275
L4::Types::Int_for_size
Metafunction to get an unsigned integral type for the given size.
Definition types:153
L4::Types::Int_for_type
Metafunction to get an integral type of the same size as T.
Definition types:181
L4::Types::Int_for_type::type
Int_for_size< sizeof(T)>::type type
The resulting unsigned integer type with the size like T.
Definition types:185
L4::Types::Same
Compare two data types for equality.
Definition types:312
L4::Types::True
True meta value.
Definition types:300