-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_stream_helpers.cc
57 lines (44 loc) · 1.63 KB
/
byte_stream_helpers.cc
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
#include "byte_stream.hh"
#include <cstdint>
#include <stdexcept>
/*
* read: A helper function thats peeks and pops up to `len` bytes
* from a ByteStream Reader into a string;
*/
void read( Reader& reader, uint64_t len, std::string& out )
{
out.clear();
while ( reader.bytes_buffered() and out.size() < len ) {
auto view = reader.peek();
if ( view.empty() ) {
throw std::runtime_error( "Reader::peek() returned empty string_view" );
}
view = view.substr( 0, len - out.size() ); // Don't return more bytes than desired.
out += view;
reader.pop( view.size() );
}
}
Reader& ByteStream::reader()
{
static_assert( sizeof( Reader ) == sizeof( ByteStream ),
"Please add member variables to the ByteStream base, not the ByteStream Reader." );
return static_cast<Reader&>( *this ); // NOLINT(*-downcast)
}
const Reader& ByteStream::reader() const
{
static_assert( sizeof( Reader ) == sizeof( ByteStream ),
"Please add member variables to the ByteStream base, not the ByteStream Reader." );
return static_cast<const Reader&>( *this ); // NOLINT(*-downcast)
}
Writer& ByteStream::writer()
{
static_assert( sizeof( Writer ) == sizeof( ByteStream ),
"Please add member variables to the ByteStream base, not the ByteStream Writer." );
return static_cast<Writer&>( *this ); // NOLINT(*-downcast)
}
const Writer& ByteStream::writer() const
{
static_assert( sizeof( Writer ) == sizeof( ByteStream ),
"Please add member variables to the ByteStream base, not the ByteStream Writer." );
return static_cast<const Writer&>( *this ); // NOLINT(*-downcast)
}