Iros
 
Loading...
Searching...
No Matches
bit_proxy_reference.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/types/prelude.h"
4
5namespace di::bit::detail {
7public:
8 constexpr explicit BitProxyReference(u8* byte, u8 bit_offset) : m_byte(byte), m_bit_offset(bit_offset) {}
9
10 constexpr BitProxyReference(BitProxyReference const&) = default;
11 constexpr BitProxyReference(BitProxyReference&&) = default;
12
13 constexpr auto operator=(bool value) const -> BitProxyReference const& {
14 if (value) {
15 *m_byte |= (1U << m_bit_offset);
16 } else {
17 *m_byte &= ~(1U << m_bit_offset);
18 }
19 return *this;
20 }
21
22 // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
23 constexpr auto operator=(BitProxyReference const& other) const -> BitProxyReference const& {
24 return *this = bool(other);
25 }
26
27 constexpr operator bool() const { return !!(*m_byte & (1U << m_bit_offset)); }
28
29 constexpr auto operator~() const -> bool { return !bool(*this); }
30
31 constexpr auto flip() const -> BitProxyReference const& {
32 *m_byte ^= (1U << m_bit_offset);
33 return *this;
34 }
35
36private:
37 u8* m_byte;
38 u8 m_bit_offset;
39};
40}
constexpr BitProxyReference(u8 *byte, u8 bit_offset)
Definition bit_proxy_reference.h:8
constexpr BitProxyReference(BitProxyReference const &)=default
constexpr auto operator=(BitProxyReference const &other) const -> BitProxyReference const &
Definition bit_proxy_reference.h:23
constexpr auto flip() const -> BitProxyReference const &
Definition bit_proxy_reference.h:31
constexpr auto operator~() const -> bool
Definition bit_proxy_reference.h:29
constexpr auto operator=(bool value) const -> BitProxyReference const &
Definition bit_proxy_reference.h:13
constexpr BitProxyReference(BitProxyReference &&)=default
Definition bit_proxy_reference.h:5
std::byte byte
Definition byte.h:64
__UINT8_TYPE__ u8
Definition integers.h:9