intnslib 0.1
A library to hold common functionality used across multiple projects.
Loading...
Searching...
No Matches
intns::io::MemoryReader< E > Class Template Reference

A memory-based binary reader with configurable endianness. More...

#include <BinaryReader.hpp>

Public Member Functions

 MemoryReader (const std::vector< uint8_t > &buffer, size_t position=0)
 Constructs a MemoryReader from a vector buffer.
 
 MemoryReader (std::span< const uint8_t > buffer, size_t position=0)
 Constructs a MemoryReader from a span buffer.
 
size_t size () const noexcept
 Returns the total size of the buffer.
 
size_t position () const noexcept
 Returns the current read position.
 
size_t remaining () const noexcept
 Returns the number of bytes remaining to be read.
 
void set_position (size_t pos) noexcept
 Sets the read position within the buffer.
 
void skip (size_t bytes) noexcept
 Advances the read position by the specified number of bytes.
 
uint8_t read_u8 ()
 Reads an unsigned 8-bit integer.
 
uint16_t read_u16 ()
 Reads an unsigned 16-bit integer with endianness conversion.
 
uint32_t read_u32 ()
 Reads an unsigned 32-bit integer with endianness conversion.
 
uint64_t read_u64 ()
 Reads an unsigned 64-bit integer with endianness conversion.
 
int8_t read_s8 ()
 Reads a signed 8-bit integer.
 
int16_t read_s16 ()
 Reads a signed 16-bit integer with endianness conversion.
 
int32_t read_s32 ()
 Reads a signed 32-bit integer with endianness conversion.
 
int64_t read_s64 ()
 Reads a signed 64-bit integer with endianness conversion.
 
float read_f32 ()
 Reads a 32-bit floating-point value with endianness conversion.
 
double read_f64 ()
 Reads a 64-bit floating-point value with endianness conversion.
 
void read_bytes (void *dest, size_t bytes)
 Reads raw bytes into a destination buffer.
 
void read_u16_array (uint16_t *array, size_t count)
 Reads an array of unsigned 16-bit integers with endianness conversion.
 
void read_u32_array (uint32_t *array, size_t count)
 Reads an array of unsigned 32-bit integers with endianness conversion.
 
std::string read_string (size_t length)
 Reads a fixed-length string from the buffer.
 
std::string read_cstring ()
 Reads a null-terminated C-style string from the buffer.
 
uint8_t peek_u8 () const
 Peeks at an unsigned 8-bit integer without advancing position.
 
uint16_t peek_u16 () const
 Peeks at an unsigned 16-bit integer without advancing position.
 

Detailed Description

template<Endianness E = Endianness::kLittle>
class intns::io::MemoryReader< E >

A memory-based binary reader with configurable endianness.

MemoryReader provides efficient reading of binary data from memory buffers with automatic endianness conversion. It supports reading primitive types, arrays, and strings from a contiguous memory region.

Template Parameters
EThe endianness for data interpretation (default: little endian).

Usage

Construct with a buffer and read data sequentially:

std::vector<uint8_t> buffer = {...};
A memory-based binary reader with configurable endianness.
Definition BinaryReader.hpp:40
uint32_t read_u32()
Reads an unsigned 32-bit integer with endianness conversion.
Definition BinaryReader.hpp:159

Safety

All read operations throw std::out_of_range if attempting to read beyond the buffer boundary. Position manipulation methods provide the no-throw guarantee.

Constructor & Destructor Documentation

◆ MemoryReader() [1/2]

template<Endianness E = Endianness::kLittle>
intns::io::MemoryReader< E >::MemoryReader ( const std::vector< uint8_t > &  buffer,
size_t  position = 0 
)
inlineexplicit

Constructs a MemoryReader from a vector buffer.

Parameters
bufferThe source buffer to read from.
positionInitial read position (default: 0).
Exceptions
std::out_of_rangeIf position is beyond buffer size.

◆ MemoryReader() [2/2]

template<Endianness E = Endianness::kLittle>
intns::io::MemoryReader< E >::MemoryReader ( std::span< const uint8_t buffer,
size_t  position = 0 
)
inlineexplicit

Constructs a MemoryReader from a span buffer.

Parameters
bufferThe source buffer to read from.
positionInitial read position (default: 0).
Exceptions
std::out_of_rangeIf position is beyond buffer size.

Member Function Documentation

◆ peek_u16()

template<Endianness E = Endianness::kLittle>
uint16_t intns::io::MemoryReader< E >::peek_u16 ( ) const
inline

Peeks at an unsigned 16-bit integer without advancing position.

Returns
The value at the current position with endianness conversion.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ peek_u8()

template<Endianness E = Endianness::kLittle>
uint8_t intns::io::MemoryReader< E >::peek_u8 ( ) const
inline

Peeks at an unsigned 8-bit integer without advancing position.

Returns
The value at the current position.
Exceptions
std::out_of_rangeIf the position is at or beyond the buffer end.

◆ position()

