RAVENNAKIT
Loading...
Searching...
No Matches
rav::OutputStream Class Referenceabstract

An abstract class representing an output stream, providing all kinds of write operations. More...

#include <output_stream.hpp>

Inheritance diagram for rav::OutputStream:
rav::ByteStream rav::FileOutputStream

Public Types

enum class  Error { failed_to_write , out_of_memory }

Public Member Functions

 OutputStream ()=default
virtual ~OutputStream ()=default
virtual tl::expected< void, Error > write (const uint8_t *buffer, size_t size)=0
 Writes data from the given buffer to the stream.
tl::expected< void, Error > write (const char *buffer, const size_t size)
 Convenience function to write data from a char buffer to the stream.
virtual tl::expected< void, Error > set_write_position (size_t position)=0
 Sets the write position in the stream.
virtual size_t get_write_position ()=0
virtual void flush ()=0
 Flushes the stream, ensuring that all data is written to the underlying storage.
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.

Detailed Description

An abstract class representing an output stream, providing all kinds of write operations.

Definition at line 37 of file output_stream.hpp.

Member Enumeration Documentation

◆ Error

enum class rav::OutputStream::Error
strong
Enumerator
failed_to_write 
out_of_memory 

Definition at line 39 of file output_stream.hpp.

Constructor & Destructor Documentation

◆ OutputStream()

rav::OutputStream::OutputStream ( )
default

◆ ~OutputStream()

virtual rav::OutputStream::~OutputStream ( )
virtualdefault

Member Function Documentation

◆ flush()

virtual void rav::OutputStream::flush ( )
pure virtual

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

Not all streams support this operation.

Implemented in rav::ByteStream, and rav::FileOutputStream.

◆ get_write_position()

virtual size_t rav::OutputStream::get_write_position ( )
nodiscardpure virtual
Returns
The current write position in the stream.

Implemented in rav::ByteStream, and rav::FileOutputStream.

◆ set_write_position()

virtual tl::expected< void, Error > rav::OutputStream::set_write_position ( size_t position)
nodiscardpure virtual

Sets the write position in the stream.

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

Implemented in rav::ByteStream, and rav::FileOutputStream.

◆ write() [1/2]

tl::expected< void, Error > rav::OutputStream::write ( const char * buffer,
const size_t size )
inlinenodiscard

Convenience function to write data from a char buffer to the stream.

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

Definition at line 61 of file output_stream.hpp.

◆ write() [2/2]

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

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.

Implemented in rav::ByteStream, and rav::FileOutputStream.

◆ write_be()

template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< void, Error > rav::OutputStream::write_be ( const Type value)
inlinenodiscard

Writes a big-endian value to the stream.

Template Parameters
TypeThe type of the value to write.
Parameters
valueThe value to write.
Returns
An expected indicating success or failure.

Definition at line 101 of file output_stream.hpp.

◆ write_cstring()

tl::expected< void, Error > rav::OutputStream::write_cstring ( const char * str)
inlinenodiscard

Writes a c-string to the stream, up to and including the null character.

If the string doesn't have a null character then the behaviour is undefined (and probably will lead to invalid memory access).

Parameters
strThe string to write.
Returns
An expected indicating success or failure.

Definition at line 140 of file output_stream.hpp.

◆ write_le()

template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< void, Error > rav::OutputStream::write_le ( const Type value)
inlinenodiscard

Writes a little-endian value to the stream.

Template Parameters
TypeThe type of the value to write.
Parameters
valueThe value to write.
Returns
An expected indicating success or failure.

Definition at line 112 of file output_stream.hpp.

◆ write_ne()

template<typename Type, std::enable_if_t< std::is_trivially_copyable_v< Type >, bool > = true>
tl::expected< void, Error > rav::OutputStream::write_ne ( const Type value)
inlinenodiscard

Writes a value to the stream in native byte order (not to be confused with network-endian).

Template Parameters
TypeThe type of the value to write.
Parameters
valueThe value to write.
Returns
An expected indicating success or failure.

Definition at line 90 of file output_stream.hpp.

◆ write_string()

tl::expected< void, Error > rav::OutputStream::write_string ( const std::string & str)
inlinenodiscard

Writes a string to the stream.

The string is prefixed with its size in little-endian format.

Parameters
strThe string to write.
Returns
An expected indicating success or failure.

Definition at line 121 of file output_stream.hpp.


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