Base bitfield class
This commit is contained in:
parent
c08d4e862e
commit
2acee08590
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2017 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_BASE_BIT_FIELD_H_
|
||||
#define XENIA_BASE_BIT_FIELD_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
|
||||
namespace xe {
|
||||
|
||||
// Bitfield, where position starts at the LSB.
|
||||
template <typename T, size_t position, size_t n_bits>
|
||||
struct bf {
|
||||
bf() = default;
|
||||
inline operator T() const { return value(); }
|
||||
|
||||
inline T value() const {
|
||||
return static_cast<T>((storage & mask()) >> position);
|
||||
}
|
||||
|
||||
// For enum values, we strip them down to an underlying type.
|
||||
typedef
|
||||
typename std::conditional<std::is_enum<T>::value, std::underlying_type<T>,
|
||||
std::identity<T>>::type::type value_type;
|
||||
inline value_type mask() const {
|
||||
return (((value_type)~0) >> (8 * sizeof(value_type) - n_bits)) << position;
|
||||
}
|
||||
|
||||
value_type storage;
|
||||
};
|
||||
|
||||
} // namespace xe
|
||||
|
||||
#endif // XENIA_BASE_BIT_FIELD_H_
|
|
@ -68,4 +68,11 @@
|
|||
</DisplayString>
|
||||
</Type>
|
||||
|
||||
<Type Name="xe::bf<*>">
|
||||
<DisplayString>
|
||||
<!-- (storage & mask()) >> position -->
|
||||
{($T1)((storage & ((((value_type)~0) >> (8 * sizeof(value_type) - $T3)) << $T2)) >> $T2)}
|
||||
</DisplayString>
|
||||
</Type>
|
||||
|
||||
</AutoVisualizer>
|
||||
|
|
Loading…
Reference in New Issue