Fawkes API  Fawkes Development Version
fawkes::DynamicBuffer Class Reference

Dynamically growing buffer. More...

#include <>>

Public Member Functions

 DynamicBuffer (dynamic_list_t *db, size_t initial_buffer_size=1024)
 Write constructor. More...
 
 DynamicBuffer (dynamic_list_t *db, void *buf, size_t size)
 Read constructor. More...
 
virtual ~DynamicBuffer ()
 Destructor. More...
 
void append (const void *data, size_t data_size)
 Append data. More...
 
void * buffer ()
 Get pointer to buffer. More...
 
size_t buffer_size ()
 Get buffer size. More...
 
unsigned int num_elements ()
 Get number of elements. More...
 
size_t real_buffer_size ()
 Get real buffer size. More...
 
bool has_next ()
 Check if another element is available. More...
 
void * next (size_t *size)
 Get next buffer. More...
 
void reset_iterator ()
 Reset iterator. More...
 

Detailed Description

Dynamically growing buffer.

This class maintains a list or arbitrary data objects stuffed into one consecutive memory. The buffer layout is like the following: A dynamic_list_t element is supplied and can reside anywhere you like (in the case of the Fawkes network protocol in your static struct). It contains information about the size and number of elements of the list. The list itself is formed by concatenated memory regions, each preceeded by a two byte length value.

The list may be at most 4 GB in total size (including in-between headers) Each list item by itself can be at most 64 KB in size. The buffer starts with an initial size. If this initial size is exceeded the buffer size is doubled. If the double size would exceed 4 GB it is increased to exactly 4 GB.

The numbers in the headers are stored in network byte order and thus are suitable for direct sending over the network.

Author
Tim Niemueller

Definition at line 46 of file dynamic_buffer.h.

Constructor & Destructor Documentation

◆ DynamicBuffer() [1/2]

fawkes::DynamicBuffer::DynamicBuffer ( dynamic_list_t db,
size_t  initial_buffer_size = 1024 
)

Write constructor.

Use this constructor to create and write to this dynamic buffer.

Parameters
dbdynamic list header in your message
initial_buffer_sizeinitial buffer size to use, by default 1 KB

Definition at line 64 of file dynamic_buffer.cpp.

References fawkes::dynamic_list_t::num_elements, and fawkes::dynamic_list_t::size.

◆ DynamicBuffer() [2/2]

fawkes::DynamicBuffer::DynamicBuffer ( dynamic_list_t db,
void *  buf,
size_t  size 
)

Read constructor.

Use this constructor to read from a dynamic buffer.

Parameters
dbdynamic list header in the incoming message
bufbuffer to parse
sizesize of the buffer

Definition at line 88 of file dynamic_buffer.cpp.

◆ ~DynamicBuffer()

fawkes::DynamicBuffer::~DynamicBuffer ( )
virtual

Destructor.

If in writing mode frees up the buffer.

Definition at line 107 of file dynamic_buffer.cpp.

Member Function Documentation

◆ append()

void fawkes::DynamicBuffer::append ( const void *  data,
size_t  data_size 
)

Append data.

Appends data to the list. Throws an exception if there is not enough memory to hold the data or if in read-only mode.

Parameters
datadata to append
data_sizesize of the data in bytes
Exceptions
AccessViolationExceptionthrown if buffer is in read-only mode
IllegalArgumentExceptionthrown if data size is bigger than 64 KB - 2 bytes
OutOfMemoryExceptionthrown if no memory could be allocated

Definition at line 123 of file dynamic_buffer.cpp.

References fawkes::dynamic_list_t::num_elements, and fawkes::dynamic_list_t::size.

Referenced by fawkes::PluginListMessage::append(), fawkes::ConfigListContent::append(), and fawkes::BlackBoardInterfaceListContent::append_interface().

◆ buffer()

void * fawkes::DynamicBuffer::buffer ( )

Get pointer to buffer.

Returns
packed buffer

Definition at line 159 of file dynamic_buffer.cpp.

Referenced by fawkes::PluginListMessage::serialize(), fawkes::ConfigListContent::serialize(), and fawkes::BlackBoardInterfaceListContent::serialize().

◆ buffer_size()

size_t fawkes::DynamicBuffer::buffer_size ( )

Get buffer size.

Gets the size of the used part of the buffer. The size of the buffer that is really occupied by data.

Returns
size of occupied buffer

Definition at line 201 of file dynamic_buffer.cpp.

References fawkes::dynamic_list_t::size.

Referenced by fawkes::PluginListMessage::serialize(), fawkes::ConfigListContent::serialize(), and fawkes::BlackBoardInterfaceListContent::serialize().

◆ has_next()

bool fawkes::DynamicBuffer::has_next ( )

Check if another element is available.

Returns
true if another element can be fetched with next(), false otherwise

Definition at line 245 of file dynamic_buffer.cpp.

References fawkes::dynamic_list_t::num_elements.

Referenced by fawkes::PluginListMessage::has_next(), fawkes::ConfigListContent::has_next(), fawkes::BlackBoardInterfaceListContent::has_next(), and next().

◆ next()

void * fawkes::DynamicBuffer::next ( size_t *  size)

Get next buffer.

Parameters
sizeupon successful return contains size of the current list buffer
Returns
the next buffer.
Exceptions
OutOfBoundsExceptionthrown if no further element is available in the list.

Definition at line 258 of file dynamic_buffer.cpp.

References has_next().

Referenced by fawkes::PluginListMessage::next(), fawkes::ConfigListContent::next(), and fawkes::BlackBoardInterfaceListContent::next().

◆ num_elements()

unsigned int fawkes::DynamicBuffer::num_elements ( )

Get number of elements.

Returns
number of elements in list

Definition at line 223 of file dynamic_buffer.cpp.

References fawkes::dynamic_list_t::num_elements.

◆ real_buffer_size()

size_t fawkes::DynamicBuffer::real_buffer_size ( )

Get real buffer size.

Gets the real size of the buffer including yet unused parts. Meant to be used for debugging or informational usage.

Returns
real buffer size

Definition at line 213 of file dynamic_buffer.cpp.

◆ reset_iterator()

void fawkes::DynamicBuffer::reset_iterator ( )

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