API Documentation
Loading...
Searching...
No Matches
BitFlag.hpp
Go to the documentation of this file.
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>
35
36/**--------------------------------------------------------------------------------------------------
37Namespace: Parke
38Namespace that wraps all Logic created by Tyler Parke
39 *-----------------------------------------------------------------------------------------------**/
40
41namespace NDEVR
42{
43 /**--------------------------------------------------------------------------------------------------
44 Class: BitFlag
45
46 \brief A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false, ...).
47
48 The class emulates an array of 8 bool elements, but optimized for space allocation: generally,
49 each element occupies only one bit (which, on most systems, is eight times less than
50 the smallest elemental type: int01).
51
52 Each bit position can be accessed individually: for example, for a given bitset named foo,
53 the expression foo[3] accesses its fourth bit, just like a regular array accesses its elements.
54 But because no elemental type is a single bit, operator(bit, value) must be used to set
55 values.
56
57 Bitsets have the feature of being able to be constructed from and converted to both integer
58 values and binary strings (see its constructor and members to_ulong and to_string).
59 They can also be directly inserted and extracted from streams in binary format
60 (see applicable operators).
61
62 Author: Tyler Parke
63
64 Date: 2017-11-17
65 *-----------------------------------------------------------------------------------------------**/
66
67 class BitFlag
68 {
69 public:
70 constexpr BitFlag()
71 : m_bits(0)
72 {}
73 constexpr BitFlag(sint04 flags)
74 : m_bits(cast<uint01>(flags))
75 {
76 lib_assert(flags <= 255, "Bad bitflag");
77 }
78 constexpr BitFlag(uint01 flags)
79 : m_bits(flags)
80 {}
81 explicit constexpr BitFlag(bool flag)
82 : m_bits(flag ? 255U : 0U)
83 {}
84
85 template<class t_type>
86 BitFlag(std::initializer_list<t_type> active_list)
87 : m_bits(0)
88 {
89 for (auto i : active_list)
90 (*this)(cast<uint01>(i), true);
91 }
92
93 /**--------------------------------------------------------------------------------------------------
94 Fn: constexpr bool BitFlag::operator[](uint01 index) const
95
96 Accesses the value at bit position specified by index. Note this function may have more overhead
97 than traditional [] since an operation will be used. Can only be a read-only value.
98
99 Author: Tyler Parke
100
101 Date: 2017-11-17
102
103 Parameters:
104 bit_index - The index of the bit you wish to access
105
106 Returns: The indexed value.
107 *-----------------------------------------------------------------------------------------------**/
108
109 constexpr bool operator[](uint01 bit_index) const
110 {
111 lib_assert(bit_index < 8, "Tried to access large bit value");
112 const uint01 mask = cast<uint01>(1U << bit_index);
113 return (m_bits & mask) != 0;
114 }
115
116 /**--------------------------------------------------------------------------------------------------
117 Fn: constexpr void BitFlag::operator()(const uint01 index, bool value)
118
119 Operator used to set a bit in the bitflag to the given value
120
121 Author: Tyler Parke
122
123 Date: 2017-11-17
124
125 Parameters:
126 index - The index of the bit you wish to set
127 value - The value to set the bit
128 *-----------------------------------------------------------------------------------------------**/
129
130 constexpr void operator()(const uint01 bit_index, bool value)
131 {
132 const uint01 mask = cast<uint01>(1U << bit_index);
133 if (value)
134 m_bits |= mask;
135 else
136 m_bits &= ~mask;
137 }
138 /**--------------------------------------------------------------------------------------------------
139 Fn: constexpr operator BitFlag::uint01() const
140
141 Cast that converts the given to an uint01.
142
143 Author: Tyler Parke
144
145 Date: 2017-11-17
146
147 Returns: The result of the operation.
148 *-----------------------------------------------------------------------------------------------**/
149
150 constexpr operator uint01() const
151 {
152 return m_bits;
153 }
154
155 /**--------------------------------------------------------------------------------------------------
156 Fn: constexpr void BitFlag::operator|=(const BitFlag& mask)
157
158 Bitwise 'or' assignment operator.
159
160 Author: Tyler Parke
161
162 Date: 2017-11-17
163
164 Parameters:
165 mask - The mask.
166 *-----------------------------------------------------------------------------------------------**/
167
168 constexpr void operator|=(const BitFlag& mask)
169 {
170 m_bits |= mask.m_bits;
171 }
172
173
174 constexpr void operator^=(const BitFlag& mask)
175 {
176 m_bits ^= mask.m_bits;
177 }
178
179 /**--------------------------------------------------------------------------------------------------
180 Fn: constexpr void BitFlag::operator&=(const BitFlag& mask)
181
182 Bitwise 'and' assignment operator.
183
184 Author: Tyler Parke
185
186 Date: 2017-11-17
187
188 Parameters:
189 mask - The mask.
190 *-----------------------------------------------------------------------------------------------**/
191
192 constexpr void operator&=(const BitFlag& mask)
193 {
194 m_bits &= mask.m_bits;
195 }
196
197 /**--------------------------------------------------------------------------------------------------
198 Fn: constexpr void BitFlag::operator&=(uint01 mask)
199
200 Bitwise 'and' assignment operator.
201
202 Author: Tyler Parke
203
204 Date: 2017-11-17
205
206 Parameters:
207 mask - The mask.
208 *-----------------------------------------------------------------------------------------------**/
209
210 constexpr void operator&=(uint01 mask)
211 {
212 m_bits &= mask;
213 }
214
215 /**--------------------------------------------------------------------------------------------------
216 Fn: constexpr BitFlag BitFlag::operator&(const BitFlag& mask) const
217
218 Bitwise 'and' operator.
219
220 Author: Tyler Parke
221
222 Date: 2017-11-17
223
224 Parameters:
225 mask - The mask.
226
227 Returns: The result of the operation.
228 *-----------------------------------------------------------------------------------------------**/
229
230 constexpr BitFlag operator&(const BitFlag& mask) const
231 {
232 return BitFlag(cast<uint01>(m_bits & mask.m_bits));
233 }
234
235 /**--------------------------------------------------------------------------------------------------
236 Fn: constexpr BitFlag BitFlag::operator^(const BitFlag& mask) const
237
238 Bitwise 'exclusive or' operator.
239
240 Author: Tyler Parke
241
242 Date: 2017-11-17
243
244 Parameters:
245 mask - The mask.
246
247 Returns: The result of the operation.
248 *-----------------------------------------------------------------------------------------------**/
249
250 constexpr BitFlag operator^(const BitFlag& mask) const
251 {
252 return BitFlag(cast<uint01>(m_bits ^ mask.m_bits));
253 }
254
255 /**--------------------------------------------------------------------------------------------------
256 Fn: constexpr BitFlag BitFlag::operator&(uint01 mask) const
257
258 Bitwise 'and' operator.
259
260 Author: Tyler Parke
261
262 Date: 2017-11-17
263
264 Parameters:
265 mask - The mask.
266
267 Returns: The result of the operation.
268 *-----------------------------------------------------------------------------------------------**/
269
270 constexpr BitFlag operator&(uint01 mask) const
271 {
272 return BitFlag(cast<uint01>(m_bits & mask));
273 }
274
275 /**--------------------------------------------------------------------------------------------------
276 Fn: constexpr BitFlag BitFlag::operator^(uint01 mask) const
277
278 Bitwise 'exclusive or' operator.
279
280 Author: Tyler Parke
281
282 Date: 2017-11-17
283
284 Parameters:
285 mask - The mask.
286
287 Returns: The result of the operation.
288 *-----------------------------------------------------------------------------------------------**/
289
290 constexpr BitFlag operator^(uint01 mask) const
291 {
292 return BitFlag(cast<uint01>(m_bits ^ mask));
293 }
294
295 /**--------------------------------------------------------------------------------------------------
296 Fn: constexpr BitFlag BitFlag::operator~() const
297
298 Bitwise 'ones complement' operator.
299
300 Author: Tyler Parke
301
302 Date: 2017-11-17
303
304 Returns: The result of the operation.
305 *-----------------------------------------------------------------------------------------------**/
306
307 constexpr BitFlag operator~() const
308 {
309 return BitFlag(cast<uint01>(~m_bits));
310 }
311
312
313 constexpr bool operator!=(const BitFlag& other) const
314 {
315 return m_bits != other.m_bits;
316 }
317 constexpr bool operator==(const BitFlag& other) const
318 {
319 return m_bits == other.m_bits;
320 }
321
322 /**--------------------------------------------------------------------------------------------------
323 Fn: constexpr static BitFlag BitFlag::merge(const BitFlag& value_a, const BitFlag& value_b,
324 const BitFlag& mask)
325
326 Given bit value_a and bit value_b merges the bits such that indices with a mask of false will be set to
327 value_a and values with a bit of mask 0 will be set to b in the returning bitflag
328
329 Author: Tyler Parke
330
331 Date: 2017-11-17
332
333 Parameters:
334 value_a - A BitFlag to process where values of mask 0 will be used.
335 value_b - A BitFlag to process where values of mask 1 will be used.
336 mask - The mask used to determine which bits will be used.
337
338 Returns: A BitFlag consisting of the merging of value_a and value_b as defined by the mask.
339 *-----------------------------------------------------------------------------------------------**/
340
341 constexpr static BitFlag merge(const BitFlag& value_a, const BitFlag& value_b, const BitFlag& mask)
342 {
343 return value_b ^ ((value_b ^ value_a) & mask);
344 }
345
346 /**--------------------------------------------------------------------------------------------------
347 Fn: constexpr static BitFlag BitFlag::merge(const BitFlag& value_a, const BitFlag& value_b,
348 const BitFlag& mask)
349
350 Given bit value_a and bit value_b merges the bits such that indices with a value lower than num_of_bits
351 will be set to value_a and values with a bit of mask 0 will be set to b in the returning bitflag
352
353 Author: Tyler Parke
354
355 Date: 2017-11-17
356
357 Parameters:
358 value_a - A BitFlag to process where values of mask 0 will be used.
359 value_b - A BitFlag to process where values of mask 1 will be used.
360 num_of_bits - The bits used to use from value_a.
361
362 Returns: A BitFlag consisting of the merging of value_a and value_b as defined by the num_of_bits.
363 *-----------------------------------------------------------------------------------------------**/
364
365 constexpr static BitFlag merge(const BitFlag& a, const BitFlag& b, uint01 num_of_bits)
366 {
367 const uint01 mask = cast<uint01>((1U << (num_of_bits + 1U)) - 1U);
368 return b ^ ((b ^ a) & mask);
369 }
370
371 [[nodiscard]] uint01 bits() const
372 {
373 return m_bits;
374 }
375 private:
376 /** The bit information. */
377 uint01 m_bits;
378 };
379}
380#ifdef _MSC_VER
381//Used to help debuging
382namespace natvis
383{
384 struct b4lo { unsigned __int8 v : 4; unsigned __int8 _ : 4; }; // NOLINT(clang-diagnostic-language-extension-token)
385 struct b4hi { unsigned __int8 _ : 4; unsigned __int8 v : 4; }; // NOLINT(clang-diagnostic-language-extension-token)
386 struct b8 { unsigned __int8 _; }; // NOLINT(clang-diagnostic-language-extension-token)
387 struct b32 { __int32 _; }; // NOLINT(clang-diagnostic-language-extension-token)
388}
389#endif
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:68
constexpr bool operator[](uint01 bit_index) const
Definition BitFlag.hpp:109
constexpr BitFlag(bool flag)
Definition BitFlag.hpp:81
constexpr void operator&=(const BitFlag &mask)
Definition BitFlag.hpp:192
static constexpr BitFlag merge(const BitFlag &value_a, const BitFlag &value_b, const BitFlag &mask)
Definition BitFlag.hpp:341
constexpr BitFlag operator^(const BitFlag &mask) const
Definition BitFlag.hpp:250
constexpr void operator|=(const BitFlag &mask)
Definition BitFlag.hpp:168
constexpr void operator()(const uint01 bit_index, bool value)
Definition BitFlag.hpp:130
constexpr BitFlag(uint01 flags)
Definition BitFlag.hpp:78
constexpr void operator&=(uint01 mask)
Definition BitFlag.hpp:210
BitFlag(std::initializer_list< t_type > active_list)
Definition BitFlag.hpp:86
constexpr BitFlag operator^(uint01 mask) const
Definition BitFlag.hpp:290
constexpr BitFlag operator~() const
Definition BitFlag.hpp:307
constexpr bool operator==(const BitFlag &other) const
Definition BitFlag.hpp:317
constexpr BitFlag()
Definition BitFlag.hpp:70
constexpr bool operator!=(const BitFlag &other) const
Definition BitFlag.hpp:313
constexpr BitFlag operator&(uint01 mask) const
Definition BitFlag.hpp:270
uint01 bits() const
Definition BitFlag.hpp:371
constexpr BitFlag(sint04 flags)
Definition BitFlag.hpp:73
static constexpr BitFlag merge(const BitFlag &a, const BitFlag &b, uint01 num_of_bits)
Definition BitFlag.hpp:365
constexpr void operator^=(const BitFlag &mask)
Definition BitFlag.hpp:174
constexpr BitFlag operator&(const BitFlag &mask) const
Definition BitFlag.hpp:230
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:76
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514