libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Ship::BinaryWriter Class Reference

Sequential binary writer with configurable byte-order support. More...

#include <BinaryWriter.h>

Public Member Functions

 BinaryWriter ()
 Constructs a BinaryWriter backed by a new, empty, growable MemoryStream.
 
 BinaryWriter (Stream *nStream)
 Constructs a BinaryWriter over a raw (non-owning) Stream pointer.
 
 BinaryWriter (std::shared_ptr< Stream > nStream)
 Constructs a BinaryWriter over a shared Stream.
 
void SetEndianness (Endianness endianness)
 Sets the byte order used for all subsequent multi-byte writes.
 
std::shared_ptr< StreamGetStream ()
 Returns the underlying stream (e.g. to pass to a BinaryReader after writing).
 
uint64_t GetBaseAddress ()
 Returns the current absolute write position.
 
uint64_t GetLength ()
 Returns the total number of bytes written to the stream.
 
void Seek (int32_t offset, SeekOffsetType seekType)
 Moves the stream write position.
 
void Close ()
 Closes the underlying stream and flushes any buffered data.
 
void Write (int8_t value)
 Writes a signed 8-bit integer.
 
void Write (uint8_t value)
 Writes an unsigned 8-bit integer.
 
void Write (int16_t value)
 Writes a signed 16-bit integer, applying the configured byte order.
 
void Write (uint16_t value)
 Writes an unsigned 16-bit integer, applying the configured byte order.
 
void Write (int32_t value)
 Writes a signed 32-bit integer, applying the configured byte order.
 
void Write (int32_t valueA, int32_t valueB)
 Writes two signed 32-bit integers sequentially, applying the configured byte order.
 
void Write (uint32_t value)
 Writes an unsigned 32-bit integer, applying the configured byte order.
 
void Write (int64_t value)
 Writes a signed 64-bit integer, applying the configured byte order.
 
void Write (uint64_t value)
 Writes an unsigned 64-bit integer, applying the configured byte order.
 
void Write (float value)
 Writes a 32-bit IEEE 754 float, applying the configured byte order.
 
void Write (double value)
 Writes a 64-bit IEEE 754 double, applying the configured byte order.
 
void Write (const std::string &str)
 Writes a length-prefixed UTF-8 string.
 
void Write (char *srcBuffer, size_t length)
 Writes length raw bytes from srcBuffer.
 
std::vector< char > ToVector ()
 Returns the entire stream contents as a vector of chars.
 

Protected Attributes

std::shared_ptr< StreammStream
 Underlying byte stream.
 
Endianness mEndianness = Endianness::Native
 Active byte order for multi-byte writes.
 

Detailed Description

Sequential binary writer with configurable byte-order support.

BinaryWriter wraps a Stream and provides typed write methods for all primitive integer and floating-point types as well as strings. The byte order used for multi-byte writes is configurable at runtime via SetEndianness().

The default constructor creates an internal MemoryStream that grows automatically; use GetStream() to retrieve the result or ToVector() for a raw copy.

Typical usage:

writer.SetEndianness(Ship::Endianness::Big);
writer.Write(static_cast<uint32_t>(0xDEADBEEF));
writer.Write(std::string("hello"));
auto bytes = writer.ToVector();
Sequential binary writer with configurable byte-order support.
Definition BinaryWriter.h:30
void Write(int8_t value)
Writes a signed 8-bit integer.
std::vector< char > ToVector()
Returns the entire stream contents as a vector of chars.
void SetEndianness(Endianness endianness)
Sets the byte order used for all subsequent multi-byte writes.

Constructor & Destructor Documentation

◆ BinaryWriter() [1/3]

Ship::BinaryWriter::BinaryWriter ( )

Constructs a BinaryWriter backed by a new, empty, growable MemoryStream.

◆ BinaryWriter() [2/3]

Ship::BinaryWriter::BinaryWriter ( Stream nStream)

Constructs a BinaryWriter over a raw (non-owning) Stream pointer.

Parameters
nStreamPointer to an already-constructed stream (ownership not transferred).

