Base bitfield class

This commit is contained in:
Dr. Chat 2017-02-19 19:58:58 -06:00
parent c08d4e862e
commit 2acee08590
2 changed files with 49 additions and 0 deletions

View File

@ -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_

View File

@ -68,4 +68,11 @@
</DisplayString>
</Type>
<Type Name="xe::bf&lt;*&gt;">
<DisplayString>
<!-- (storage & mask()) >> position -->
{($T1)((storage &amp; ((((value_type)~0) &gt;&gt; (8 * sizeof(value_type) - $T3)) &lt;&lt; $T2)) &gt;&gt; $T2)}
</DisplayString>
</Type>
</AutoVisualizer>