Core/Flags: Add AutoFlag property to disable automatic bit shifting
This commit is contained in:
parent
080fd9c7eb
commit
32dcc11258
|
|
@ -18,17 +18,22 @@ namespace Nz
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
// From: https://stackoverflow.com/questions/11927032/sfinae-check-for-static-member-using-decltype
|
template<typename, typename = void>
|
||||||
|
struct IsEnumFlag : std::false_type {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class IsEnumFlag
|
struct IsEnumFlag<T, std::void_t<decltype(EnumAsFlags<T>::max)>> : std::true_type {};
|
||||||
|
|
||||||
|
template<typename, typename = void>
|
||||||
|
struct GetEnumAutoFlag : std::false_type
|
||||||
{
|
{
|
||||||
template<typename U, typename = typename std::enable_if<!std::is_member_pointer<decltype(&EnumAsFlags<U>::max)>::value>::type>
|
static constexpr bool value = true;
|
||||||
static std::true_type check(int);
|
};
|
||||||
|
|
||||||
template <typename> static std::false_type check(...);
|
template<typename T>
|
||||||
|
struct GetEnumAutoFlag<T, std::void_t<decltype(T::AutoFlag)>> : std::true_type
|
||||||
public:
|
{
|
||||||
static constexpr bool value = decltype(check<T>(0))::value;
|
static constexpr bool value = T::AutoFlag;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename E>
|
template<typename E>
|
||||||
|
|
@ -36,8 +41,10 @@ namespace Nz
|
||||||
{
|
{
|
||||||
static_assert(std::is_enum<E>::value, "Type must be an enumeration");
|
static_assert(std::is_enum<E>::value, "Type must be an enumeration");
|
||||||
static_assert(IsEnumFlag<E>::value, "Enum has not been enabled as flags by an EnumAsFlags specialization");
|
static_assert(IsEnumFlag<E>::value, "Enum has not been enabled as flags by an EnumAsFlags specialization");
|
||||||
|
static_assert(std::is_same_v<std::remove_cv_t<decltype(EnumAsFlags<E>::max)>, E>, "EnumAsFlags field max should be of the same type as the enum");
|
||||||
|
|
||||||
static constexpr std::size_t MaxValue = static_cast<std::size_t>(EnumAsFlags<E>::max);
|
static constexpr std::size_t MaxValue = static_cast<std::size_t>(EnumAsFlags<E>::max);
|
||||||
|
static constexpr bool AutoFlag = GetEnumAutoFlag<E>::value;
|
||||||
|
|
||||||
using BitField16 = std::conditional_t<(MaxValue >= 8), UInt16, UInt8>;
|
using BitField16 = std::conditional_t<(MaxValue >= 8), UInt16, UInt8>;
|
||||||
using BitField32 = std::conditional_t<(MaxValue >= 16), UInt32, BitField16>;
|
using BitField32 = std::conditional_t<(MaxValue >= 16), UInt32, BitField16>;
|
||||||
|
|
|
||||||
|
|
@ -258,7 +258,10 @@ namespace Nz
|
||||||
template<typename E>
|
template<typename E>
|
||||||
constexpr typename Flags<E>::BitField Flags<E>::GetFlagValue(E enumValue)
|
constexpr typename Flags<E>::BitField Flags<E>::GetFlagValue(E enumValue)
|
||||||
{
|
{
|
||||||
|
if constexpr (AutoFlag)
|
||||||
return 1U << static_cast<BitField>(enumValue);
|
return 1U << static_cast<BitField>(enumValue);
|
||||||
|
else
|
||||||
|
return enumValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue