| IOLink 1.11.0

Loading...
Searching...
No Matches
FlagSet.h
1#pragma once
2
3#include <cstdint>
4
5 namespace iolink
6{
7
41 template <typename EnumType>
42 class FlagSet
43{
44 public:
48 FlagSet()
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
127 private:
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
iolink::FlagSet
Template class to handle a flags system from an enum.
Definition FlagSet.h:43
iolink::FlagSet::add
void add(FlagSet flags)
Set the given set of flags to true.
Definition FlagSet.h:88
iolink::FlagSet::FlagSet
FlagSet(uint64_t value)
Initialize from a value.
Definition FlagSet.h:56
iolink::FlagSet::has
bool has(FlagSet flags) const
Check if a given set of flags is true.
Definition FlagSet.h:83
iolink::FlagSet::FlagSet
FlagSet()
Initialize with no flags set.
Definition FlagSet.h:48
iolink::FlagSet::value
uint64_t value() const
Return the internal value.
Definition FlagSet.h:78
iolink::FlagSet::remove
void remove(FlagSet flags)
Set the given set of flags to false.
Definition FlagSet.h:93
iolink::FlagSet::FlagSet
FlagSet(EnumType flags)
Initialize from a value from the wrapped enum type.
Definition FlagSet.h:64
iolink
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8