Sponge
CS144's user-space TCP library
wrapping_integers.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_WRAPPING_INTEGERS_HH
2 #define SPONGE_LIBSPONGE_WRAPPING_INTEGERS_HH
3 
4 #include <cstdint>
5 #include <ostream>
6 
9 class WrappingInt32 {
10  private:
11  uint32_t _raw_value;
12 
13  public:
15  explicit WrappingInt32(uint32_t raw_value) : _raw_value(raw_value) {}
16 
17  uint32_t raw_value() const { return _raw_value; }
18 };
19 
24 WrappingInt32 wrap(uint64_t n, WrappingInt32 isn);
25 
36 uint64_t unwrap(WrappingInt32 n, WrappingInt32 isn, uint64_t checkpoint);
37 
40 
47 inline int32_t operator-(WrappingInt32 a, WrappingInt32 b) { return a.raw_value() - b.raw_value(); }
48 
50 inline bool operator==(WrappingInt32 a, WrappingInt32 b) { return a.raw_value() == b.raw_value(); }
51 
53 inline bool operator!=(WrappingInt32 a, WrappingInt32 b) { return !(a == b); }
54 
56 inline std::ostream &operator<<(std::ostream &os, WrappingInt32 a) { return os << a.raw_value(); }
57 
59 inline WrappingInt32 operator+(WrappingInt32 a, uint32_t b) { return WrappingInt32{a.raw_value() + b}; }
60 
62 inline WrappingInt32 operator-(WrappingInt32 a, uint32_t b) { return a + -b; }
64 
65 #endif // SPONGE_LIBSPONGE_WRAPPING_INTEGERS_HH
WrappingInt32
A 32-bit integer, expressed relative to an arbitrary initial sequence number (ISN)
Definition: wrapping_integers.hh:9
operator+
WrappingInt32 operator+(WrappingInt32 a, uint32_t b)
The point b steps past a.
Definition: wrapping_integers.hh:59
operator<<
std::ostream & operator<<(std::ostream &os, WrappingInt32 a)
Serializes the wrapping integer, a.
Definition: wrapping_integers.hh:56
WrappingInt32::WrappingInt32
WrappingInt32(uint32_t raw_value)
Construct from a raw 32-bit unsigned integer.
Definition: wrapping_integers.hh:15
WrappingInt32::_raw_value
uint32_t _raw_value
The raw 32-bit stored integer.
Definition: wrapping_integers.hh:11
operator-
int32_t operator-(WrappingInt32 a, WrappingInt32 b)
The offset of a relative to b
Definition: wrapping_integers.hh:47
std::ostream
wrap
WrappingInt32 wrap(uint64_t n, WrappingInt32 isn)
operator!=
bool operator!=(WrappingInt32 a, WrappingInt32 b)
Whether the two integers are not equal.
Definition: wrapping_integers.hh:53
unwrap
uint64_t unwrap(WrappingInt32 n, WrappingInt32 isn, uint64_t checkpoint)
operator==
bool operator==(WrappingInt32 a, WrappingInt32 b)
Whether the two integers are equal.
Definition: wrapping_integers.hh:50