diff options
-rw-r--r-- | bitfield.h | 80 |
1 files changed, 35 insertions, 45 deletions
@@ -6,22 +6,18 @@ namespace bitfield { namespace impl { -/** - * Constant check for supported underlying BITFILED types. - */ +/// Constant check for supported underlying BITFILED types. template <typename ValueType> static constexpr bool is_bitfield_type_v = std::is_integral<ValueType>::value && // NOLINT(misc-redundant-expression) std::is_unsigned<ValueType>::value && // NOLINT(misc-redundant-expression) !std::is_same<ValueType, bool>::value; -/** - * Compute a BITMASK based on the HIGHBIT and LOWBIT template parameters. - * The HIGHBIT and LOWBIT are inclusive. - * - * # Example - * ComputeMask<4, 7, uint32_t>() -> 0xf0 - */ +/// Compute a BITMASK based on the HIGHBIT and LOWBIT template parameters. +/// The HIGHBIT and LOWBIT are inclusive. +/// +/// # Example +/// compute_mask<4, 7, uint32_t>() -> 0xf0 template <std::uint8_t LowBit, std::uint8_t HighBit, typename ValueType> static constexpr inline ValueType compute_mask() { using unsigned_t = typename std::make_unsigned<ValueType>::type; @@ -40,9 +36,7 @@ static constexpr inline ValueType compute_mask() { // -- FIELD REF ---------------------------------------------------------------- -/** - * A mutable reference to a single FIELD in a BITFIELD. - */ +/// A mutable reference to a single FIELD in a BITFIELD. template <std::uint8_t LowBit, std::uint8_t HighBit, typename ValueType> struct field_ref { constexpr explicit field_ref(ValueType& val) : m_val{val} {} @@ -80,9 +74,7 @@ struct field_ref { // -- CONST FIELD REF ---------------------------------------------------------- -/** - * A constant reference to a single FIELD in a BITFIELD. - */ +/// A constant reference to a single FIELD in a BITFIELD. template <std::uint8_t LowBit, std::uint8_t HighBit, typename ValueType> struct const_field_ref { constexpr explicit const_field_ref(const ValueType& val) : m_val{val} {} @@ -104,9 +96,7 @@ struct const_field_ref { // -- BITFIELD ----------------------------------------------------------------- -/** - * The BITFIELD base class. - */ +/// The BITFIELD base class. template <typename ValueType = std::uint32_t> struct bitfield { static_assert(impl::is_bitfield_type_v<ValueType>, @@ -138,25 +128,25 @@ struct bitfield { }; // -- MACROS ------------------------------------------------------------------- -// -// Code generator macros to conveniently define BITFIELDs with multiple FIELDs. -// -// # Example -// -// BITFIELD_START(status_reg, std::uint32_t) -// FIELD(N, 0, 0) -// FIELD(V, 1, 1) -// FIELD(C, 2, 2) -// FIELD(Z, 3, 3) -// FIELD(MODE, 4, 7) -// BITFIELD_END() -// -// status_ref r(0); -// -// std::uint32_t n = r.V(); -// r.V() = 0xfff; -// assert(r.V() == 0x1); -// assert(r == 0x2); + +/// Code generator macros to conveniently define BITFIELDs with multiple FIELDs. +/// +/// # Example +/// +/// BITFIELD_START(status_reg, std::uint32_t) +/// FIELD(N, 0, 0) +/// FIELD(V, 1, 1) +/// FIELD(C, 2, 2) +/// FIELD(Z, 3, 3) +/// FIELD(MODE, 4, 7) +/// BITFIELD_END() +/// +/// status_ref r(0); +/// +/// std::uint32_t n = r.V(); +/// r.V() = 0xfff; +/// assert(r.V() == 0x1); +/// assert(r == 0x2); #define BITFIELD_START(NAME, TYPE) \ struct NAME : public bitfield::bitfield<TYPE> { \ @@ -169,13 +159,13 @@ struct bitfield { } \ ; -#define FIELD(NAME, L, H) \ - constexpr ::bitfield::field_ref<L, H, ValueType> NAME() { \ - return ::bitfield::field_ref<L, H, ValueType>(m_val); \ - } \ - \ - constexpr ::bitfield::const_field_ref<L, H, ValueType> NAME() const { \ - return ::bitfield::const_field_ref<L, H, ValueType>(m_val); \ +#define FIELD(NAME, L, H) \ + constexpr auto NAME() { \ + return ::bitfield::field_ref<L, H, ValueType>(m_val); \ + } \ + \ + constexpr auto NAME() const { \ + return ::bitfield::const_field_ref<L, H, ValueType>(m_val); \ } // -- TESTS -------------------------------------------------------------------- |