◆ BinaryWriter() [3/3]

Ship::BinaryWriter::BinaryWriter ( std::shared_ptr< Stream nStream)

Constructs a BinaryWriter over a shared Stream.

Parameters
nStreamShared stream to write into.

Member Function Documentation

◆ Close()

void Ship::BinaryWriter::Close ( )

Closes the underlying stream and flushes any buffered data.

◆ GetBaseAddress()

uint64_t Ship::BinaryWriter::GetBaseAddress ( )

Returns the current absolute write position.

◆ GetLength()

uint64_t Ship::BinaryWriter::GetLength ( )

Returns the total number of bytes written to the stream.

◆ GetStream()

std::shared_ptr< Stream > Ship::BinaryWriter::GetStream ( )

Returns the underlying stream (e.g. to pass to a BinaryReader after writing).

◆ Seek()

void Ship::BinaryWriter::Seek ( int32_t  offset,
SeekOffsetType  seekType 
)

Moves the stream write position.

Parameters
offsetByte offset.
seekTypeReference point (Start, Current, or End).

◆ SetEndianness()

void Ship::BinaryWriter::SetEndianness ( Endianness  endianness)

Sets the byte order used for all subsequent multi-byte writes.

Parameters
endiannessEndianness::Little, Big, or Native.

◆ ToVector()

std::vector< char > Ship::BinaryWriter::ToVector ( )

Returns the entire stream contents as a vector of chars.

◆ Write() [1/13]

void Ship::BinaryWriter::Write ( char *  srcBuffer,
size_t  length 
)

Writes length raw bytes from srcBuffer.

Parameters
srcBufferSource buffer.
lengthNumber of bytes to write.

◆ Write() [2/13]

void Ship::BinaryWriter::Write ( const std::string &  str)

Writes a length-prefixed UTF-8 string.

Writes a 32-bit unsigned length (in the configured byte order) followed by the string's bytes. No null terminator is written.

Parameters
strString to write.

◆ Write() [3/13]

void Ship::BinaryWriter::Write ( double  value)

Writes a 64-bit IEEE 754 double, applying the configured byte order.

◆ Write() [4/13]

void Ship::BinaryWriter::Write ( float  value)

Writes a 32-bit IEEE 754 float, applying the configured byte order.

◆ Write() [5/13]

void Ship::BinaryWriter::Write ( int16_t  value)

Writes a signed 16-bit integer, applying the configured byte order.

◆ Write() [6/13]

void Ship::BinaryWriter::Write ( int32_t  value)

Writes a signed 32-bit integer, applying the configured byte order.

◆ Write() [7/13]

void Ship::BinaryWriter::Write ( int32_t  valueA,
int32_t  valueB 
)

Writes two signed 32-bit integers sequentially, applying the configured byte order.

Parameters
valueAFirst integer.
valueBSecond integer.

◆ Write() [8/13]

void Ship::BinaryWriter::Write ( int64_t  value)

Writes a signed 64-bit integer, applying the configured byte order.

◆ Write() [9/13]

void Ship::BinaryWriter::Write ( int8_t  value)

Writes a signed 8-bit integer.

◆ Write() [10/13]

void Ship::BinaryWriter::Write ( uint16_t  value)

Writes an unsigned 16-bit integer, applying the configured byte order.

◆ Write() [11/13]

void Ship::BinaryWriter::Write ( uint32_t  value)

Writes an unsigned 32-bit integer, applying the configured byte order.

◆ Write() [12/13]

void Ship::BinaryWriter::Write ( uint64_t  value)

Writes an unsigned 64-bit integer, applying the configured byte order.

◆ Write() [13/13]

void Ship::BinaryWriter::Write ( uint8_t  value)

Writes an unsigned 8-bit integer.

Member Data Documentation

◆ mEndianness

Endianness Ship::BinaryWriter::mEndianness = Endianness::Native
protected

Active byte order for multi-byte writes.

◆ mStream

std::shared_ptr<Stream> Ship::BinaryWriter::mStream
protected

Underlying byte stream.


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