42 static_assert(std::is_integral_v<T>,
"WrappingUint only supports integral types");
43 static_assert(std::is_unsigned_v<T>,
"WrappingUint only supports unsigned types");
68 if (is_older_than(
value, value_)) {
74 return static_cast<T
>(
diff);
80 [[nodiscard]]
explicit operator T()
const {
145 return value_ == other;
154 return value_ != other;
164 return lhs.value_ == rhs.value_;
174 return lhs.value_ != rhs.value_;
184 return is_older_than(lhs.value_, rhs.value_);
194 return lhs < rhs || lhs == rhs;
204 return !(lhs <= rhs);
214 return lhs > rhs || lhs == rhs;
225 return diff(other.value_);
235 [[nodiscard]] std::make_signed_t<T>
diff(T other)
const {
237 if (is_older_than(other, value_)) {
238 return static_cast<std::make_signed_t<T>
>(value_ - other) * -1;
240 return static_cast<std::make_signed_t<T>
>(other - value_);
252 static bool is_older_than(T a, T b) {
253 return !(a == b) &&
static_cast<T
>(b - a) < std::numeric_limits<T>::max() / 2 + 1;
Represents sequence numbers with rollover (wraparound) semantics.
friend bool operator>(const WrappingUint &lhs, const WrappingUint &rhs)
Compares the sequence number with the given value.
friend bool operator>=(const WrappingUint &lhs, const WrappingUint &rhs)
Compares the sequence number with the given value.
WrappingUint()=default
Default construct a sequence number with the value 0.
WrappingUint operator-(const T value) const
Decrements the sequence number by the given value.
std::optional< T > update(const T value)
Updates the value in the sequence.
WrappingUint & operator+=(const T value)
Increments the sequence number by 1 using modulo arithmetic.
std::make_signed_t< uint8_t > diff(const WrappingUint &other) const
std::make_signed_t< T > diff(T other) const
Calculates the difference between two sequence numbers, taking into account wraparound.
bool operator!=(T other)
Compares the sequence number with the given value.
WrappingUint operator+(const T value) const
Increments the sequence number by the given value.
WrappingUint & operator-=(const T value)
Decrements the sequence number by 1 using modulo arithmetic.
friend bool operator<=(const WrappingUint &lhs, const WrappingUint &rhs)
Compares the sequence number with the given value.
WrappingUint(const T value)
Construct a sequence number with the given value.
WrappingUint & operator=(const T value)
Assigns a new value to the sequence number.
bool operator==(T other)
Compares the sequence number with the given value.
friend bool operator==(const WrappingUint &lhs, const WrappingUint &rhs)
Compares the sequence number with the given value.
friend bool operator<(const WrappingUint &lhs, const WrappingUint &rhs)
Compares the sequence number with the given value.
friend bool operator!=(const WrappingUint &lhs, const WrappingUint &rhs)
Compares the sequence number with the given value.
WrappingUint< uint32_t > WrappingUint32
32-bit wrapping unsigned integer.
WrappingUint< uint8_t > WrappingUint8
8-bit wrapping unsigned integer.
WrappingUint< uint16_t > WrappingUint16
16-bit wrapping unsigned integer.
WrappingUint< uint64_t > WrappingUint64
64-bit wrapping unsigned integer.