Skylicht Engine
Loading...
Searching...
No Matches
Skylicht::CMemoryStream Class Reference

In-memory byte stream for binary serialization and deserialization. More...

#include <Utils/CMemoryStream.h>

Public Member Functions

 CMemoryStream (unsigned int initMem=512)
 Create an owning memory stream.
 CMemoryStream (unsigned char *fromMem, unsigned int size)
 Wrap an existing memory block for reading or fixed-size writing.
 CMemoryStream (const CMemoryStream &stream)
 Copy a stream into a new backing buffer.
virtual ~CMemoryStream ()
 Destroy the stream and release owned memory.
bool autoGrow (unsigned int writeSize)
 Ensure the stream has enough capacity for an upcoming write.
void writeData (const void *data, unsigned int size)
 Append raw bytes to the stream.
void writeStream (CMemoryStream *stream)
 Append the full contents of another memory stream.
void writeChar (char data)
 Append a signed 8-bit character.
void writeShort (short data)
 Append a signed 16-bit integer.
void writeUShort (unsigned short data)
 Append an unsigned 16-bit integer.
void writeInt (int data)
 Append a signed 32-bit integer.
void writeUInt (unsigned int data)
 Append an unsigned 32-bit integer.
void writeFloat (float data)
 Append a 32-bit floating-point value.
void writeDouble (double data)
 Append a 64-bit floating-point value.
void writeString (const std::string &s)
 Append a UTF-8/narrow string with its character count prefix.
void writeString (const std::wstring &s)
 Append a wide string with its character count prefix.
void writeFloatArray (const float *f, int count)
 Append an array of floats.
unsigned int readData (void *data, unsigned int size)
 Read raw bytes from the current read position.
char readChar ()
 Read a signed 8-bit character and advance the read position.
short readShort ()
 Read a signed 16-bit integer and advance the read position.
unsigned short readUShort ()
 Read an unsigned 16-bit integer and advance the read position.
int readInt ()
 Read a signed 32-bit integer and advance the read position.
unsigned int readUInt ()
 Read an unsigned 32-bit integer and advance the read position.
float readFloat ()
 Read a 32-bit floating-point value and advance the read position.
double readDouble ()
 Read a 64-bit floating-point value and advance the read position.
std::string readString ()
 Read a narrow string written by writeString(const std::string&).
std::wstring readWString ()
 Read a wide string written by writeString(const std::wstring&).
void readFloatArray (float *f, int count)
 Read an array of floats and advance the read position.
unsigned char * getData ()
 Get the backing memory pointer.
unsigned int getSize ()
 Get the number of bytes written or available for reading.
unsigned int getPos ()
 Get the current read position.
unsigned int getTotalSize ()
 Get the total allocated capacity.
void setPos (unsigned int pos)
 Set the current read position.
void resetWrite ()
 Clear written data and reset the read position.
void encrypt ()
 Encrypt the stream contents.
void decrypt ()
 Decrypt the stream contents.

Protected Attributes

unsigned char * m_memory
unsigned int m_size
unsigned int m_totalSize
unsigned int m_pos
bool m_fromMemory

Detailed Description

In-memory byte stream for binary serialization and deserialization.

A stream can own its backing buffer or wrap an external memory block. Streams created from external memory cannot grow automatically.

Constructor & Destructor Documentation

◆ CMemoryStream() [1/3]

Skylicht::CMemoryStream::CMemoryStream ( unsigned int initMem = 512)

Create an owning memory stream.

Parameters
initMemInitial buffer capacity in bytes.

◆ CMemoryStream() [2/3]

Skylicht::CMemoryStream::CMemoryStream ( unsigned char * fromMem,
unsigned int size )

Wrap an existing memory block for reading or fixed-size writing.

Parameters
fromMemExternal memory block. The stream does not delete this pointer.
sizeSize of the external memory block in bytes.

◆ CMemoryStream() [3/3]

Skylicht::CMemoryStream::CMemoryStream ( const CMemoryStream & stream)

Copy a stream into a new backing buffer.

The copy starts with read position 0 and contains the used bytes from the source. Ownership behavior follows the implementation in the source file.

Parameters
streamSource stream.

Member Function Documentation

◆ autoGrow()

bool Skylicht::CMemoryStream::autoGrow ( unsigned int writeSize)

Ensure the stream has enough capacity for an upcoming write.

Parameters
writeSizeNumber of bytes that will be appended.
Returns
True if the stream has capacity, false for fixed external memory.

◆ decrypt()

void Skylicht::CMemoryStream::decrypt ( )

Decrypt the stream contents.

Currently implemented as a no-op.

◆ encrypt()

void Skylicht::CMemoryStream::encrypt ( )

Encrypt the stream contents.

Currently implemented as a no-op.

◆ getData()

unsigned char * Skylicht::CMemoryStream::getData ( )
inline

Get the backing memory pointer.

Returns
Pointer to the stream buffer.

◆ getPos()

unsigned int Skylicht::CMemoryStream::getPos ( )
inline

Get the current read position.

Returns
Current position in bytes.

◆ getSize()

unsigned int Skylicht::CMemoryStream::getSize ( )
inline

Get the number of bytes written or available for reading.

Returns
Used stream size in bytes.

◆ getTotalSize()

unsigned int Skylicht::CMemoryStream::getTotalSize ( )
inline

Get the total allocated capacity.

Returns
Buffer capacity in bytes.

◆ readData()

unsigned int Skylicht::CMemoryStream::readData ( void * data,
unsigned int size )

Read raw bytes from the current read position.

Parameters
dataDestination buffer.
sizeRequested number of bytes.
Returns
Number of bytes actually read.

◆ readFloatArray()

void Skylicht::CMemoryStream::readFloatArray ( float * f,
int count )

Read an array of floats and advance the read position.

Parameters
fDestination float array.
countNumber of float values to read.

◆ setPos()

void Skylicht::CMemoryStream::setPos ( unsigned int pos)
inline

Set the current read position.

Parameters
posNew byte position.

◆ writeData()

void Skylicht::CMemoryStream::writeData ( const void * data,
unsigned int size )

Append raw bytes to the stream.

Parameters
dataSource data.
sizeNumber of bytes to write.

◆ writeFloatArray()

void Skylicht::CMemoryStream::writeFloatArray ( const float * f,
int count )

Append an array of floats.

Parameters
fSource float array.
countNumber of float values.

◆ writeStream()

void Skylicht::CMemoryStream::writeStream ( CMemoryStream * stream)

Append the full contents of another memory stream.

Parameters
streamSource stream.

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