template<Endianness E = Endianness::kLittle>
size_t intns::io::MemoryReader< E >::position ( ) const
inlinenoexcept

Returns the current read position.

Returns
The current position within the buffer.

◆ read_bytes()

template<Endianness E = Endianness::kLittle>
void intns::io::MemoryReader< E >::read_bytes ( void dest,
size_t  bytes 
)
inline

Reads raw bytes into a destination buffer.

Parameters
destPointer to the destination buffer.
bytesNumber of bytes to read.
Exceptions
std::invalid_argumentIf dest is nullptr.
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_cstring()

template<Endianness E = Endianness::kLittle>
std::string intns::io::MemoryReader< E >::read_cstring ( )
inline

Reads a null-terminated C-style string from the buffer.

Reads until a null terminator is found or the end of buffer is reached. If no null terminator is found, reads all remaining bytes.

Returns
The string read from the buffer (without null terminator).
Note
This method never throws.

◆ read_f32()

template<Endianness E = Endianness::kLittle>
float intns::io::MemoryReader< E >::read_f32 ( )
inline

Reads a 32-bit floating-point value with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_f64()

template<Endianness E = Endianness::kLittle>
double intns::io::MemoryReader< E >::read_f64 ( )
inline

Reads a 64-bit floating-point value with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_s16()

template<Endianness E = Endianness::kLittle>
int16_t intns::io::MemoryReader< E >::read_s16 ( )
inline

Reads a signed 16-bit integer with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_s32()

template<Endianness E = Endianness::kLittle>
int32_t intns::io::MemoryReader< E >::read_s32 ( )
inline

Reads a signed 32-bit integer with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_s64()

template<Endianness E = Endianness::kLittle>
int64_t intns::io::MemoryReader< E >::read_s64 ( )
inline

Reads a signed 64-bit integer with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_s8()

template<Endianness E = Endianness::kLittle>
int8_t intns::io::MemoryReader< E >::read_s8 ( )
inline

Reads a signed 8-bit integer.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_string()

template<Endianness E = Endianness::kLittle>
std::string intns::io::MemoryReader< E >::read_string ( size_t  length)
inline

Reads a fixed-length string from the buffer.

Parameters
lengthNumber of bytes to read.
Returns
The string read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_u16()

template<Endianness E = Endianness::kLittle>
uint16_t intns::io::MemoryReader< E >::read_u16 ( )
inline

Reads an unsigned 16-bit integer with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_u16_array()

template<Endianness E = Endianness::kLittle>
void intns::io::MemoryReader< E >::read_u16_array ( uint16_t array,
size_t  count 
)
inline

Reads an array of unsigned 16-bit integers with endianness conversion.

Parameters
arrayPointer to the destination array.
countNumber of elements to read.
Exceptions
std::invalid_argumentIf array is nullptr.
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_u32()

template<Endianness E = Endianness::kLittle>
uint32_t intns::io::MemoryReader< E >::read_u32 ( )
inline

Reads an unsigned 32-bit integer with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_u32_array()

template<Endianness E = Endianness::kLittle>
void intns::io::MemoryReader< E >::read_u32_array ( uint32_t array,
size_t  count 
)
inline

Reads an array of unsigned 32-bit integers with endianness conversion.

Parameters
arrayPointer to the destination array.
countNumber of elements to read.
Exceptions
std::invalid_argumentIf array is nullptr.
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_u64()

template<Endianness E = Endianness::kLittle>
uint64_t intns::io::MemoryReader< E >::read_u64 ( )
inline

Reads an unsigned 64-bit integer with endianness conversion.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ read_u8()

template<Endianness E = Endianness::kLittle>
uint8_t intns::io::MemoryReader< E >::read_u8 ( )
inline

Reads an unsigned 8-bit integer.

Returns
The value read from the buffer.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the buffer.

◆ remaining()

template<Endianness E = Endianness::kLittle>
size_t intns::io::MemoryReader< E >::remaining ( ) const
inlinenoexcept

Returns the number of bytes remaining to be read.

Returns
The number of bytes from current position to end of buffer.

◆ set_position()

template<Endianness E = Endianness::kLittle>
void intns::io::MemoryReader< E >::set_position ( size_t  pos)
inlinenoexcept

Sets the read position within the buffer.

If the specified position exceeds the buffer size, it is clamped to the buffer size.

Parameters
posThe new position to set.
Note
This method never throws.

◆ size()

template<Endianness E = Endianness::kLittle>
size_t intns::io::MemoryReader< E >::size ( ) const
inlinenoexcept

Returns the total size of the buffer.

Returns
The size of the underlying buffer in bytes.

◆ skip()

template<Endianness E = Endianness::kLittle>
void intns::io::MemoryReader< E >::skip ( size_t  bytes)
inlinenoexcept

Advances the read position by the specified number of bytes.

If skipping would move beyond the buffer end, the position is set to the buffer end.

Parameters
bytesThe number of bytes to skip.
Note
This method never throws.

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