CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
flags.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_FLAGS_H
10#define CPPCOMMON_FLAGS_H
11
12#include <bitset>
13#include <type_traits>
14
15namespace CppCommon {
16
18template <typename TEnum>
19struct IsEnumFlags : public std::false_type {};
20
22
48#define ENUM_FLAGS(type)\
49using CppCommon::operator&;\
50using CppCommon::operator|;\
51using CppCommon::operator^;\
52namespace CppCommon {\
53template <> struct IsEnumFlags<type> : std::true_type {};\
54}
55
57
63template <typename TEnum>
64class Flags
65{
67 typedef typename std::make_unsigned<typename std::underlying_type<TEnum>::type>::type type;
68
69public:
70 Flags() noexcept : _value(0) {}
71 Flags(type value) noexcept : _value(value) {}
72 Flags(TEnum value) noexcept : _value((type)value) {}
73 Flags(const Flags&) noexcept = default;
76
78 { _value = value; return *this; }
80 { _value = (type)value; return *this; }
81 Flags& operator=(const Flags&) noexcept = default;
83
85 explicit operator bool() const noexcept { return isset(); }
86
88 bool operator!() const noexcept { return !isset(); }
89
91 Flags operator~() const noexcept { return Flags(~_value); }
92
94 Flags& operator&=(const Flags& flags) noexcept
95 { _value &= flags._value; return *this; }
96 Flags& operator|=(const Flags& flags) noexcept
97 { _value |= flags._value; return *this; }
98 Flags& operator^=(const Flags& flags) noexcept
99 { _value ^= flags._value; return *this; }
100
102 friend Flags operator&(const Flags& flags1, const Flags& flags2) noexcept
103 { return Flags(flags1._value & flags2._value); }
104 friend Flags operator|(const Flags& flags1, const Flags& flags2) noexcept
105 { return Flags(flags1._value | flags2._value); }
106 friend Flags operator^(const Flags& flags1, const Flags& flags2) noexcept
107 { return Flags(flags1._value ^ flags2._value); }
108
109 // Flags comparison
110 friend bool operator==(const Flags& flags1, const Flags& flags2) noexcept
111 { return flags1._value == flags2._value; }
112 friend bool operator!=(const Flags& flags1, const Flags& flags2) noexcept
113 { return flags1._value != flags2._value; }
114
116 operator TEnum() const noexcept { return (TEnum)_value; }
117
119 bool isset() const noexcept { return (_value != 0); }
121 bool isset(type value) const noexcept { return (_value & value) != 0; }
123 bool isset(TEnum value) const noexcept { return (_value & (type)value) != 0; }
124
126 TEnum value() const noexcept { return (TEnum)_value; }
128 type underlying() const noexcept { return _value; }
130 std::bitset<sizeof(type) * 8> bitset() const noexcept { return {_value}; }
131
133 void swap(Flags& flags) noexcept { using std::swap; swap(_value, flags._value); }
134 template <typename UEnum>
135 friend void swap(Flags<UEnum>& flags1, Flags<UEnum>& flags2) noexcept;
136
137private:
138 type _value;
139};
140
143} // namespace CppCommon
144
145#include "flags.inl"
146
147#endif // CPPCOMMON_FLAGS_H
Enum-based flags.
Definition flags.h:65
Flags & operator|=(const Flags &flags) noexcept
Definition flags.h:96
bool isset(TEnum value) const noexcept
Is the given flag set?
Definition flags.h:123
bool isset(type value) const noexcept
Is the given flag set?
Definition flags.h:121
friend void swap(Flags< UEnum > &flags1, Flags< UEnum > &flags2) noexcept
std::bitset< sizeof(type) *8 > bitset() const noexcept
Get the bitset value.
Definition flags.h:130
friend Flags operator|(const Flags &flags1, const Flags &flags2) noexcept
Definition flags.h:104
Flags & operator=(const Flags &) noexcept=default
Flags(TEnum value) noexcept
Definition flags.h:72
void swap(Flags &flags) noexcept
Swap two instances.
Definition flags.h:133
friend bool operator!=(const Flags &flags1, const Flags &flags2) noexcept
Definition flags.h:112
Flags & operator=(TEnum value) noexcept
Definition flags.h:79
type underlying() const noexcept
Get the underlying enum value.
Definition flags.h:128
friend Flags operator&(const Flags &flags1, const Flags &flags2) noexcept
Flags logical friend operators.
Definition flags.h:102
bool isset() const noexcept
Is any flag set?
Definition flags.h:119
Flags() noexcept
Definition flags.h:70
TEnum value() const noexcept
Get the enum value.
Definition flags.h:126
Flags operator~() const noexcept
Reverse all flags.
Definition flags.h:91
Flags & operator=(Flags &&) noexcept=default
Flags(type value) noexcept
Definition flags.h:71
Flags & operator&=(const Flags &flags) noexcept
Flags logical assign operators.
Definition flags.h:94
Flags & operator^=(const Flags &flags) noexcept
Definition flags.h:98
Flags(const Flags &) noexcept=default
friend Flags operator^(const Flags &flags1, const Flags &flags2) noexcept
Definition flags.h:106
bool operator!() const noexcept
Is no flag set?
Definition flags.h:88
friend bool operator==(const Flags &flags1, const Flags &flags2) noexcept
Definition flags.h:110
Flags(Flags &&) noexcept=default
Enum-based flags inline implementation.
C++ Common project definitions.
Enum-based flags false checker.
Definition flags.h:19