Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Divide::ByteBuffer Class Reference

#include <ByteBuffer.h>

+ Inheritance diagram for Divide::ByteBuffer:

Public Member Functions

 ByteBuffer () noexcept=default
 
void clear () noexcept
 Resets the entire storage and the read and write positions.
 
template<class T , size_t N>
void addMarker (const T(&pattern)[N])
 Add a specific list of items in order to mark a special point in the buffer. Can be used for skipping entire blocks later.
 
template<class T , size_t N>
void readSkipToMarker (const T(&pattern)[N])
 Skip (consume) everything in the buffer until we find the specified list of items. If the markers aren't found, the buffer will skip to the end!
 
template<typename T >
void put (size_t pos, const T &value)
 Inserts 'value' at the specified position overriding any existing data at that location. Does not allocate storage!
 
template<typename T >
ByteBufferoperator<< (const T &value)
 Appends 'value' to the buffer.
 
template<typename T >
ByteBufferoperator>> (T &value)
 Reads sizeof(T) data from the buffer and saves its contents into 'value'. Reading moves the read head forward!
 
template<typename U >
ByteBufferoperator>> (Unused< U > &value)
 Can be used as an alternative for readSkip<U>() to preserve local coding conventions.
 
template<typename T >
void readSkip ()
 Moves the read head sizeof(T) units forward.
 
void readSkip (size_t skip) noexcept
 Moves the read head 'skip' units forward.
 
template<typename T >
void readNoSkip (T &value)
 Reads sizeof(T) data from the buffer and saves it into 'value' but does NOT move the read head forward!
 
template<typename T >
void read (T &out)
 Reads sizeof(T) data from the buffer and returns it. Reading moves the read head forward!
 
template<typename T >
void readNoSkipFrom (size_t pos, T &out) const
 Reads sizeof(T) data from the buffer and returns it but does NOT move the read head forward!
 
void read (Byte *dest, size_t len)
 Reads 'len' bytes of data from the buffer and memcpy's it into dest. Reading moves the read head forward!
 
void readPackXYZ (F32 &x, F32 &y, F32 &z)
 Reads a packed U32 from the buffer and unpackes it into x,y,z. Reading moves the read head forward!
 
void appendPackXYZ (F32 x, F32 y, F32 z)
 Packes x,y and z into a single U32 and appends it to the buffer.
 
U64 readPackGUID ()
 Reads a packed U64 from the buffer and returns it. Reading moves the read head forward!
 
void appendPackGUID (U64 guid)
 Packes guid into a multiple I32s and appends them to the buffer.
 
void append (const Byte *src, size_t cnt)
 Appends 'cnt' bytes from 'src' to the buffer.
 
template<typename T >
void append (const T *src, const size_t cnt)
 Appends (sizeof(T) * 'cnt') bytes from 'src' to the buffer.
 
Byte operator[] (size_t pos) const
 Read the byte at position 'pos' from the buffer without moving the read head.
 
size_t rpos () const noexcept
 Returns the current read head position.
 
size_t rpos (size_t rpos_) noexcept
 
size_t wpos () const noexcept
 Returns the current write head position.
 
size_t wpos (size_t wpos_) noexcept
 Returns the current write head position.
 
size_t bufferSize () const noexcept
 Returns the size (in bytes) of the data inside of the buffer (wpos - rpos)
 
bool bufferEmpty () const noexcept
 Returns true if the read position and the write position are identical.
 
size_t storageSize () const noexcept
 Returns the total size (in bytes) of the underlying storage, regardles of wpos and rpos.
 
bool storageEmpty () const noexcept
 Returns true if the underlying storage is empty, regardless of wpos and rpos.
 
void resize (size_t newsize)
 Resizes the underlying storage to 'newsize' bytes and resets the read and write head positions.
 
void reserve (size_t resize)
 Reserves 'resize' bytes of additional storage in the underlying storage structure without changing the read and write head positions.
 
const Bytecontents () const noexcept
 Returns a raw pointer to the underlying storage data. Does NOT depend on the read head position! (use contents() + rpos() for that)
 
void put (size_t pos, const Byte *src, size_t cnt)
 Overrides existing data in the buffer starting ad position 'pos' by memcpy-ing 'cnt' bytes from 'src'.
 
bool dumpToFile (const ResourcePath &path, std::string_view fileName, const U8 version=BUFFER_FORMAT_VERSION)
 Saves the entire buffer contents to file. Always appends the version at the end of the file.
 
