Libftpp
A modern C++ library
DataBuffer Class Reference

A simple LIFO data buffer for serialization and deserialization for simple data types and std::string. More...

#include <data_buffer.hpp>

Public Member Functions

 DataBuffer ()
 
 ~DataBuffer ()=default
 
const std::vector< unsigned char > data () const
 
void increaseCursor (size_t amount) const
 Increase the read/write cursor by a specified amount. More...
 
void decreaseCursor (size_t amount) const
 Decrease the read/write cursor by a specified amount. More...
 
void reset () const
 Reset the read/write cursor to the beginning of the buffer. More...
 
void clear ()
 Clear the buffer and reset the cursor. More...
 
size_t size () const
 
void append (const unsigned char *data, size_t len)
 
DataBufferoperator<< (const std::string &value)
 
const DataBufferoperator>> (std::string &value) const
 
template<typename T >
DataBufferoperator<< (const T &value)
 
template<typename T >
const DataBufferoperator>> (T &value) const
 

Detailed Description

A simple LIFO data buffer for serialization and deserialization for simple data types and std::string.

DataBuffer buffer;
int intValue = 42;
float floatValue = 3.14f;
std::string strValue = "Hello, DataBuffer!";
buffer << intValue;
buffer << floatValue;
buffer << strValue;
int readInt;
float readFloat;
std::string readStr;
buffer >> readStr;
buffer >> readFloat;
buffer >> readInt;
A simple LIFO data buffer for serialization and deserialization for simple data types and std::string...
Definition: data_buffer.hpp:37
Exceptions
std::out_of_rangeThrown when trying to read more data than available in the buffer.

Definition at line 36 of file data_buffer.hpp.

Constructor & Destructor Documentation

◆ DataBuffer()

DataBuffer::DataBuffer ( )

Definition at line 3 of file data_buffer.cpp.

◆ ~DataBuffer()

DataBuffer::~DataBuffer ( )
default

Member Function Documentation

◆ append()

void DataBuffer::append ( const unsigned char *  data,
size_t  len 
)

Definition at line 22 of file data_buffer.cpp.

References data().

Referenced by Message::appendBytes().

◆ clear()

void DataBuffer::clear ( )

Clear the buffer and reset the cursor.

Definition at line 16 of file data_buffer.cpp.

◆ data()

const std::vector< unsigned char > DataBuffer::data ( ) const

Definition at line 57 of file data_buffer.cpp.

Referenced by append(), and Message::getSerializedData().

◆ decreaseCursor()

void DataBuffer::decreaseCursor ( size_t  amount) const

Decrease the read/write cursor by a specified amount.

Parameters
amountThe amount to decrease the cursor by.

Usefull with const data() to move the cursor backward after reading data.

Definition at line 80 of file data_buffer.cpp.

Referenced by Message::decr_cursor(), and Message::isComplet().

◆ increaseCursor()

void DataBuffer::increaseCursor ( size_t  amount) const

Increase the read/write cursor by a specified amount.

Parameters
amountThe amount to increase the cursor by.

Usefull with const data() to move the cursor forward after reading data.

Definition at line 67 of file data_buffer.cpp.

Referenced by Message::incr_cursor().

◆ operator<<() [1/2]

DataBuffer & DataBuffer::operator<< ( const std::string &  value)

Definition at line 29 of file data_buffer.cpp.

References size().

◆ operator<<() [2/2]

template<typename T >
DataBuffer& DataBuffer::operator<< ( const T &  value)
inline

Definition at line 60 of file data_buffer.hpp.

◆ operator>>() [1/2]

const DataBuffer & DataBuffer::operator>> ( std::string &  value) const

Definition at line 38 of file data_buffer.cpp.

References size().

◆ operator>>() [2/2]

template<typename T >
const DataBuffer& DataBuffer::operator>> ( T &  value) const
inline

Definition at line 68 of file data_buffer.hpp.

◆ reset()

void DataBuffer::reset ( ) const

Reset the read/write cursor to the beginning of the buffer.

Definition at line 8 of file data_buffer.cpp.

Referenced by Message::reset().

◆ size()

size_t DataBuffer::size ( ) const