forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathoptional_bool.hpp
More file actions
189 lines (167 loc) · 5.12 KB
/
Copy pathoptional_bool.hpp
File metadata and controls
189 lines (167 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief A lightweight implementation of std::optional<bool> using a single
* byte.
*
* The first bit (bit 0) indicates whether a value is present.
* The second bit (bit 1) stores the boolean value.
*/
#pragma once
#include <cstdint>
#include <stdexcept>
namespace scale {
/**
* @class OptionalBool
* @brief A compact implementation of std::optional<bool> using a single byte.
*
* Internal representation:
* - 0: no value (std::nullopt)
* - 1: true
* - 2: false
*/
class OptionalBool {
public:
/**
* @brief Default constructor. Initializes an empty OptionalBool
* (std::nullopt).
*/
constexpr OptionalBool() noexcept : data_(0) {}
/**
* @brief Constructs an empty OptionalBool using std::nullopt.
* @param none std::nullopt constant.
*/
constexpr OptionalBool(std::nullopt_t) noexcept : data_(0) {}
/**
* @brief Constructs an OptionalBool with a given boolean value.
* @param value The boolean value to store.
*/
constexpr OptionalBool(bool value) noexcept {
set(value);
}
/**
* @brief Checks whether the OptionalBool contains a value.
* @return True if a value is present, false otherwise.
*/
constexpr bool has_value() const noexcept {
return data_ != 0;
}
/**
* @brief Explicit boolean conversion to check if a value is present.
*/
constexpr explicit operator bool() const noexcept {
return has_value();
}
/**
* @brief Retrieves the stored boolean value.
* @return The stored boolean value.
* @throws std::logic_error if no value is present.
*/
bool value() const {
if (!has_value()) throw std::logic_error("OptionalBool has no value");
return data_ == 1;
}
/**
* @brief Dereference operator for accessing the stored value.
* @return The stored boolean value.
*/
bool operator*() const {
return value();
}
/**
* @brief Retrieves the stored value or a default value if none is present.
* @param default_value The default value to return if no value is present.
* @return The stored value if present, otherwise default_value.
*/
constexpr bool value_or(bool default_value) const noexcept {
return has_value() ? (data_ == 1) : default_value;
}
/**
* @brief Resets the OptionalBool to an empty state.
*/
constexpr void reset() noexcept {
data_ = 0;
}
/**
* @brief Assigns a boolean value to the OptionalBool.
* @param value The boolean value to assign.
* @return Reference to the modified OptionalBool.
*/
OptionalBool &operator=(bool value) noexcept {
set(value);
return *this;
}
/**
* @brief Assigns std::nullopt to the OptionalBool, resetting its state.
* @param none std::nullopt constant.
* @return Reference to the modified OptionalBool.
*/
OptionalBool &operator=(std::nullopt_t) noexcept {
reset();
return *this;
}
/**
* @brief Equality comparison operator.
* @param lhs First OptionalBool.
* @param rhs Second OptionalBool.
* @return True if both are equal, otherwise false.
*/
friend constexpr bool operator==(const OptionalBool &lhs,
const OptionalBool &rhs) noexcept {
return lhs.data_ == rhs.data_;
}
/**
* @brief Inequality comparison operator.
* @param lhs First OptionalBool.
* @param rhs Second OptionalBool.
* @return True if both are not equal, otherwise false.
*/
friend constexpr bool operator!=(const OptionalBool &lhs,
const OptionalBool &rhs) noexcept {
return !(lhs == rhs);
}
/**
* @brief Encodes the optional bool into a SCALE encoder.
* @param opt_bool The optional bool to encode.
* @param encoder The encoder instance to write into.
*/
friend void encode(const OptionalBool &opt_bool, Encoder &encoder) {
encoder.put(opt_bool.data_);
}
/**
* @brief Decodes a BitVec from a SCALE decoder.
* @param opt_bool The optional bool instance to decode into.
* @param decoder The decoder instance to read from.
* @throws DecodeError::NOT_ENOUGH_DATA If there is insufficient data.
* @throws DecodeError::UNEXPECTED_VALUE If there is wrong data.
*/
friend void decode(OptionalBool &opt_bool, Decoder &decoder) {
auto byte = decoder.take();
switch (byte) {
case 0:
opt_bool.reset();
return;
case 1:
opt_bool = true;
return;
case 2:
opt_bool = false;
return;
default:
raise(DecodeError::UNEXPECTED_VALUE);
}
}
private:
uint8_t data_; ///< Internal data representing the state.
/**
* @brief Sets the internal data to represent the given boolean value.
* @param value The boolean value to set.
*/
constexpr void set(bool value) noexcept {
data_ = value ? 1 : 2;
}
};
} // namespace scale