RPNX::DataStructures
Header-only C++ data structures and supporting utilities.
Loading...
Searching...
No Matches
uint64_base.hpp
1// Copyright 2025 Ryan P. Nicholl, rnicholl@protonmail.com
2
3#ifndef RPNX_DATASTRUCTURES_RPNX_UINT64_BASE_HPP
4#define RPNX_DATASTRUCTURES_RPNX_UINT64_BASE_HPP
5
6#include <cstdint>
7
8namespace rpnx
9{
19 template < typename derived_t >
20 class uint64_base
21 {
22 private:
23 std::uint64_t value_;
24
25 public:
26 // Default constructor
27 uint64_base() : value_(0)
28 {
29 }
30
32 explicit uint64_base(std::uint64_t val) : value_(val)
33 {
34 }
35
37 uint64_base(const derived_t& other) : value_(other.value_)
38 {
39 }
40
42 derived_t& operator=(const derived_t& other)
43 {
44 if (this != &other)
45 {
46 value_ = other.value_;
47 }
48 return static_cast< derived_t& >(*this);
49 }
50
52 operator std::uint64_t() const
53 {
54 return value_;
55 }
56
58 derived_t operator+(const derived_t& other) const
59 {
60 return derived_t(value_ + other.value_);
61 }
62
64 derived_t operator-(const derived_t& other) const
65 {
66 return derived_t(value_ - other.value_);
67 }
68
70 derived_t operator*(const derived_t& other) const
71 {
72 return derived_t(value_ * other.value_);
73 }
74
76 derived_t operator/(const derived_t& other) const
77 {
78 // You might want to add error handling for division by zero
79 return derived_t(value_ / other.value_);
80 }
81
83 derived_t operator%(const derived_t& other) const
84 {
85 return derived_t(value_ % other.value_);
86 }
87
89 derived_t& operator+=(const derived_t& other)
90 {
91 value_ += other.value_;
92 return static_cast< derived_t& >(*this);
93 }
94
96 derived_t& operator-=(const derived_t& other)
97 {
98 value_ -= other.value_;
99 return static_cast< derived_t& >(*this);
100 }
101
103 derived_t& operator*=(const derived_t& other)
104 {
105 value_ *= other.value_;
106 return static_cast< derived_t& >(*this);
107 }
108
110 derived_t& operator/=(const derived_t& other)
111 {
112 value_ /= other.value_;
113 return static_cast< derived_t& >(*this);
114 }
115
117 derived_t& operator%=(const derived_t& other)
118 {
119 value_ %= other.value_;
120 return static_cast< derived_t& >(*this);
121 }
122
124 derived_t& operator++()
125 { // Pre-increment
126 ++value_;
127 return static_cast< derived_t& >(*this);
128 }
129
131 derived_t operator++(int)
132 { // Post-increment
133 derived_t temp = static_cast< derived_t& >(*this);
134 ++value_;
135 return temp;
136 }
137
139 derived_t& operator--()
140 { // Pre-decrement
141 --value_;
142 return static_cast< derived_t& >(*this);
143 }
144
146 derived_t operator--(int)
147 { // Post-decrement
148 derived_t temp = static_cast< derived_t& >(*this);
149 --value_;
150 return temp;
151 }
152
154 bool operator==(const derived_t& other) const
155 {
156 return value_ == other.value_;
157 }
158
160 bool operator==( uint64_base<derived_t> const& other) const
161 {
162 return value_ == other.value_;
163 }
164
166 bool operator!=(const derived_t& other) const
167 {
168 return value_ != other.value_;
169 }
170
172 bool operator!=(uint64_base<derived_t> const& other) const
173 {
174 return value_ != other.value_;
175 }
176
178 bool operator<(const derived_t& other) const
179 {
180 return value_ < other.value_;
181 }
182
184 bool operator>(const derived_t& other) const
185 {
186 return value_ > other.value_;
187 }
188
190 bool operator<=(const derived_t& other) const
191 {
192 return value_ <= other.value_;
193 }
194
196 bool operator>=(const derived_t& other) const
197 {
198 return value_ >= other.value_;
199 }
200
202 derived_t operator&(const derived_t& other) const
203 {
204 return derived_t(value_ & other.value_);
205 }
206
208 derived_t operator|(const derived_t& other) const
209 {
210 return derived_t(value_ | other.value_);
211 }
212
214 derived_t operator^(const derived_t& other) const
215 {
216 return derived_t(value_ ^ other.value_);
217 }
218
220 derived_t operator~() const
221 {
222 return derived_t(~value_);
223 }
224
226 derived_t operator<<(int shift) const
227 {
228 return derived_t(value_ << shift);
229 }
230
232 derived_t operator>>(int shift) const
233 {
234 return derived_t(value_ >> shift);
235 }
236
238 derived_t& operator&=(const derived_t& other)
239 {
240 value_ &= other.value_;
241 return static_cast< derived_t& >(*this);
242 }
243
245 derived_t& operator|=(const derived_t& other)
246 {
247 value_ |= other.value_;
248 return static_cast< derived_t& >(*this);
249 }
250
252 derived_t& operator^=(const derived_t& other)
253 {
254 value_ ^= other.value_;
255 return static_cast< derived_t& >(*this);
256 }
257
259 derived_t& operator<<=(int shift)
260 {
261 value_ <<= shift;
262 return static_cast< derived_t& >(*this);
263 }
264
266 derived_t& operator>>=(int shift)
267 {
268 value_ >>= shift;
269 return static_cast< derived_t& >(*this);
270 }
271
273 std::uint64_t const& serialize_interface() const
274 {
275 return value_;
276 }
277
279 std::uint64_t& deserialize_interface()
280 {
281 return value_;
282 }
283
285 auto tie() const
286 {
287 return std::tie(value_);
288 }
289
290 auto tie()
291 {
292 return std::tie(value_);
293 }
294
296 static auto constexpr strings()
297 {
298 return std::vector< std::string >{ "value" };
299 }
300
301
302 };
303} // namespace rpnx
304
313#define RPNX_UNIQUE_U64(name) \
314 struct name : public rpnx::uint64_base< name > \
315 { \
316 using rpnx::uint64_base< name >::uint64_base; \
317 static constexpr auto class_name() { return #name; } \
318 using rpnx::uint64_base< name >::operator=; \
319 name() = default; \
320 using rpnx::uint64_base< name >::operator==; \
321 using rpnx::uint64_base< name >::operator!=; \
322 using rpnx::uint64_base< name >::operator<; \
323 using rpnx::uint64_base< name >::operator>; \
324 using rpnx::uint64_base< name >::operator<=; \
325 using rpnx::uint64_base< name >::operator>=; \
326 };
327
328#endif // UINT64_BASE_HPP
derived_t & operator>>=(int shift)
Shifts right and assigns.
Definition uint64_base.hpp:266
auto tie() const
Returns the serialization fields as an immutable tuple.
Definition uint64_base.hpp:285
derived_t & operator/=(const derived_t &other)
Divides and assigns.
Definition uint64_base.hpp:110
derived_t & operator=(const derived_t &other)
Assigns a value from the derived wrapper type.
Definition uint64_base.hpp:42
derived_t operator/(const derived_t &other) const
Divides two wrapped values.
Definition uint64_base.hpp:76
derived_t operator-(const derived_t &other) const
Subtracts two wrapped values.
Definition uint64_base.hpp:64
bool operator!=(const derived_t &other) const
Compares with a derived value for inequality.
Definition uint64_base.hpp:166
derived_t & operator|=(const derived_t &other)
Computes bitwise OR and assigns.
Definition uint64_base.hpp:245
bool operator!=(uint64_base< derived_t > const &other) const
Compares with a base value for inequality.
Definition uint64_base.hpp:172
bool operator==(const derived_t &other) const
Compares with a derived value for equality.
Definition uint64_base.hpp:154
derived_t operator*(const derived_t &other) const
Multiplies two wrapped values.
Definition uint64_base.hpp:70
derived_t operator++(int)
Increments the stored value.
Definition uint64_base.hpp:131
uint64_base(const derived_t &other)
Copies a value from the derived wrapper type.
Definition uint64_base.hpp:37
derived_t operator--(int)
Decrements the stored value.
Definition uint64_base.hpp:146
static auto constexpr strings()
Returns serialization field names in tuple order.
Definition uint64_base.hpp:296
derived_t & operator--()
Decrements the stored value.
Definition uint64_base.hpp:139
derived_t operator&(const derived_t &other) const
Computes bitwise AND.
Definition uint64_base.hpp:202
derived_t operator^(const derived_t &other) const
Computes bitwise exclusive OR.
Definition uint64_base.hpp:214
derived_t operator~() const
Computes bitwise complement.
Definition uint64_base.hpp:220
uint64_base(std::uint64_t val)
Constructs from an unsigned 64-bit value.
Definition uint64_base.hpp:32
bool operator==(uint64_base< derived_t > const &other) const
Compares with a base value for equality.
Definition uint64_base.hpp:160
std::uint64_t & deserialize_interface()
Exposes the stored integer to deserialization code.
Definition uint64_base.hpp:279
derived_t & operator*=(const derived_t &other)
Multiplies and assigns.
Definition uint64_base.hpp:103
std::uint64_t const & serialize_interface() const
Exposes the stored integer to serialization code.
Definition uint64_base.hpp:273
bool operator>=(const derived_t &other) const
Tests whether this value is greater than or equal.
Definition uint64_base.hpp:196
bool operator>(const derived_t &other) const
Tests whether this value is greater.
Definition uint64_base.hpp:184
derived_t & operator-=(const derived_t &other)
Subtracts and assigns.
Definition uint64_base.hpp:96
bool operator<=(const derived_t &other) const
Tests whether this value is less than or equal.
Definition uint64_base.hpp:190
derived_t operator<<(int shift) const
Shifts left.
Definition uint64_base.hpp:226
derived_t operator|(const derived_t &other) const
Computes bitwise OR.
Definition uint64_base.hpp:208
derived_t operator>>(int shift) const
Shifts right.
Definition uint64_base.hpp:232
derived_t & operator%=(const derived_t &other)
Computes a remainder and assigns.
Definition uint64_base.hpp:117
derived_t & operator&=(const derived_t &other)
Computes bitwise AND and assigns.
Definition uint64_base.hpp:238
derived_t & operator+=(const derived_t &other)
Adds and assigns.
Definition uint64_base.hpp:89
derived_t & operator^=(const derived_t &other)
Computes bitwise exclusive OR and assigns.
Definition uint64_base.hpp:252
bool operator<(const derived_t &other) const
Tests whether this value is less.
Definition uint64_base.hpp:178
derived_t & operator++()
Increments the stored value.
Definition uint64_base.hpp:124
derived_t operator%(const derived_t &other) const
Computes the remainder of two wrapped values.
Definition uint64_base.hpp:83
auto tie()
Returns the serialization fields as a mutable tuple.
Definition uint64_base.hpp:290
derived_t & operator<<=(int shift)
Shifts left and assigns.
Definition uint64_base.hpp:259
derived_t operator+(const derived_t &other) const
Adds two wrapped values.
Definition uint64_base.hpp:58
Containers, iterator adapters, callable wrappers, and value utilities.
Definition annex.hpp:14