GF2++
Loading...
Searching...
No Matches
BitRef.h
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: 2025 Nessan Fitzmaurice <nzznfitz+gh@icloud.com>
3// SPDX-License-Identifier: MIT
4
8
9#include <gf2/BitStore.h>
10
11namespace gf2 {
12
32template<BitStore Store>
33class BitRef {
34private:
35 // The bit-store we are referencing.
36 Store* m_store;
37
38 // The index of the bit of interest within that bit-store
39 usize m_index;
40
41public:
43 BitRef(Store* store, usize index) : m_store(store), m_index(index) {}
44
45 // The default compiler generator implementations for most "rule of 5" methods will be fine ...
46 constexpr BitRef() = default;
47 constexpr BitRef(BitRef const&) = default;
48 constexpr BitRef(BitRef&&) noexcept = default;
49 ~BitRef() = default;
50
59 constexpr operator bool() const { return gf2::get(*m_store, m_index); }
60
69 constexpr BitRef& operator=(bool rhs) {
70 gf2::set(*m_store, m_index, rhs);
71 return *this;
72 }
73
87 constexpr BitRef& operator=(BitRef const& rhs) {
88 gf2::set(*m_store, m_index, static_cast<bool>(rhs));
89 return *this;
90 }
91
100 constexpr BitRef& flip() {
101 gf2::flip(*m_store, m_index);
102 return *this;
103 }
104
113 constexpr BitRef& operator&=(bool rhs) {
114 if (!rhs) gf2::set(*m_store, m_index, false);
115 return *this;
116 }
117
127 constexpr BitRef& operator&=(BitRef const& rhs) {
128 if (!static_cast<bool>(rhs)) gf2::set(*m_store, m_index, false);
129 return *this;
130 }
131
140 constexpr BitRef& operator|=(bool rhs) {
141 if (rhs) gf2::set(*m_store, m_index);
142 return *this;
143 }
144
154 constexpr BitRef& operator|=(BitRef const& rhs) {
155 if (static_cast<bool>(rhs)) gf2::set(*m_store, m_index);
156 return *this;
157 }
158
167 constexpr BitRef& operator^=(bool rhs) {
168 if (rhs) gf2::flip(*m_store, m_index);
169 return *this;
170 }
171
181 constexpr BitRef& operator^=(BitRef const& rhs) {
182 if (static_cast<bool>(rhs)) gf2::flip(*m_store, m_index);
183 return *this;
184 }
185};
186
187} // namespace gf2
188
189namespace std {
190
192template<gf2::BitStore Store>
193struct formatter<gf2::BitRef<Store>> : formatter<bool> {
194 template<typename FormatContext>
195 auto format(gf2::BitRef<Store> const& bit, FormatContext& ctx) const {
196 return formatter<bool>::format(static_cast<bool>(bit), ctx);
197 }
198};
199
200} // namespace std
A concept for types that can access individual bits packed into contiguous primitive unsigned words,...
A BitRef is a proxy class to reference a single bit in a bit-store.
Definition BitRef.h:33
constexpr BitRef & operator|=(BitRef const &rhs)
Performs an OR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:154
constexpr BitRef & operator|=(bool rhs)
Performs an OR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:140
constexpr BitRef & operator^=(BitRef const &rhs)
Performs an XOR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:181
constexpr BitRef & operator^=(bool rhs)
Performs an XOR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:167
constexpr BitRef & flip()
Flips the bit at index in the referenced bit-store.
Definition BitRef.h:100
constexpr BitRef & operator=(bool rhs)
Sets the bit at index in the referenced bit-store to the given value.
Definition BitRef.h:69
constexpr BitRef & operator&=(bool rhs)
Performs an AND operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:113
constexpr BitRef & operator=(BitRef const &rhs)
Sets the bit at index in the referenced bit-store to the value of the given BitRef.
Definition BitRef.h:87
constexpr BitRef & operator&=(BitRef const &rhs)
Performs an AND operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:127
BitRef(Store *store, usize index)
Constructs a reference to the bit at index in the given bit-store.
Definition BitRef.h:43
The namespace for the gf2 library.
Definition assert.h:119
constexpr auto & flip(Store &store, usize i)
Flips the value of the bit-element at the given index and returns the store for chaining.
Definition BitStore.h:269
constexpr bool get(Store const &store, usize i)
Returns the bool value of the bit at index i in the given bit-store.
Definition BitStore.h:163
constexpr auto & set(Store &store, usize i, bool value=true)
Sets the bit-element at the given index to the specified boolean value and returns the store for chai...
Definition BitStore.h:244
std::size_t usize
Word type alias for the platform's "native"-sized unsigned integer.
Definition Unsigned.h:42
STL namespace.