bool loadFromFile (const ResourcePath &path, std::string_view fileName, const U8 version=BUFFER_FORMAT_VERSION)
 
template<>
ByteBufferoperator>> (bool &value)
 
template<>
ByteBufferoperator>> (string &value)
 
template<>
void readNoSkip (bool &value)
 
template<>
void readNoSkip (string &value)
 
template<>
void readNoSkip (ResourcePath &value)
 
template<>
void readSkip ()
 
template<>
void readSkip ()
 
template<>
void readSkip ()
 
template<>
void append (const string &str)
 
template<>
void append (const ResourcePath &str)
 
template<>
void append (const bool &value)
 
template<>
void append (const ByteBuffer &buffer)
 

Static Public Attributes

static const U8 BUFFER_FORMAT_VERSION { 10u }
 
static const size_t DEFAULT_SIZE { 0x1000 }
 

Protected Attributes

size_t _rpos = 0u
 
size_t _wpos = 0u
 
vector< Byte_storage
 

Private Member Functions

template<typename T >
void append (const T &value)
 Limited for internal use because can "append" any unexpected type (e.g. a pointer) with hard detection problem.
 

Detailed Description

Definition at line 74 of file ByteBuffer.h.

Constructor & Destructor Documentation

◆ ByteBuffer()

Divide::ByteBuffer::ByteBuffer ( )
defaultnoexcept

Member Function Documentation

◆ addMarker()

template<class T , size_t N>
void Divide::ByteBuffer::addMarker ( const T(&)  pattern[N])

Add a specific list of items in order to mark a special point in the buffer. Can be used for skipping entire blocks later.

Definition at line 56 of file ByteBuffer.inl.

◆ append() [1/7]

template<>
void Divide::ByteBuffer::append ( const bool &  value)
inline

Definition at line 411 of file ByteBuffer.inl.

◆ append() [2/7]

void Divide::ByteBuffer::append ( const Byte src,
size_t  cnt 
)

Appends 'cnt' bytes from 'src' to the buffer.

Definition at line 33 of file ByteBuffer.cpp.

◆ append() [3/7]

template<>
void Divide::ByteBuffer::append ( const ByteBuffer buffer)
inline

Definition at line 416 of file ByteBuffer.inl.

◆ append() [4/7]

template<>
void Divide::ByteBuffer::append ( const ResourcePath str)
inline

Definition at line 406 of file ByteBuffer.inl.

◆ append() [5/7]

template<>
void Divide::ByteBuffer::append ( const string str)
inline

Definition at line 400 of file ByteBuffer.inl.

◆ append() [6/7]

template<typename T >
void Divide::ByteBuffer::append ( const T &  value)
private

Limited for internal use because can "append" any unexpected type (e.g. a pointer) with hard detection problem.

Definition at line 389 of file ByteBuffer.inl.

◆ append() [7/7]

template<typename T >
void Divide::ByteBuffer::append ( const T *  src,
const size_t  cnt 
)

Appends (sizeof(T) * 'cnt') bytes from 'src' to the buffer.

Definition at line 394 of file ByteBuffer.inl.

◆ appendPackGUID()

void Divide::ByteBuffer::appendPackGUID ( U64  guid)
inline

Packes guid into a multiple I32s and appends them to the buffer.

Definition at line 305 of file ByteBuffer.inl.

◆ appendPackXYZ()

void Divide::ByteBuffer::appendPackXYZ ( F32  x,
F32  y,
F32  z 
)
inline

Packes x,y and z into a single U32 and appends it to the buffer.

Definition at line 296 of file ByteBuffer.inl.

◆ bufferEmpty()

bool Divide::ByteBuffer::bufferEmpty ( ) const
inlinenoexcept

Returns true if the read position and the write position are identical.

Definition at line 353 of file ByteBuffer.inl.

◆ bufferSize()

size_t Divide::ByteBuffer::bufferSize ( ) const
inlinenoexcept

Returns the size (in bytes) of the data inside of the buffer (wpos - rpos)

Definition at line 349 of file ByteBuffer.inl.

◆ clear()

void Divide::ByteBuffer::clear ( )
noexcept

Resets the entire storage and the read and write positions.

Definition at line 28 of file ByteBuffer.cpp.

◆ contents()

const Byte * Divide::ByteBuffer::contents ( ) const
inlinenoexcept

Returns a raw pointer to the underlying storage data. Does NOT depend on the read head position! (use contents() + rpos() for that)

