class
#include <core/memory/stream.hpp>
Stream Interface class for memory streams. Abstracts away sources or destinations of data.
Motivation
Why do we have our own streams? Well, the standard library streams are hard to use and extend, and very template heavy. Using our own streams allows us to abstract away more easily where the data is coming from, or going to, allowing us, for example, to embed files into the executable and not have to worry about the code that reads them.
Derived classes
-
template<typename T>class cubos::core::data::FileStream final
- Wrapper around an implementation-specific file stream which keeps the file alive and does some sanity checks.
- class BufferStream
- Stream implementation which writes to/reads from a buffer.
- class StandardStream
- Stream implementation which wraps a
libc
file pointer. - class cubos::core::net::TcpStream
- Stream implementation which writes to/reads from a TCP connection.
Public static variables
Constructors, destructors, conversion operators
- Stream() defaulted
- Constructs.
- Stream(Stream&&) defaulted
- Move constructs.
-
template<typename T>requires(::std::is_integral_v<T> && ::std::is_signed_v<T>) void print(T value
- Prints a signed integer to the stream.
-
template<typename T>requires(::std::is_integral_v<T> && !::std::is_signed_v<T>) inline void print(T value
- Prints a signed integer to the stream.
Public functions
- auto read(void* data, std::size_t size) -> std::size_t pure virtual
- Reads data from the stream.
- auto write(const void* data, std::size_t size) -> std::size_t pure virtual
- Writes data to the stream.
- auto readExact(void* data, std::size_t size) -> bool
- Reads data from the stream with the exact specified size.
- auto writeExact(const void* data, std::size_t size) -> bool
- Writes data to the stream with the exact specified size.
- auto tell() const -> std::size_t pure virtual
- Gets the current position in the stream.
- void seek(ptrdiff_t offset, SeekOrigin origin) pure virtual
- Seeks to a position in the stream.
- auto eof() const -> bool pure virtual
- Checks if the stream still has content to read.
- auto peek() -> char pure virtual
- Peeks one byte from the stream.
- auto get() -> char
- Gets one byte from the stream.
- void put(char c)
- Puts one byte into the stream.
- void print(int64_t value, std::size_t base = 10)
- Prints a 64 bit signed integer to the stream.
- void print(uint64_t value, std::size_t base = 10)
- Prints a 64 bit unsigned integer to the stream.
- void print(float value, std::size_t decimalPlaces = 4)
- Prints a float to the stream.
- void print(double value, std::size_t decimalPlaces = 4)
- Prints a double to the stream.
- void print(const char* str)
- Prints a string to the stream.
- void print(const char* str, std::size_t size)
- Prints a string to the stream.
- void print(const ::std::string& str)
- Prints a string to the stream.
-
template<typename T, typename... TArgs>void printf(const char* fmt, T arg, TArgs... args)
- Prints a formatted string the stream.
- void printf(const char* fmt)
- Prints a string to the stream.
- void parse(int8_t& value, std::size_t base = 10)
- Parses a 8 bit signed integer from the stream.
- void parse(int16_t& value, std::size_t base = 10)
- Parses a 16 bit signed integer from the stream.
- void parse(int32_t& value, std::size_t base = 10)
- Parses a 32 bit signed integer from the stream.
- void parse(int64_t& value, std::size_t base = 10)
- Parses a 64 bit signed integer from the stream.
- void parse(uint8_t& value, std::size_t base = 10)
- Parses a 8 bit unsigned integer from the stream.
- void parse(uint16_t& value, std::size_t base = 10)
- Parses a 16 bit unsigned integer from the stream.
- void parse(uint32_t& value, std::size_t base = 10)
- Parses a 32 bit unsigned integer from the stream.
- void parse(uint64_t& value, std::size_t base = 10)
- Parses a 64 bit unsigned integer from the stream.
- void parse(float& value)
- Parses a float from the stream.
- void parse(double& value)
- Parses a double from the stream.
- void readUntil(std::string& str, const char* terminator)
- Reads a string from the stream until the
terminator
(or\0
) is found. - auto readUntil(char* buffer, std::size_t size, const char* terminator) -> std::size_t
- Reads a string from the stream until the
terminator
(or\0
) is found. - void ignore(std::size_t size)
- Ignores a number of bytes from the stream.
Function documentation
template<typename T>
cubos:: core:: memory:: Stream:: requires(::std::is_integral_v<T> && ::std::is_signed_v<T>) void print(T value
Prints a signed integer to the stream.
Template parameters | |
---|---|
T | Type of the integer. |
template<typename T>
cubos:: core:: memory:: Stream:: requires(::std::is_integral_v<T> && !::std::is_signed_v<T>) inline void print(T value
Prints a signed integer to the stream.
Template parameters | |
---|---|
T | Integer type. |
std::size_t cubos:: core:: memory:: Stream:: read(void* data,
std::size_t size) pure virtual
Reads data from the stream.
Parameters | |
---|---|
data | Buffer to read data into. |
size | Size of the buffer. |
Returns | Number of bytes read. |
std::size_t cubos:: core:: memory:: Stream:: write(const void* data,
std::size_t size) pure virtual
Writes data to the stream.
Parameters | |
---|---|
data | Buffer to write data from. |
size | Size of the buffer. |
Returns | Number of bytes written. |
bool cubos:: core:: memory:: Stream:: readExact(void* data,
std::size_t size)
Reads data from the stream with the exact specified size.
Parameters | |
---|---|
data | Buffer to read data into. |
size | Size of the buffer. |
Returns | Whether reading was sucessful. |
bool cubos:: core:: memory:: Stream:: writeExact(const void* data,
std::size_t size)
Writes data to the stream with the exact specified size.
Parameters | |
---|---|
data | Buffer to write data from. |
size | Size of the buffer. |
Returns | Whether writing was sucessful. |
std::size_t cubos:: core:: memory:: Stream:: tell() const pure virtual
Gets the current position in the stream.
Returns | Current position in the stream, or SIZE_MAX if the position is unknown. |
---|
void cubos:: core:: memory:: Stream:: seek(ptrdiff_t offset,
SeekOrigin origin) pure virtual
Seeks to a position in the stream.
Parameters | |
---|---|
offset | Offset to seek to. |
origin | Origin of the offset. |
bool cubos:: core:: memory:: Stream:: eof() const pure virtual
Checks if the stream still has content to read.
Returns | Whether the stream has reached the end. |
---|
char cubos:: core:: memory:: Stream:: peek() pure virtual
Peeks one byte from the stream.
Returns | Peeked byte. |
---|
void cubos:: core:: memory:: Stream:: put(char c)
Puts one byte into the stream.
Parameters | |
---|---|
c | Byte to put. |
void cubos:: core:: memory:: Stream:: print(int64_t value,
std::size_t base = 10)
Prints a 64 bit signed integer to the stream.
Parameters | |
---|---|
value | Value to print. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: print(uint64_t value,
std::size_t base = 10)
Prints a 64 bit unsigned integer to the stream.
Parameters | |
---|---|
value | Value to print. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: print(float value,
std::size_t decimalPlaces = 4)
Prints a float to the stream.
Parameters | |
---|---|
value | Value to print. |
decimalPlaces | Number of decimal places to print. |
void cubos:: core:: memory:: Stream:: print(double value,
std::size_t decimalPlaces = 4)
Prints a double to the stream.
Parameters | |
---|---|
value | Value to print. |
decimalPlaces | Number of decimal places to print. |
void cubos:: core:: memory:: Stream:: print(const char* str)
Prints a string to the stream.
Parameters | |
---|---|
str | String to print. |
void cubos:: core:: memory:: Stream:: print(const char* str,
std::size_t size)
Prints a string to the stream.
Parameters | |
---|---|
str | String to print. |
size | Size of the string. |
void cubos:: core:: memory:: Stream:: print(const ::std::string& str)
Prints a string to the stream.
Parameters | |
---|---|
str | Value to print. |
template<typename T, typename... TArgs>
void cubos:: core:: memory:: Stream:: printf(const char* fmt,
T arg,
TArgs... args)
Prints a formatted string the stream.
Template parameters | |
---|---|
T | Type of the first argument. |
TArgs | Types of the remaining arguments. |
Parameters | |
fmt | Format string. |
arg | First argument to print. |
args | Remaining arguments to print. |
Usage
stream.printf("Hello, {}!\n", "world"); stream.printf("{} + {} = {}\n", 1, 2, 3); stream.printf("\\{} {}\n", 1, 2); // This will print "{} 2"
void cubos:: core:: memory:: Stream:: printf(const char* fmt)
Prints a string to the stream.
Parameters | |
---|---|
fmt | Format string. |
void cubos:: core:: memory:: Stream:: parse(int8_t& value,
std::size_t base = 10)
Parses a 8 bit signed integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(int16_t& value,
std::size_t base = 10)
Parses a 16 bit signed integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(int32_t& value,
std::size_t base = 10)
Parses a 32 bit signed integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(int64_t& value,
std::size_t base = 10)
Parses a 64 bit signed integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(uint8_t& value,
std::size_t base = 10)
Parses a 8 bit unsigned integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(uint16_t& value,
std::size_t base = 10)
Parses a 16 bit unsigned integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(uint32_t& value,
std::size_t base = 10)
Parses a 32 bit unsigned integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(uint64_t& value,
std::size_t base = 10)
Parses a 64 bit unsigned integer from the stream.
Parameters | |
---|---|
value out | Parsed value. |
base | Base to use. |
void cubos:: core:: memory:: Stream:: parse(float& value)
Parses a float from the stream.
Parameters | |
---|---|
value out | Parsed value. |
void cubos:: core:: memory:: Stream:: parse(double& value)
Parses a double from the stream.
Parameters | |
---|---|
value out | Parsed value. |
void cubos:: core:: memory:: Stream:: readUntil(std::string& str,
const char* terminator)
Reads a string from the stream until the terminator
(or \0
) is found.
Parameters | |
---|---|
str out | Read string. |
terminator | Optional terminator to use. |
std::size_t cubos:: core:: memory:: Stream:: readUntil(char* buffer,
std::size_t size,
const char* terminator)
Reads a string from the stream until the terminator
(or \0
) is found.
Parameters | |
---|---|
buffer | Buffer to read into. |
size | Size of the buffer. |
terminator | Optional terminator to use. |
Returns | Number of bytes read. |
void cubos:: core:: memory:: Stream:: ignore(std::size_t size)
Ignores a number of bytes from the stream.
Parameters | |
---|---|
size | Number of bytes to ignore. |