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{
38 {
39 public:
40 constexpr BitReference(BitFlag& flag, uint01 bit)
41 : m_flag(flag)
42 , m_bit(bit)
43 {
44 lib_assert(bit < 8, "Bit out of expected range");
45 }
46 constexpr BitReference(const BitReference& reference)
47 : m_flag(reference.m_flag)
48 , m_bit(reference.m_bit)
49 {}
50
51 [[nodiscard]] constexpr bool get() const
52 {
53 return m_flag[m_bit];
54 }
55 constexpr void set(const bool& value)
56 {
57 m_flag(m_bit, value);
58 }
59 constexpr BitReference& operator=(const bool& b)
60 {
61 m_flag(m_bit, b);
62 return *this;
63 };
64 constexpr BitReference& operator=(const BitReference& b)
65 {
66 m_flag(m_bit, b.get());
67 return *this;
68 };
69 constexpr BitReference& operator|=(const bool& b)
70 {
71 m_flag(m_bit, b || get());
72 return *this;
73 };
75 {
76 m_flag(m_bit, b.get() || get());
77 return *this;
78 };
79 constexpr bool operator==(const bool& b) const
80 {
81 return get() == b;
82 };
83 constexpr bool operator!=(const bool& b) const
84 {
85 return get() != b;
86 };
87 constexpr operator bool() const
88 {
89 return m_flag[m_bit];
90 }
91 constexpr bool operator!() const
92 {
93 return !m_flag[m_bit];
94 }
95 private:
96 BitFlag& m_flag;
97 const uint01 m_bit;
98 };
99
100 template<class t_to>
101 constexpr t_to cast(BitReference value)
102 {
103 return static_cast<t_to>(value.get());
104 }
105};
106
#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
Definition BitReference.hpp:38
constexpr BitReference & operator=(const bool &b)
Definition BitReference.hpp:59
constexpr BitReference(const BitReference &reference)
Definition BitReference.hpp:46
constexpr BitReference & operator|=(const bool &b)
Definition BitReference.hpp:69
constexpr bool get() const
Definition BitReference.hpp:51
constexpr BitReference & operator|=(const BitReference &b)
Definition BitReference.hpp:74
constexpr BitReference & operator=(const BitReference &b)
Definition BitReference.hpp:64
constexpr bool operator==(const bool &b) const
Definition BitReference.hpp:79
constexpr bool operator!=(const bool &b) const
Definition BitReference.hpp:83
constexpr bool operator!() const
Definition BitReference.hpp:91
constexpr void set(const bool &value)
Definition BitReference.hpp:55
constexpr BitReference(BitFlag &flag, uint01 bit)
Definition BitReference.hpp:40
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:98
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514