Core/Flag: Improve code

This commit is contained in:
Jérôme Leclercq 2021-02-19 14:51:21 +01:00
parent 36be581d9a
commit 7c5b1844d3
1 changed files with 7 additions and 11 deletions

View File

@ -18,33 +18,29 @@ namespace Nz
{ {
}; };
template<typename, typename = void> template<typename, typename = void>
struct IsEnumFlag : std::false_type {}; struct IsEnumFlag : std::false_type {};
template<typename T> template<typename T>
struct IsEnumFlag<T, std::void_t<decltype(EnumAsFlags<T>::max)>> : std::true_type {}; struct IsEnumFlag<T, std::void_t<decltype(EnumAsFlags<T>::max)>> : std::true_type {};
template<typename, typename = void> template<typename, typename = void>
struct GetEnumAutoFlag : std::false_type struct GetEnumAutoFlag : std::integral_constant<bool, true> {};
{
static constexpr bool value = true;
};
template<typename T> template<typename T>
struct GetEnumAutoFlag<T, std::void_t<decltype(T::AutoFlag)>> : std::true_type struct GetEnumAutoFlag<T, std::void_t<decltype(T::AutoFlag)>> : std::integral_constant<bool, T::AutoFlag> {};
{
static constexpr bool value = T::AutoFlag;
};
template<typename E> template<typename E>
class Flags class Flags
{ {
static_assert(std::is_enum<E>::value, "Type must be an enumeration"); static_assert(std::is_enum_v<E>, "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>(), "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_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; static constexpr bool AutoFlag = GetEnumAutoFlag<E>();
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>;