LibCppBinary
Modern cross-platform C++ binary parsing library
Loading...
Searching...
No Matches
Binary::BufferStream Class Reference

Represents a memory buffer that can be used as a stream. More...

#include <BufferStream.h>

Inheritance diagram for Binary::BufferStream:
Binary::RawField Binary::Stream Binary::DataField

Public Member Functions

 BufferStream (size_t size)
 Creates a new instance of BufferStream.
void Read (DataField *field) const override
 Reads data from the stream into the specified field.
void Read (DataStructure *structure) const override
 Reads data from stream into the specified data structure.
std::shared_ptr< ChunkHeaderFindNextChunk (std::string id) const override
 Finds the next chunk header with the specified ID.
void Write (const DataField *field) override
 Writes data to the stream from the specified field.
void Write (const DataStructure *structure) override
 Writes the specified structure to the stream.
size_t Position () const override
 Gets the current position in the stream.
void SetPosition (size_t position) const override
 Sets the current position in the stream.
size_t Beginning () const override
 Gets the beginning position of the file.
size_t End () const override
 Gets the end position of the file.
Public Member Functions inherited from Binary::RawField
 RawField (size_t size)
 Constructor; creates a new instance of Field.
 RawField (const RawField &other)
 Copy constructor; creates a deep copy of the field.
size_t Size () const override
 Gets the size of the data in the field.
char * RawData () override
 Provides access to the field's raw data via pointer.
const char * RawData () const override
 Provides access to the field's raw data via const pointer.
std::string ToString () const override
 Converts the field's data to a string representation.
std::string ToString (StringFormat format) const override
 Converts the field's data to a string representation.
RawFieldoperator= (const RawField &other)
 Assignment operator for RawField.
Public Member Functions inherited from Binary::DataField
virtual ~DataField ()=default
 Default destructor; properly destroys the instance.
virtual void CopyRawDataTo (DataField *other)
 Copies the raw data to another DataField.
Public Member Functions inherited from Binary::Stream
virtual ~Stream ()=default
 Default destructor; properly destroys the instance.

Additional Inherited Members

Protected Member Functions inherited from Binary::RawField
std::string FormatData (StringFormat format) const
 Format's the field's data as a string in a specific format.
Protected Attributes inherited from Binary::RawField
size_t size
std::unique_ptr< char[]> rawData

Detailed Description

Represents a memory buffer that can be used as a stream.

Rather than reading or writing individual fields or structures to a file, you can use this class to read or write a large block of data to or from memory, which can improve performance. Individual fields and structures can then be read from or written to the buffer as needed.

Invariant
Size must be greater than 0.
Position must be between 0 and Size, inclusive.
Buffer memory must always be valid and allocated to size.
Reading advances the position by field or structure size.
Reading does not modify the buffer's contents.
Writing both advances the position and modifies buffer.

Constructor & Destructor Documentation

◆ BufferStream()

Binary::BufferStream::BufferStream ( size_t size)
inline

Creates a new instance of BufferStream.

Parameters
sizeThe size of the buffer, in bytes.
Precondition
Size must be greater than 0.

Member Function Documentation

◆ Beginning()

size_t Binary::BufferStream::Beginning ( ) const
inlineoverridevirtual

Gets the beginning position of the file.

Returns
A size_t value representing the beginning position.

Implements Binary::Stream.

◆ End()

size_t Binary::BufferStream::End ( ) const
inlineoverridevirtual

Gets the end position of the file.

Returns
A size_t value representing the end position.

Implements Binary::Stream.

◆ FindNextChunk()

std::shared_ptr< ChunkHeader > Binary::BufferStream::FindNextChunk ( std::string id) const
overridevirtual

Finds the next chunk header with the specified ID.

Parameters
idThe ID of the chunk to find.
Returns
A pointer to the chunk header if found, otherwise nullptr.
Precondition
id must be exactly 4 characters long.
Postcondition
Position is advanced to the beginning of found chunk header.
Exceptions
std::invalid_argumentif id is not 4 characters long.

Implements Binary::Stream.

◆ Position()

size_t Binary::BufferStream::Position ( ) const
inlineoverridevirtual

Gets the current position in the stream.

Returns
A size_t representing the position.

Implements Binary::Stream.

◆ Read() [1/2]

void Binary::BufferStream::Read ( DataField * field) const
overridevirtual

Reads data from the stream into the specified field.

Parameters
fieldA pointer to the field to read data into.
Precondition
Field must not be null.
Field size must be <= remaining buffer size.
Postcondition
Position is advanced by field size.
Specified field is updated with the data read from the buffer.
Exceptions
std::out_of_rangeif field size > remaining buffer size.
std::invalid_argumentif field is null.

Implements Binary::Stream.

◆ Read() [2/2]

void Binary::BufferStream::Read ( DataStructure * structure) const
overridevirtual

Reads data from stream into the specified data structure.

Parameters
structureA pointer to the data structure to read data into.
Precondition
Structure must not be null.
Structure size must be <= remaining buffer size.
Postcondition
Position is advanced by structure size.
Specified structure is updated with the data read from buffer.
Exceptions
std::out_of_rangeif structure size > remaining buffer size.
std::invalid_argumentif structure is null.

Implements Binary::Stream.

◆ SetPosition()

void Binary::BufferStream::SetPosition ( size_t position) const
overridevirtual

Sets the current position in the stream.

Parameters
positionThe position value to set.
Precondition
Position must be between 0 and size, inclusive.
Postcondition
Position is updated to the specified value.
Exceptions
std::out_of_rangeif position > size.

Implements Binary::Stream.

◆ Write() [1/2]

void Binary::BufferStream::Write ( const DataField * field)
overridevirtual

Writes data to the stream from the specified field.

Parameters
fieldA pointer to the field to write to the stream.
Precondition
Field must not be null.
Field size must be <= remaining buffer size.
Postcondition
Position is advanced by field size.
Buffer is updated with the data from the specified field.
Exceptions
std::out_of_rangeif field size > remaining buffer size.
std::invalid_argumentif field is null.

Implements Binary::Stream.

◆ Write() [2/2]

void Binary::BufferStream::Write ( const DataStructure * structure)
overridevirtual

Writes the specified structure to the stream.

Parameters
structureA pointer to the structure to write to the stream.
Precondition
Structure must not be null.
Structure size must be <= remaining buffer size.
Postcondition
Position is advanced by structure size.
Buffer is updated with the data from the specified structure.
Exceptions
std::out_of_rangeif structure size > remaining buffer size.
std::invalid_argumentif structure is null.

Implements Binary::Stream.


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