Definition at line 377 of file ByteBuffer.inl.

◆ dumpToFile()

bool Divide::ByteBuffer::dumpToFile ( const ResourcePath path,
std::string_view  fileName,
const U8  version = BUFFER_FORMAT_VERSION 
)

Saves the entire buffer contents to file. Always appends the version at the end of the file.

Definition at line 47 of file ByteBuffer.cpp.

◆ loadFromFile()

bool Divide::ByteBuffer::loadFromFile ( const ResourcePath path,
std::string_view  fileName,
const U8  version = BUFFER_FORMAT_VERSION 
)

Reads the specified file and loads its contents as raw data into the buffer. Returns FALSE if reading of the file failed OR if the version doesn't match To skip version checking, pass 0u as the version! This will erase any existing data inside of the buffer

Definition at line 57 of file ByteBuffer.cpp.

◆ operator<<()

template<typename T >
ByteBuffer & Divide::ByteBuffer::operator<< ( const T &  value)

Appends 'value' to the buffer.

Definition at line 112 of file ByteBuffer.inl.

◆ operator>>() [1/4]

template<>
ByteBuffer & Divide::ByteBuffer::operator>> ( bool &  value)
inline

Definition at line 126 of file ByteBuffer.inl.

◆ operator>>() [2/4]

template<>
ByteBuffer & Divide::ByteBuffer::operator>> ( string value)
inline

Definition at line 135 of file ByteBuffer.inl.

◆ operator>>() [3/4]

template<typename T >
ByteBuffer & Divide::ByteBuffer::operator>> ( T &  value)

Reads sizeof(T) data from the buffer and saves its contents into 'value'. Reading moves the read head forward!

Definition at line 119 of file ByteBuffer.inl.

◆ operator>>() [4/4]

template<typename U >
ByteBuffer & Divide::ByteBuffer::operator>> ( Unused< U > &  value)

Can be used as an alternative for readSkip<U>() to preserve local coding conventions.

Definition at line 197 of file ByteBuffer.inl.

◆ operator[]()

Byte Divide::ByteBuffer::operator[] ( size_t  pos) const
inline

Read the byte at position 'pos' from the buffer without moving the read head.

Definition at line 325 of file ByteBuffer.inl.

◆ put() [1/2]

void Divide::ByteBuffer::put ( size_t  pos,
const Byte src,
size_t  cnt 
)
inline

Overrides existing data in the buffer starting ad position 'pos' by memcpy-ing 'cnt' bytes from 'src'.

Definition at line 381 of file ByteBuffer.inl.

◆ put() [2/2]

template<typename T >
void Divide::ByteBuffer::put ( size_t  pos,
const T &  value 
)

Inserts 'value' at the specified position overriding any existing data at that location. Does not allocate storage!

Definition at line 105 of file ByteBuffer.inl.

◆ read() [1/2]

void Divide::ByteBuffer::read ( Byte dest,
size_t  len 
)
inline

Reads 'len' bytes of data from the buffer and memcpy's it into dest. Reading moves the read head forward!

Definition at line 256 of file ByteBuffer.inl.

◆ read() [2/2]

template<typename T >
void Divide::ByteBuffer::read ( T &  out)

Reads sizeof(T) data from the buffer and returns it. Reading moves the read head forward!

Definition at line 239 of file ByteBuffer.inl.

◆ readNoSkip() [1/4]

template<>
void Divide::ByteBuffer::readNoSkip ( bool &  value)
inline

Definition at line 160 of file ByteBuffer.inl.

◆ readNoSkip() [2/4]

template<>
void Divide::ByteBuffer::readNoSkip ( ResourcePath value)
inline

Definition at line 189 of file ByteBuffer.inl.

◆ readNoSkip() [3/4]

template<>
void Divide::ByteBuffer::readNoSkip ( string value)
inline

Definition at line 167 of file ByteBuffer.inl.

◆ readNoSkip() [4/4]

template<typename T >
void Divide::ByteBuffer::readNoSkip ( T &  value)

Reads sizeof(T) data from the buffer and saves it into 'value' but does NOT move the read head forward!

Definition at line 154 of file ByteBuffer.inl.

◆ readNoSkipFrom()

template<typename T >
void Divide::ByteBuffer::readNoSkipFrom ( size_t  pos,
T &  out 
) const

Reads sizeof(T) data from the buffer and returns it but does NOT move the read head forward!

