9 #ifndef CPPCOMMON_FLAGS_H
10 #define CPPCOMMON_FLAGS_H
13 #include <type_traits>
18 template <
typename TEnum>
48 #define ENUM_FLAGS(type)\
49 using CppCommon::operator&;\
50 using CppCommon::operator|;\
51 using CppCommon::operator^;\
52 namespace CppCommon {\
53 template <> struct IsEnumFlags<type> : std::true_type {};\
63 template <
typename TEnum>
67 typedef typename std::make_unsigned<typename std::underlying_type<TEnum>::type>::type type;
78 { _value =
value;
return *
this; }
80 { _value = (type)
value;
return *
this; }
85 explicit operator
bool() const noexcept {
return isset(); }
95 { _value &= flags._value;
return *
this; }
97 { _value |= flags._value;
return *
this; }
99 { _value ^= flags._value;
return *
this; }
103 {
return Flags(flags1._value & flags2._value); }
105 {
return Flags(flags1._value | flags2._value); }
107 {
return Flags(flags1._value ^ flags2._value); }
111 {
return flags1._value == flags2._value; }
113 {
return flags1._value != flags2._value; }
116 operator TEnum() const noexcept {
return (TEnum)_value; }
119 bool isset() const noexcept {
return (_value != 0); }
126 TEnum
value() const noexcept {
return (TEnum)_value; }
130 std::bitset<
sizeof(type) * 8>
bitset() const noexcept {
return {_value}; }
134 template <
typename UEnum>
std::bitset< sizeof(type) *8 > bitset() const noexcept
Get the bitset value.
Flags & operator&=(const Flags &flags) noexcept
Flags logical assign operators.
bool isset(TEnum value) const noexcept
Is the given flag set?
bool isset(type value) const noexcept
Is the given flag set?
friend void swap(Flags< UEnum > &flags1, Flags< UEnum > &flags2) noexcept
Flags & operator^=(const Flags &flags) noexcept
Flags & operator|=(const Flags &flags) noexcept
friend Flags operator|(const Flags &flags1, const Flags &flags2) noexcept
Flags(TEnum value) noexcept
void swap(Flags &flags) noexcept
Swap two instances.
friend bool operator!=(const Flags &flags1, const Flags &flags2) noexcept
type underlying() const noexcept
Get the underlying enum value.
friend Flags operator&(const Flags &flags1, const Flags &flags2) noexcept
Flags logical friend operators.
bool isset() const noexcept
Is any flag set?
TEnum value() const noexcept
Get the enum value.
Flags operator~() const noexcept
Reverse all flags.
Flags & operator=(const Flags &) noexcept=default
Flags(type value) noexcept
Flags & operator=(Flags &&) noexcept=default
Flags(const Flags &) noexcept=default
Flags & operator=(TEnum value) noexcept
friend Flags operator^(const Flags &flags1, const Flags &flags2) noexcept
bool operator!() const noexcept
Is no flag set?
friend bool operator==(const Flags &flags1, const Flags &flags2) noexcept
Flags(Flags &&) noexcept=default
Enum-based flags inline implementation.
C++ Common project definitions.
void swap(FileCache &cache1, FileCache &cache2) noexcept
Enum-based flags false checker.