NDEVR
API Documentation
BitFlag.hpp
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: BitFlag
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35namespace NDEVR
36{
54 class BitFlag
55 {
56 protected:
57 static constexpr uint01 kBitMask[8] = {
58 0x01, // 0000 0001
59 0x02, // 0000 0010
60 0x04, // 0000 0100
61 0x08, // 0000 1000
62 0x10, // 0001 0000
63 0x20, // 0010 0000
64 0x40, // 0100 0000
65 0x80 // 1000 0000
66 };
67 static constexpr uint01 kBitMaskInv[8] = {
68 0xFE, // 1111 1110
69 0xFD, // 1111 1101
70 0xFB, // 1111 1011
71 0xF7, // 1111 0111
72 0xEF, // 1110 1111
73 0xDF, // 1101 1111
74 0xBF, // 1011 1111
75 0x7F // 0111 1111
76 };
77 public:
78 constexpr BitFlag()
79 : m_bits(0)
80 {}
81 constexpr BitFlag(sint04 flags)
82 : m_bits(cast<uint01>(flags))
83 {
84 lib_assert(flags <= 255, "Bad bitflag");
85 }
86 constexpr BitFlag(uint01 flags)
87 : m_bits(flags)
88 {}
89 explicit constexpr BitFlag(bool flag)
90 : m_bits(flag ? 255U : 0U)
91 {}
92
93 template<class t_type>
94 BitFlag(std::initializer_list<t_type> active_list)
95 : m_bits(0)
96 {
97 for (auto i : active_list)
98 (*this)(cast<uint01>(i), true);
99 }
100
107 constexpr bool operator[](uint01 bit_index) const
108 {
109 lib_assert(bit_index < 8, "Tried to access large bit value");
110 return (m_bits & kBitMask[bit_index]) != 0;
111 }
112
119 constexpr void operator()(const uint01 bit_index, bool value)
120 {
121 if (value)
122 m_bits |= kBitMask[bit_index];
123 else
124 m_bits &= kBitMaskInv[bit_index];
125 }
126
131 constexpr operator uint01() const
132 {
133 return m_bits;
134 }
135
140 constexpr void operator|=(const BitFlag& mask)
141 {
142 m_bits |= mask.m_bits;
143 }
144
145
146 constexpr void operator^=(const BitFlag& mask)
147 {
148 m_bits ^= mask.m_bits;
149 }
150
155 constexpr void operator&=(const BitFlag& mask)
156 {
157 m_bits &= mask.m_bits;
158 }
159
164 constexpr void operator&=(uint01 mask)
165 {
166 m_bits &= mask;
167 }
168
174 constexpr BitFlag operator&(const BitFlag& mask) const
175 {
176 return BitFlag(cast<uint01>(m_bits & mask.m_bits));
177 }
178
184 constexpr BitFlag operator^(const BitFlag& mask) const
185 {
186 return BitFlag(cast<uint01>(m_bits ^ mask.m_bits));
187 }
188
194 constexpr BitFlag operator&(uint01 mask) const
195 {
196 return BitFlag(cast<uint01>(m_bits & mask));
197 }
198
204 constexpr BitFlag operator^(uint01 mask) const
205 {
206 return BitFlag(cast<uint01>(m_bits ^ mask));
207 }
208
213 constexpr BitFlag operator~() const
214 {
215 return BitFlag(cast<uint01>(~m_bits));
216 }
217
218
219 constexpr bool operator!=(const BitFlag& other) const
220 {
221 return m_bits != other.m_bits;
222 }
223 constexpr bool operator==(const BitFlag& other) const
224 {
225 return m_bits == other.m_bits;
226 }
227
237 constexpr static BitFlag merge(const BitFlag& value_a, const BitFlag& value_b, const BitFlag& mask)
238 {
239 return value_b ^ ((value_b ^ value_a) & mask);
240 }
241
250 constexpr static BitFlag merge(const BitFlag& value_a, const BitFlag& value_b, uint01 num_of_bits)
251 {
252 const uint01 mask = cast<uint01>((1U << (num_of_bits + 1U)) - 1U);
253 return value_b ^ ((value_b ^ value_a) & mask);
254 }
255
256 [[nodiscard]] uint01 bits() const
257 {
258 return m_bits;
259 }
260 private:
262 uint01 m_bits;
263 };
264}
265#ifdef _MSC_VER
266//Used to help debuging
267namespace natvis
268{
269 struct b4lo { unsigned __int8 v : 4; unsigned __int8 _ : 4; }; // NOLINT(clang-diagnostic-language-extension-token)
270 struct b4hi { unsigned __int8 _ : 4; unsigned __int8 v : 4; }; // NOLINT(clang-diagnostic-language-extension-token)
271 struct b8 { unsigned __int8 _; }; // NOLINT(clang-diagnostic-language-extension-token)
272 struct b32 { __int32 _; }; // NOLINT(clang-diagnostic-language-extension-token)
273}
274#endif
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:55
constexpr void operator()(const uint01 bit_index, bool value)
Operator used to set a bit in the bitflag to the given value Parameters:
Definition BitFlag.hpp:119
static constexpr BitFlag merge(const BitFlag &value_a, const BitFlag &value_b, const BitFlag &mask)
Given bit value_a and bit value_b merges the bits such that indices with a mask of false will be set ...
Definition BitFlag.hpp:237
constexpr void operator|=(const BitFlag &mask)
Bitwise 'or' assignment operator.
Definition BitFlag.hpp:140
constexpr BitFlag operator~() const
Bitwise 'ones complement' operator.
Definition BitFlag.hpp:213
constexpr BitFlag operator^(uint01 mask) const
Bitwise 'exclusive or' operator.
Definition BitFlag.hpp:204
static constexpr BitFlag merge(const BitFlag &value_a, const BitFlag &value_b, uint01 num_of_bits)
Given bit value_a and bit value_b merges the bits such that indices with a value lower than num_of_bi...
Definition BitFlag.hpp:250
constexpr BitFlag operator&(uint01 mask) const
Bitwise 'and' operator.
Definition BitFlag.hpp:194
constexpr bool operator[](uint01 bit_index) const
Accesses the value at bit position specified by index.
Definition BitFlag.hpp:107
constexpr void operator&=(const BitFlag &mask)
Bitwise 'and' assignment operator.
Definition BitFlag.hpp:155
constexpr BitFlag operator^(const BitFlag &mask) const
Bitwise 'exclusive or' operator.
Definition BitFlag.hpp:184
constexpr BitFlag operator&(const BitFlag &mask) const
Bitwise 'and' operator.
Definition BitFlag.hpp:174
constexpr void operator&=(uint01 mask)
Bitwise 'and' assignment operator.
Definition BitFlag.hpp:164
The primary namespace for the NDEVR SDK.
constexpr bool operator!=(const Vector< t_dims, t_type > &vec_a, const Vector< t_dims, t_type > &vec_b)
Inequality operator.
Definition Vector.hpp:673
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408