Skip to content

Commit 5fc5daa

Browse files
committed
Add stream operator for uint16_t
1 parent 8561f07 commit 5fc5daa

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/quicr/detail/ctrl_message_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace quicr::messages {
1414
quicr::Bytes& operator<<(quicr::Bytes& buffer, std::uint8_t value);
1515
quicr::BytesSpan operator>>(quicr::BytesSpan buffer, uint8_t& value);
1616

17+
Bytes& operator<<(Bytes& buffer, std::uint16_t value);
18+
BytesSpan operator>>(BytesSpan buffer, std::uint16_t& value);
19+
1720
quicr::Bytes& operator<<(quicr::Bytes& buffer, const quicr::UintVar& value);
1821

1922
using GroupId = uint64_t;

src/ctrl_message_types.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ namespace quicr::messages {
2929
return buffer;
3030
}
3131

32+
Bytes& operator<<(Bytes& buffer, std::uint16_t value)
33+
{
34+
const std::uint16_t swapped = SwapBytes(value);
35+
buffer.push_back(static_cast<uint8_t>(swapped >> 8 & 0xFF));
36+
buffer.push_back(static_cast<uint8_t>(swapped & 0xFF));
37+
return buffer;
38+
}
39+
3240
Bytes& operator<<(Bytes& buffer, std::uint64_t value)
3341
{
3442
UintVar varint = value;
@@ -51,6 +59,18 @@ namespace quicr::messages {
5159
return buffer.subspan(sizeof(value));
5260
}
5361

62+
BytesSpan operator>>(BytesSpan buffer, uint16_t& value)
63+
{
64+
if (buffer.size() < sizeof(value)) {
65+
throw std::invalid_argument("Provider buffer too small");
66+
}
67+
const std::uint16_t high = buffer[0];
68+
const std::uint16_t low = buffer[1];
69+
value = high << 8 | low;
70+
value = SwapBytes(value);
71+
return buffer.subspan(sizeof(std::uint16_t));
72+
}
73+
5474
BytesSpan operator>>(BytesSpan buffer, uint64_t& value)
5575
{
5676
UintVar value_uv(buffer);

test/moq_ctrl_messages.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,4 +978,14 @@ TEST_CASE("Key Value Pair encode/decode")
978978
CHECK_EQ(out.value, value);
979979
}
980980
}
981+
}
982+
983+
TEST_CASE("UInt16 Encode/decode")
984+
{
985+
std::uint16_t value = 65535;
986+
Bytes buffer;
987+
buffer << value;
988+
std::uint16_t reconstructed_value = 0;
989+
buffer >> reconstructed_value;
990+
CHECK_EQ(reconstructed_value, value);
981991
}

0 commit comments

Comments
 (0)