Core/Flags: Add AutoFlag property to disable automatic bit shifting

This commit is contained in:
Jérôme Leclercq 2021-02-19 14:45:37 +01:00
parent 080fd9c7eb
commit 32dcc11258
2 changed files with 20 additions and 10 deletions

View File

@ -18,17 +18,22 @@ namespace Nz
{
};
// From: https://stackoverflow.com/questions/11927032/sfinae-check-for-static-member-using-decltype
template <typename T>
class IsEnumFlag
template<typename, typename = void>
struct IsEnumFlag : std::false_type {};
template<typename T>
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 std::true_type check(int);
static constexpr bool value = true;
};
template <typename> static std::false_type check(...);
public:
static constexpr bool value = decltype(check<T>(0))::value;
template<typename T>
struct GetEnumAutoFlag<T, std::void_t<decltype(T::AutoFlag)>> : std::true_type
{
static constexpr bool value = T::AutoFlag;
};
template<typename E>
@ -36,8 +41,10 @@ namespace Nz
{
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(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 bool AutoFlag = GetEnumAutoFlag<E>::value;
using BitField16 = std::conditional_t<(MaxValue >= 8), UInt16, UInt8>;
using BitField32 = std::conditional_t<(MaxValue >= 16), UInt32, BitField16>;

View File

@ -258,7 +258,10 @@ namespace Nz
template<typename E>
constexpr typename Flags<E>::BitField Flags<E>::GetFlagValue(E enumValue)
{
return 1U << static_cast<BitField>(enumValue);
if constexpr (AutoFlag)
return 1U << static_cast<BitField>(enumValue);
else
return enumValue;
}
/*!