Definition at line 246 of file ByteBuffer.inl.

◆ readPackGUID()

U64 Divide::ByteBuffer::readPackGUID ( )
inline

Reads a packed U64 from the buffer and returns it. Reading moves the read head forward!

Definition at line 276 of file ByteBuffer.inl.

◆ readPackXYZ()

void Divide::ByteBuffer::readPackXYZ ( F32 x,
F32 y,
F32 z 
)
inline

Reads a packed U32 from the buffer and unpackes it into x,y,z. Reading moves the read head forward!

Definition at line 267 of file ByteBuffer.inl.

◆ readSkip() [1/5]

template<typename T >
void Divide::ByteBuffer::readSkip

Moves the read head sizeof(T) units forward.

Definition at line 204 of file ByteBuffer.inl.

◆ readSkip() [2/5]

template<>
void Divide::ByteBuffer::readSkip ( )
inline

Definition at line 210 of file ByteBuffer.inl.

◆ readSkip() [3/5]

template<>
void Divide::ByteBuffer::readSkip ( )
inline

Definition at line 217 of file ByteBuffer.inl.

◆ readSkip() [4/5]

template<>
void Divide::ByteBuffer::readSkip ( )
inline

Definition at line 223 of file ByteBuffer.inl.

◆ readSkip() [5/5]

void Divide::ByteBuffer::readSkip ( size_t  skip)
inlinenoexcept

Moves the read head 'skip' units forward.

Definition at line 228 of file ByteBuffer.inl.

◆ readSkipToMarker()

template<class T , size_t N>
void Divide::ByteBuffer::readSkipToMarker ( const T(&)  pattern[N])

Skip (consume) everything in the buffer until we find the specified list of items. If the markers aren't found, the buffer will skip to the end!

Definition at line 65 of file ByteBuffer.inl.

◆ reserve()

void Divide::ByteBuffer::reserve ( size_t  resize)
inline

Reserves 'resize' bytes of additional storage in the underlying storage structure without changing the read and write head positions.

Definition at line 371 of file ByteBuffer.inl.

◆ resize()

void Divide::ByteBuffer::resize ( size_t  newsize)
inline

Resizes the underlying storage to 'newsize' bytes and resets the read and write head positions.

Definition at line 365 of file ByteBuffer.inl.

◆ rpos() [1/2]

size_t Divide::ByteBuffer::rpos ( ) const
inlinenoexcept

Returns the current read head position.

Definition at line 331 of file ByteBuffer.inl.

◆ rpos() [2/2]

size_t Divide::ByteBuffer::rpos ( size_t  rpos_)
inlinenoexcept

Returns the current read head position Returns the current read head position

Definition at line 335 of file ByteBuffer.inl.

◆ storageEmpty()

bool Divide::ByteBuffer::storageEmpty ( ) const
inlinenoexcept

Returns true if the underlying storage is empty, regardless of wpos and rpos.

Definition at line 361 of file ByteBuffer.inl.

◆ storageSize()

size_t Divide::ByteBuffer::storageSize ( ) const
inlinenoexcept

Returns the total size (in bytes) of the underlying storage, regardles of wpos and rpos.

Definition at line 357 of file ByteBuffer.inl.

◆ wpos() [1/2]

size_t Divide::ByteBuffer::wpos ( ) const
inlinenoexcept

Returns the current write head position.

Definition at line 340 of file ByteBuffer.inl.

◆ wpos() [2/2]

size_t Divide::ByteBuffer::wpos ( size_t  wpos_)
inlinenoexcept

Returns the current write head position.

Definition at line 344 of file ByteBuffer.inl.

Member Data Documentation

◆ _rpos

size_t Divide::ByteBuffer::_rpos = 0u
protected

Definition at line 192 of file ByteBuffer.h.

◆ _storage

vector<Byte> Divide::ByteBuffer::_storage
protected

Definition at line 193 of file ByteBuffer.h.

◆ _wpos

size_t Divide::ByteBuffer::_wpos = 0u
protected

Definition at line 192 of file ByteBuffer.h.

◆ BUFFER_FORMAT_VERSION

const U8 Divide::ByteBuffer::BUFFER_FORMAT_VERSION { 10u }
static

Definition at line 76 of file ByteBuffer.h.

◆ DEFAULT_SIZE

const size_t Divide::ByteBuffer::DEFAULT_SIZE { 0x1000 }
static

Definition at line 77 of file ByteBuffer.h.


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