RAVENNAKIT
Loading...
Searching...
No Matches
rav::ByteStream Class Referencefinal

Simple stream implementation that writes to and reads from a vector. More...

#include <byte_stream.hpp>

Inheritance diagram for rav::ByteStream:
rav::InputStream rav::OutputStream

Public Member Functions

 ByteStream ()=default
 ByteStream (std::vector< uint8_t > data)
void reset ()
 Resets the stream to its initial state by clearing the data and setting the read and write positions to 0.
tl::expected< size_t, InputStream::Error > read (uint8_t *buffer, size_t size) override
 Reads data from the stream into the given buffer.
bool set_read_position (size_t position) override
 Sets the read position in the stream.
size_t get_read_position () override
std::optional< size_t > size () const override
bool exhausted () override
tl::expected< void, OutputStream::Error > write (const uint8_t *buffer, size_t size) override
 Writes data from the given buffer to the stream.
tl::expected< void, OutputStream::Error > set_write_position (size_t position) override
 Sets the write position in the stream.
size_t get_write_position () override
void flush () override
 Flushes the stream, ensuring that all data is written to the underlying storage.
Public Member Functions inherited from rav::InputStream
 InputStream ()=default
virtual ~InputStream ()=default
std::optional< size_t > remaining ()
bool skip (size_t size)
 Skips size amount of bytes in the stream.
tl::expected< std::string, Error > read_as_string (size_t size)
 Reads size amount of bytes from the stream and returns it as a string.
template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< Type, Error > read_ne ()
 Reads a value from the given stream in native byte order (not to be confused with network-endian).
template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< Type, Error > read_be ()
 Reads a big-endian value from the given stream.
template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< Type, Error > read_le ()
 Reads a little-endian value from the given stream.
Public Member Functions inherited from rav::OutputStream
 OutputStream ()=default
virtual ~OutputStream ()=default
tl::expected< void, Error > write (const char *buffer, const size_t size)
 Convenience function to write data from a char buffer to the stream.
template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< void, Error > write_ne (const Type value)
 Writes a value to the stream in native byte order (not to be confused with network-endian).
template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< void, Error > write_be (const Type value)
 Writes a big-endian value to the stream.
template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< void, Error > write_le (const Type value)
 Writes a little-endian value to the stream.
tl::expected< void, Error > write_string (const std::string &str)
 Writes a string to the stream.
tl::expected< void, Error > write_cstring (const char *str)
 Writes a c-string to the stream, up to and including the null character.

Additional Inherited Members

Public Types inherited from rav::InputStream
enum class  Error { insufficient_data , failed_to_set_read_position }
Public Types inherited from rav::OutputStream
enum class  Error { failed_to_write , out_of_memory }
Static Public Member Functions inherited from rav::InputStream
static const char * to_string (const Error e)

Detailed Description

Simple stream implementation that writes to and reads from a vector.

Definition at line 37 of file byte_stream.hpp.

Constructor & Destructor Documentation

◆ ByteStream() [1/2]

rav::ByteStream::ByteStream ( )
default

◆ ByteStream() [2/2]

rav::ByteStream::ByteStream ( std::vector< uint8_t > data)
explicit

Member Function Documentation

◆ exhausted()

bool rav::ByteStream::exhausted ( )
nodiscardoverridevirtual
Returns
True if the stream has no more data to read.

Implements rav::InputStream.

◆ flush()

void rav::ByteStream::flush ( )
overridevirtual

Flushes the stream, ensuring that all data is written to the underlying storage.

Not all streams support this operation.

Implements rav::OutputStream.

◆ get_read_position()

size_t rav::ByteStream::get_read_position ( )
nodiscardoverridevirtual
Returns
The current read position in the stream.

Implements rav::InputStream.

◆ get_write_position()

size_t rav::ByteStream::get_write_position ( )
nodiscardoverridevirtual
Returns
The current write position in the stream.

Implements rav::OutputStream.

◆ read()

tl::expected< size_t, InputStream::Error > rav::ByteStream::read ( uint8_t * buffer,
size_t size )
nodiscardoverridevirtual

Reads data from the stream into the given buffer.

If the stream doesn't have enough data, then nothing will be read.

Parameters
bufferThe buffer to read data into.
sizeThe number of bytes to read.
Returns
The number of bytes read.

Implements rav::InputStream.

◆ reset()

void rav::ByteStream::reset ( )

Resets the stream to its initial state by clearing the data and setting the read and write positions to 0.

◆ set_read_position()

bool rav::ByteStream::set_read_position ( size_t position)
nodiscardoverridevirtual

Sets the read position in the stream.

Parameters
positionThe new read position.
Returns
True if the read position was successfully set.

Implements rav::InputStream.

◆ set_write_position()

tl::expected< void, OutputStream::Error > rav::ByteStream::set_write_position ( size_t position)
nodiscardoverridevirtual

Sets the write position in the stream.

Parameters
positionThe new write position.
Returns
True if the write position was successfully set.

Implements rav::OutputStream.

◆ size()

std::optional< size_t > rav::ByteStream::size ( ) const
nodiscardoverridevirtual
Returns
The total number of bytes in this stream. Not all streams support this operation, in which case an empty optional is returned.

Implements rav::InputStream.

◆ write()

tl::expected< void, OutputStream::Error > rav::ByteStream::write ( const uint8_t * buffer,
size_t size )
nodiscardoverridevirtual

Writes data from the given buffer to the stream.

Parameters
bufferThe buffer to write data from.
sizeThe number of bytes to write.
Returns
An expected indicating success or failure.

Implements rav::OutputStream.


The documentation for this class was generated from the following file: