CppCommon  1.0.4.1
C++ Common Library
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 
15 namespace CppCommon {
16 
18 template <typename TEnum>
19 struct IsEnumFlags : public std::false_type {};
20 
22 
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 {};\
54 }
55 
57 
63 template <typename TEnum>
64 class Flags
65 {
67  typedef typename std::make_unsigned<typename std::underlying_type<TEnum>::type>::type type;
68 
69 public:
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;
74  Flags(Flags&&) noexcept = default;
75  ~Flags() noexcept = default;
76 
77  Flags& operator=(type value) noexcept
78  { _value = value; return *this; }
79  Flags& operator=(TEnum value) noexcept
80  { _value = (type)value; return *this; }
81  Flags& operator=(const Flags&) noexcept = default;
82  Flags& operator=(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 
137 private:
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
std::bitset< sizeof(type) *8 > bitset() const noexcept
Get the bitset value.
Definition: flags.h:130
Flags & operator&=(const Flags &flags) noexcept
Flags logical assign operators.
Definition: flags.h:94
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
Flags & operator^=(const Flags &flags) noexcept
Definition: flags.h:98
Flags & operator|=(const Flags &flags) noexcept
Definition: flags.h:96
friend Flags operator|(const Flags &flags1, const Flags &flags2) noexcept
Definition: flags.h:104
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
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=(const Flags &) noexcept=default
Flags(type value) noexcept
Definition: flags.h:71
Flags & operator=(Flags &&) noexcept=default
Flags(const Flags &) noexcept=default
Flags & operator=(TEnum value) noexcept
Definition: flags.h:79
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.
Definition: token_bucket.h:15
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23
Enum-based flags false checker.
Definition: flags.h:19