IOLink IOL_v1.8.0_release
Loading...
Searching...
No Matches
FlagSet.h
1#pragma once
2
3#include <cstdint>
4
5namespace iolink
6{
7
41template <typename EnumType>
43{
44public:
49 : m_value(0)
50 {
51 }
52
56 explicit FlagSet(uint64_t value)
57 : m_value(value)
58 {
59 }
60
64 FlagSet(EnumType flags)
65 : m_value(static_cast<uint64_t>(flags))
66 {
67 }
68
69 FlagSet(const FlagSet& other) = default;
70 FlagSet& operator=(const FlagSet& other) = default;
71
72 FlagSet(FlagSet&& other) noexcept = default;
73 FlagSet& operator=(FlagSet&& other) noexcept = default;
74
78 uint64_t value() const { return m_value; }
79
83 bool has(FlagSet flags) const { return (m_value & flags.m_value) == flags.m_value; }
84
88 void add(FlagSet flags) { m_value |= flags.m_value; }
89
93 void remove(FlagSet flags) { m_value &= ~(flags.m_value); }
94
95 bool operator==(FlagSet flags) const { return m_value == flags.m_value; }
96
97 bool operator!=(FlagSet flags) const { return m_value != flags.m_value; }
98
99 // ================= //
100 // Bitwise operators //
101 // ================= //
102
103 FlagSet& operator|=(FlagSet flags)
104 {
105 m_value |= flags.m_value;
106 return *this;
107 }
108
109 FlagSet& operator&=(FlagSet flags)
110 {
111 m_value &= flags.m_value;
112 return *this;
113 }
114
115 FlagSet& operator^=(FlagSet flags)
116 {
117 m_value ^= flags.m_value;
118 return *this;
119 }
120
121 FlagSet operator|(FlagSet flags) { return flags |= *this; }
122
123 FlagSet operator&(FlagSet flags) { return flags &= *this; }
124
125 FlagSet operator^(FlagSet flags) { return flags ^= *this; }
126
127private:
128 uint64_t m_value;
129};
130
137#define IOLINK_DEFINE_ENUM_BITWISE_OPERATORS(EnumType) \
138 inline FlagSet<EnumType> operator|(EnumType lhs, EnumType rhs) { return FlagSet<EnumType>(lhs) |= rhs; } \
139 inline FlagSet<EnumType> operator&(EnumType lhs, EnumType rhs) { return FlagSet<EnumType>(lhs) &= rhs; } \
140 inline FlagSet<EnumType> operator^(EnumType lhs, EnumType rhs) { return FlagSet<EnumType>(lhs) ^= rhs; }
141
142} // end namespace iolink