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>
35namespace NDEVR
36{
37 /**-----------------------------------------------------------------------------------------
38 \brief A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false, ...).
39
40 The class emulates an array of 8 bool elements, but optimized for space allocation: generally,
41 each element occupies only one bit (which, on most systems, is eight times less than
42 the smallest elemental type: int01).
43
44 Each bit position can be accessed individually: for example, for a given bitset named foo,
45 the expression foo[3] accesses its fourth bit, just like a regular array accesses its elements.
46 But because no elemental type is a single bit, operator(bit, value) must be used to set
47 values.
48
49 Bitsets have the feature of being able to be constructed from and converted to both integer
50 values and binary strings (see its constructor and members to_ulong and to_string).
51 They can also be directly inserted and extracted from streams in binary format
52 (see applicable operators).
53 *-----------------------------------------------------------------------------------------**/
54 class BitFlag
55 {
56 public:
57 constexpr BitFlag()
58 : m_bits(0)
59 {}
60 constexpr BitFlag(sint04 flags)
61 : m_bits(cast<uint01>(flags))
62 {
63 lib_assert(flags <= 255, "Bad bitflag");
64 }
65 constexpr BitFlag(uint01 flags)
66 : m_bits(flags)
67 {}
68 explicit constexpr BitFlag(bool flag)
69 : m_bits(flag ? 255U : 0U)
70 {}
71
72 template<class t_type>
73 BitFlag(std::initializer_list<t_type> active_list)
74 : m_bits(0)
75 {
76 for (auto i : active_list)
77 (*this)(cast<uint01>(i), true);
78 }
79
80 /**
81 \brief Accesses the value at bit position specified by index. Note this function may have more overhead
82 than traditional [] since an operation will be used. Can only be a read-only value.
83 \param[in] bit_index The index of the bit you wish to access
84 \returns The indexed value.
85 **/
86 constexpr bool operator[](uint01 bit_index) const
87 {
88 lib_assert(bit_index < 8, "Tried to access large bit value");
89 const uint01 mask = cast<uint01>(1U << bit_index);
90 return (m_bits & mask) != 0;
91 }
92
93 /**
94 \brief Operator used to set a bit in the bitflag to the given value
95 Parameters:
96 \param[in] bit_index The index of the bit you wish to set
97 \param[in] value The value to set the bit
98 **/
99 constexpr void operator()(const uint01 bit_index, bool value)
100 {
101 const uint01 mask = cast<uint01>(1U << bit_index);
102 if (value)
103 m_bits |= mask;
104 else
105 m_bits &= ~mask;
106 }
107
108 /**
109 \brief Cast that converts the given to an uint01.
110 \returns The result of the operation.
111 **/
112 constexpr operator uint01() const
113 {
114 return m_bits;
115 }
116
117 /**
118 \brief Bitwise 'or' assignment operator.
119 \param[in] mask The mask.
120 **/
121 constexpr void operator|=(const BitFlag& mask)
122 {
123 m_bits |= mask.m_bits;
124 }
125
126
127 constexpr void operator^=(const BitFlag& mask)
128 {
129 m_bits ^= mask.m_bits;
130 }
131
132 /**
133 \brief Bitwise 'and' assignment operator.
134 \param[in] mask The mask.
135 **/
136 constexpr void operator&=(const BitFlag& mask)
137 {
138 m_bits &= mask.m_bits;
139 }
140
141 /**
142 \brief Bitwise 'and' assignment operator.
143 \param[in] mask The mask.
144 **/
145 constexpr void operator&=(uint01 mask)
146 {
147 m_bits &= mask;
148 }
149
150 /**
151 \brief Bitwise 'and' operator.
152 \param[in] mask - The mask.
153 \returns The result of the operation.
154 **/
155 constexpr BitFlag operator&(const BitFlag& mask) const
156 {
157 return BitFlag(cast<uint01>(m_bits & mask.m_bits));
158 }
159
160 /**
161 \brief Bitwise 'exclusive or' operator.
162 \param[in] mask - The mask.
163 \returns The result of the operation.
164 **/
165 constexpr BitFlag operator^(const BitFlag& mask) const
166 {
167 return BitFlag(cast<uint01>(m_bits ^ mask.m_bits));
168 }
169
170 /**
171 \brief Bitwise 'and' operator.
172 \param[in] mask - The mask.
173 \returns The result of the operation.
174 **/
175 constexpr BitFlag operator&(uint01 mask) const
176 {
177 return BitFlag(cast<uint01>(m_bits & mask));
178 }
179
180 /**
181 \brief Bitwise 'exclusive or' operator.
182 \param[in] mask - The mask.
183 \returns The result of the operation.
184 **/
185 constexpr BitFlag operator^(uint01 mask) const
186 {
187 return BitFlag(cast<uint01>(m_bits ^ mask));
188 }
189
190 /**
191 \brief Bitwise 'ones complement' operator.
192 \returns The result of the operation.
193 **/
194 constexpr BitFlag operator~() const
195 {
196 return BitFlag(cast<uint01>(~m_bits));
197 }
198
199
200 constexpr bool operator!=(const BitFlag& other) const
201 {
202 return m_bits != other.m_bits;
203 }
204 constexpr bool operator==(const BitFlag& other) const
205 {
206 return m_bits == other.m_bits;
207 }
208
209 /**
210
211 \brief Given bit value_a and bit value_b merges the bits such that indices with a mask of false will be set to
212 value_a and values with a bit of mask 0 will be set to b in the returning bitflag.
213 \param[in] value_a A BitFlag to process where values of mask 0 will be used.
214 \param[in] value_b A BitFlag to process where values of mask 1 will be used.
215 \param[in] mask The mask used to determine which bits will be used.
216 \returns A BitFlag consisting of the merging of value_a and value_b as defined by the mask.
217 **/
218 constexpr static BitFlag merge(const BitFlag& value_a, const BitFlag& value_b, const BitFlag& mask)
219 {
220 return value_b ^ ((value_b ^ value_a) & mask);
221 }
222
223 /**
224 \brief Given bit value_a and bit value_b merges the bits such that indices with a value lower than num_of_bits
225 will be set to value_a and values with a bit of mask 0 will be set to b in the returning bitflag
226 \param[in] value_a A BitFlag to process where values of mask 0 will be used.
227 \param[in] value_b A BitFlag to process where values of mask 1 will be used.
228 \param[in] num_of_bits The bits used to use from value_a.
229 \returns A BitFlag consisting of the merging of value_a and value_b as defined by the num_of_bits.
230 **/
231 constexpr static BitFlag merge(const BitFlag& value_a, const BitFlag& value_b, uint01 num_of_bits)
232 {
233 const uint01 mask = cast<uint01>((1U << (num_of_bits + 1U)) - 1U);
234 return value_b ^ ((value_b ^ value_a) & mask);
235 }
236
237 [[nodiscard]] uint01 bits() const
238 {
239 return m_bits;
240 }
241 private:
242 /** The bit information. */
243 uint01 m_bits;
244 };
245}
246#ifdef _MSC_VER
247//Used to help debuging
248namespace natvis
249{
250 struct b4lo { unsigned __int8 v : 4; unsigned __int8 _ : 4; }; // NOLINT(clang-diagnostic-language-extension-token)
251 struct b4hi { unsigned __int8 _ : 4; unsigned __int8 v : 4; }; // NOLINT(clang-diagnostic-language-extension-token)
252 struct b8 { unsigned __int8 _; }; // NOLINT(clang-diagnostic-language-extension-token)
253 struct b32 { __int32 _; }; // NOLINT(clang-diagnostic-language-extension-token)
254}
255#endif
#define lib_assert(expression, message)
Definition LibAssert.h:61
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:55
constexpr bool operator[](uint01 bit_index) const
Accesses the value at bit position specified by index. Note this function may have more overhead than...
Definition BitFlag.hpp:86
constexpr BitFlag(bool flag)
Definition BitFlag.hpp:68
constexpr void operator&=(const BitFlag &mask)
Bitwise 'and' assignment operator.
Definition BitFlag.hpp:136
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:218
constexpr BitFlag operator^(const BitFlag &mask) const
Bitwise 'exclusive or' operator.
Definition BitFlag.hpp:165
constexpr void operator|=(const BitFlag &mask)
Bitwise 'or' assignment operator.
Definition BitFlag.hpp:121
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:231
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:99
constexpr BitFlag(uint01 flags)
Definition BitFlag.hpp:65
constexpr void operator&=(uint01 mask)
Bitwise 'and' assignment operator.
Definition BitFlag.hpp:145
BitFlag(std::initializer_list< t_type > active_list)
Definition BitFlag.hpp:73
constexpr BitFlag operator^(uint01 mask) const
Bitwise 'exclusive or' operator.
Definition BitFlag.hpp:185
constexpr BitFlag operator~() const
Bitwise 'ones complement' operator.
Definition BitFlag.hpp:194
constexpr bool operator==(const BitFlag &other) const
Definition BitFlag.hpp:204
constexpr BitFlag()
Definition BitFlag.hpp:57
constexpr bool operator!=(const BitFlag &other) const
Definition BitFlag.hpp:200
constexpr BitFlag operator&(uint01 mask) const
Bitwise 'and' operator.
Definition BitFlag.hpp:175
uint01 bits() const
Definition BitFlag.hpp:237
constexpr BitFlag(sint04 flags)
Definition BitFlag.hpp:60
constexpr void operator^=(const BitFlag &mask)
Definition BitFlag.hpp:127
constexpr BitFlag operator&(const BitFlag &mask) const
Bitwise 'and' operator.
Definition BitFlag.hpp:155
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:64
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
Definition BitFlag.hpp:249