53 #ifndef PSTORE_SUPPORT_BIT_FIELD_HPP 54 #define PSTORE_SUPPORT_BIT_FIELD_HPP 59 #include <type_traits> 67 template <
typename ValueType,
unsigned Bits>
69 static constexpr ValueType value =
static_cast<ValueType
> ((ValueType{1} << Bits) - 1);
72 template <
typename ValueType>
74 static constexpr ValueType value = std::numeric_limits<std::uint8_t>::max ();
77 template <
typename ValueType>
79 static constexpr ValueType value = std::numeric_limits<std::uint16_t>::max ();
82 template <
typename ValueType>
84 static constexpr ValueType value = std::numeric_limits<std::uint32_t>::max ();
87 template <
typename ValueType>
89 static constexpr ValueType value = std::numeric_limits<std::uint64_t>::max ();
92 template <
typename ValueType,
unsigned Bits>
94 template <
typename ValueType>
96 template <
typename ValueType>
98 template <
typename ValueType>
100 template <
typename ValueType>
104 template <
typename ValueType,
unsigned Index,
unsigned Bits,
105 typename =
typename std::enable_if<std::is_unsigned<ValueType>::value &&
106 Index + Bits <= sizeof (ValueType) * 8>::type>
107 class bit_field_base {
109 static constexpr
auto first_bit = Index;
110 static constexpr
auto last_bit = Index + Bits;
115 static constexpr
value_type min () noexcept {
return 0; }
117 static constexpr
value_type max () noexcept {
return mask_; }
120 constexpr
value_type value ()
const noexcept {
121 return (this->value_ >> Index) & this->mask_;
124 template <
typename T,
125 typename =
typename std::enable_if<std::is_unsigned<T>::value &&
127 void assign (T v) noexcept {
128 value_ =
static_cast<value_type> (value_ & ~(mask_ << Index)) |
129 static_cast<value_type> ((v & mask_) << Index);
133 static constexpr
auto mask_ = max_value<ValueType, Bits>::value;
140 template <
typename ValueType,
unsigned Index,
unsigned Bits>
141 class bit_field :
private details::bit_field_base<ValueType, Index, Bits> {
142 using inherited = details::bit_field_base<ValueType, Index, Bits>;
145 using inherited::first_bit;
146 using inherited::last_bit;
147 using inherited::max;
148 using inherited::min;
150 using typename inherited::value_type;
155 using inherited::value;
157 constexpr
operator value_type () const noexcept {
return this->value (); }
160 template <
typename T>
167 template <
typename T>
168 bit_field & operator+= (T other) noexcept {
169 this->assign (static_cast<value_type> (this->value () + other));
172 template <
typename T>
173 bit_field & operator-= (T other) noexcept {
174 this->assign (static_cast<value_type> (this->value () - other));
178 bit_field & operator++ () noexcept {
return operator+=(value_type{1}); }
185 bit_field & operator-- () noexcept {
return operator-=(value_type{1}); }
193 template <
typename ValueType,
unsigned Index>
194 class bit_field<ValueType, Index, 1> :
private details::bit_field_base<ValueType, Index, 1> {
195 using inherited = details::bit_field_base<ValueType, Index, 1>;
198 using inherited::first_bit;
199 using inherited::last_bit;
200 using inherited::max;
201 using inherited::min;
203 using typename inherited::value_type;
209 this->assign (static_cast<value_type> (v));
213 constexpr
bool value ()
const noexcept {
return static_cast<bool> (inherited::value ()); }
214 constexpr
operator bool ()
const noexcept {
return value (); }
219 #endif // PSTORE_SUPPORT_BIT_FIELD_HPP bool assign_type
The canonical type that can be assigned to a bit-field instance.
Definition: bit_field.hpp:205
Definition: bit_field.hpp:68
std::uint16_t assign_type
The canonical type that can be assigned to a bit-field instance.
Definition: bit_field.hpp:152
Definition: bit_field.hpp:141
Yields either 'T' or 'T const' depending on the value is IsConst.
Definition: chunked_sequence.hpp:234
Definition: nonpod2.cpp:40
An implementation of the standard assert() macro with the exception that it will, on failure...