|
4 | 4 |
|
5 | 5 | #pragma once |
6 | 6 |
|
| 7 | +#include <boost/config.hpp> |
7 | 8 | #include <type_traits> |
8 | 9 |
|
| 10 | +template<typename Enum> |
| 11 | +struct IsBitset : std::false_type |
| 12 | +{}; |
| 13 | + |
| 14 | +template<typename Enum> |
| 15 | +constexpr bool IsValidBitset_v = IsBitset<Enum>::value && std::is_unsigned<std::underlying_type_t<Enum>>::value; |
| 16 | + |
| 17 | +template<typename Enum> |
| 18 | +using require_validBitset = std::enable_if_t<IsValidBitset_v<Enum>>; |
| 19 | + |
| 20 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 21 | +constexpr auto operator&(Enum lhs, Enum rhs) noexcept |
| 22 | +{ |
| 23 | + using Int = std::underlying_type_t<Enum>; |
| 24 | + return Enum(static_cast<Int>(lhs) & static_cast<Int>(rhs)); |
| 25 | +} |
| 26 | + |
| 27 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 28 | +constexpr auto operator|(Enum lhs, Enum rhs) noexcept |
| 29 | +{ |
| 30 | + using Int = std::underlying_type_t<Enum>; |
| 31 | + return Enum(static_cast<Int>(lhs) | static_cast<Int>(rhs)); |
| 32 | +} |
| 33 | + |
| 34 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 35 | +constexpr Enum& operator&=(Enum& lhs, Enum rhs) noexcept |
| 36 | +{ |
| 37 | + return lhs = lhs & rhs; |
| 38 | +} |
| 39 | + |
| 40 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 41 | +constexpr Enum& operator|=(Enum& lhs, Enum rhs) noexcept |
| 42 | +{ |
| 43 | + return lhs = lhs | rhs; |
| 44 | +} |
| 45 | + |
| 46 | +namespace bitset { |
| 47 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 48 | +BOOST_ATTRIBUTE_NODISCARD constexpr Enum clear(const Enum val, const Enum flag) |
| 49 | +{ |
| 50 | + using Int = std::underlying_type_t<Enum>; |
| 51 | + return val & Enum(~static_cast<const Int>(flag)); |
| 52 | +} |
| 53 | + |
| 54 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 55 | +BOOST_ATTRIBUTE_NODISCARD constexpr Enum set(const Enum val, const Enum flag, const bool state = true) |
| 56 | +{ |
| 57 | + return state ? (val | flag) : clear(val, flag); |
| 58 | +} |
| 59 | + |
| 60 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 61 | +BOOST_ATTRIBUTE_NODISCARD constexpr Enum toggle(const Enum val, const Enum flag) |
| 62 | +{ |
| 63 | + using Int = std::underlying_type_t<Enum>; |
| 64 | + return Enum(static_cast<const Int>(val) ^ static_cast<const Int>(flag)); |
| 65 | +} |
| 66 | + |
| 67 | +template<typename Enum, typename = require_validBitset<Enum>> |
| 68 | +BOOST_ATTRIBUTE_NODISCARD constexpr bool isSet(const Enum val, const Enum flag) |
| 69 | +{ |
| 70 | + return (val & flag) == flag; |
| 71 | +} |
| 72 | +} // namespace bitset |
| 73 | + |
9 | 74 | /// Makes a strongly typed enum usable as a bitset |
10 | | -#define MAKE_BITSET_STRONG(Type) \ |
11 | | - inline auto operator&(Type lhs, Type rhs) \ |
12 | | - { \ |
13 | | - return Type(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); \ |
14 | | - } \ |
15 | | - inline auto operator|(Type lhs, Type rhs) \ |
16 | | - { \ |
17 | | - return Type(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); \ |
18 | | - } \ |
19 | | - /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ |
20 | | - static_assert(std::is_unsigned<std::underlying_type_t<Type>>::value, \ |
| 75 | +#define MAKE_BITSET_STRONG(Type) \ |
| 76 | + template<> \ |
| 77 | + struct IsBitset<Type> : std::true_type \ |
| 78 | + {}; \ |
| 79 | + /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ |
| 80 | + static_assert(std::is_unsigned<std::underlying_type_t<Type>>::value, \ |
21 | 81 | #Type " must use unsigned type as the underlying type") |
0 commit comments