API Documentation
Loading...
Searching...
No Matches
BitReference.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: BitReference
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/BitFlag.h>
35namespace NDEVR
36{
37 /**--------------------------------------------------------------------------------------------------
38 \brief A convenience class used with Buffers or Vectors of bools for referencing or acting on a
39 single bit.
40 **/
42 {
43 public:
44 constexpr BitReference(BitFlag& flag, uint01 bit)
45 : m_flag(flag)
46 , m_bit(bit)
47 {
48 lib_assert(bit < 8, "Bit out of expected range");
49 }
50 constexpr BitReference(const BitReference& reference)
51 : m_flag(reference.m_flag)
52 , m_bit(reference.m_bit)
53 {}
54
55 [[nodiscard]] constexpr bool get() const
56 {
57 return m_flag[m_bit];
58 }
59 constexpr void set(const bool& value)
60 {
61 m_flag(m_bit, value);
62 }
63 constexpr BitReference& operator=(const bool& b)
64 {
65 m_flag(m_bit, b);
66 return *this;
67 };
68 constexpr BitReference& operator=(const BitReference& b)
69 {
70 m_flag(m_bit, b.get());
71 return *this;
72 };
73 constexpr BitReference& operator|=(const bool& b)
74 {
75 m_flag(m_bit, b || get());
76 return *this;
77 };
79 {
80 m_flag(m_bit, b.get() || get());
81 return *this;
82 };
83 constexpr bool operator==(const bool& b) const
84 {
85 return get() == b;
86 };
87 constexpr bool operator!=(const bool& b) const
88 {
89 return get() != b;
90 };
91 constexpr operator bool() const
92 {
93 return m_flag[m_bit];
94 }
95 constexpr bool operator!() const
96 {
97 return !m_flag[m_bit];
98 }
99 private:
100 BitFlag& m_flag;
101 const uint01 m_bit;
102 };
103
104 template<class t_to>
105 constexpr t_to cast(BitReference value)
106 {
107 return static_cast<t_to>(value.get());
108 }
109};
110
#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
A convenience class used with Buffers or Vectors of bools for referencing or acting on a single bit.
Definition BitReference.hpp:42
constexpr BitReference & operator=(const bool &b)
Definition BitReference.hpp:63
constexpr BitReference(const BitReference &reference)
Definition BitReference.hpp:50
constexpr BitReference & operator|=(const bool &b)
Definition BitReference.hpp:73
constexpr bool get() const
Definition BitReference.hpp:55
constexpr BitReference & operator|=(const BitReference &b)
Definition BitReference.hpp:78
constexpr BitReference & operator=(const BitReference &b)
Definition BitReference.hpp:68
constexpr bool operator==(const bool &b) const
Definition BitReference.hpp:83
constexpr bool operator!=(const bool &b) const
Definition BitReference.hpp:87
constexpr bool operator!() const
Definition BitReference.hpp:95
constexpr void set(const bool &value)
Definition BitReference.hpp:59
constexpr BitReference(BitFlag &flag, uint01 bit)
Definition BitReference.hpp:44
Definition ACIColor.h:37
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