Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LinkedDataBufferPtr Class Reference

A class that keeps ownership of a chain of linked DataBuffer references. More...

#include <DataBuffer.hxx>

Public Member Functions

 LinkedDataBufferPtr (LinkedDataBufferPtr &&o)
 Move constructor. Takes the ownership that o has. Leaves o as empty.
 
void operator= (LinkedDataBufferPtr &&o)
 Move assignment operator.
 
 LinkedDataBufferPtr (const LinkedDataBufferPtr &)=delete
 We do not permit default copy operation.
 
void operator= (const LinkedDataBufferPtr &)=delete
 
void reset (const LinkedDataBufferPtr &o, ssize_t size=-1)
 Takes a reference of o, taking a prefix of len size (or all the data).
 
void reset (DataBuffer *buf)
 Clears the current contents and replaces it with the empty buf.
 
void reset (DataBuffer *buf, unsigned skip, unsigned size)
 Set to a single data buffer.
 
void append_empty_buffer (DataBuffer *buf)
 Adds an empty buffer to the end of this buffer chain.
 
void reset ()
 Deallocates the current content (by releasing the references).
 
uint8_t * data_write_pointer ()
 
void data_write_advance (size_t len)
 Advances the tail pointer after a write occurred into the tail.
 
const uint8_t * data_read_pointer (size_t *len)
 Retrieves a pointer where data can be read out of the buffer.
 
void data_read_advance (size_t len)
 Advances the head pointer.
 
DataBufferhead () const
 
DataBuffertail () const
 
unsigned skip () const
 
unsigned size () const
 
size_t free () const
 
LinkedDataBufferPtr transfer_head (size_t len)
 Transfers the ownership of the prefix of this buffer.
 
void append_to (std::string *recvd) const
 Appends all content in this buffer to an std::string.
 
bool try_append_from (const LinkedDataBufferPtr &o, bool add_link=false)
 Attempt to combine *this with o into a single LinkedDataBufferPtr this.
 

Private Member Functions

void clear ()
 Internal helper function of constructors and reset functions.
 

Private Attributes

DataBufferhead_ {nullptr}
 First buffer in the chain. This is the root of the ownership.
 
DataBuffertail_ {nullptr}
 Last buffer in the chain. This is where we can extend the owned bytes.
 
size_t size_ {0}
 How many bytes we have filled in (counting starts at head.data() + skip_).
 
uint16_t skip_ {0}
 How many bytes to skip in the head buffer.
 
int16_t free_ {0}
 If >= 0: How many free bytes are there in the tail buffer.
 

Detailed Description

A class that keeps ownership of a chain of linked DataBuffer references.

Definition at line 205 of file DataBuffer.hxx.

Constructor & Destructor Documentation

◆ LinkedDataBufferPtr() [1/3]

LinkedDataBufferPtr::LinkedDataBufferPtr ( )
inline

Definition at line 211 of file DataBuffer.hxx.

◆ ~LinkedDataBufferPtr()

LinkedDataBufferPtr::~LinkedDataBufferPtr ( )
inline

Definition at line 215 of file DataBuffer.hxx.

◆ LinkedDataBufferPtr() [2/3]

LinkedDataBufferPtr::LinkedDataBufferPtr ( LinkedDataBufferPtr &&  o)
inline

Move constructor. Takes the ownership that o has. Leaves o as empty.

Definition at line 221 of file DataBuffer.hxx.

◆ LinkedDataBufferPtr() [3/3]

LinkedDataBufferPtr::LinkedDataBufferPtr ( const LinkedDataBufferPtr )
delete

We do not permit default copy operation.

Use the reset() function for that.

Member Function Documentation

◆ append_empty_buffer()

void LinkedDataBufferPtr::append_empty_buffer ( DataBuffer buf)
inline

Adds an empty buffer to the end of this buffer chain.

Parameters
bufis a new, empty DataBuffer. Ownership will be taken. The size() value of it has to be denoting the amount of available bytes.

Definition at line 311 of file DataBuffer.hxx.

◆ append_to()

void LinkedDataBufferPtr::append_to ( std::string *  recvd) const
inline

Appends all content in this buffer to an std::string.

Parameters
recvdstring to append data to.

Definition at line 509 of file DataBuffer.hxx.

◆ clear()

void LinkedDataBufferPtr::clear ( )
inlineprivate

Internal helper function of constructors and reset functions.

Clears the current structure (references have to have been dealt with before).

Definition at line 616 of file DataBuffer.hxx.

◆ data_read_advance()

void LinkedDataBufferPtr::data_read_advance ( size_t  len)
inline

Advances the head pointer.

Typically used after a successful read happened.

Parameters
lenhow many bytes to advance the read pointer.

Definition at line 397 of file DataBuffer.hxx.

◆ data_read_pointer()

const uint8_t * LinkedDataBufferPtr::data_read_pointer ( size_t *  len)
inline

Retrieves a pointer where data can be read out of the buffer.

Parameters
lenwill be filled in with the number of available bytes to read at this point.
Returns
the read pointer, or nullptr if there is no data in this buffer.

Definition at line 376 of file DataBuffer.hxx.

◆ data_write_advance()

void LinkedDataBufferPtr::data_write_advance ( size_t  len)
inline

Advances the tail pointer after a write occurred into the tail.

Parameters
lenhow many bytes were written into the space pointed to by data_write_pointer().

Definition at line 363 of file DataBuffer.hxx.

◆ data_write_pointer()

uint8_t * LinkedDataBufferPtr::data_write_pointer ( )
inline
Returns
the pointer where data can be appended into the tail of this buffer chain. Use free() to know how many bytes can be written here.

Definition at line 351 of file DataBuffer.hxx.

◆ free()

size_t LinkedDataBufferPtr::free ( ) const
inline
Returns
the number of bytes that can be written into the tail of this buffer chain, at data_write_pointer().

Definition at line 458 of file DataBuffer.hxx.

◆ head()

DataBuffer * LinkedDataBufferPtr::head ( ) const
inline
Returns
buffer that is at head.

Definition at line 433 of file DataBuffer.hxx.

◆ operator=()

void LinkedDataBufferPtr::operator= ( LinkedDataBufferPtr &&  o)
inline

Move assignment operator.

Takes the ownership that o has. Leaves o as empty.

Definition at line 233 of file DataBuffer.hxx.

◆ reset() [1/4]

void LinkedDataBufferPtr::reset ( )
inline

Deallocates the current content (by releasing the references).

Definition at line 333 of file DataBuffer.hxx.

◆ reset() [2/4]

void LinkedDataBufferPtr::reset ( const LinkedDataBufferPtr o,
ssize_t  size = -1 
)
inline

Takes a reference of o, taking a prefix of len size (or all the data).

The current buffer becomes non-extensible.

Parameters
oan owned LinkedDataBufferPtr
sizeis non-negative, this is how many bytes from the beginning of o will be copied. If default (negative), takes all bytes that are filled.

Definition at line 255 of file DataBuffer.hxx.

◆ reset() [3/4]

void LinkedDataBufferPtr::reset ( DataBuffer buf)
inline

Clears the current contents and replaces it with the empty buf.

Parameters
bufis a new, empty DataBuffer. Ownership will be taken. The size() value of it has to be denoting the amount of available bytes.

Definition at line 283 of file DataBuffer.hxx.

◆ reset() [4/4]

void LinkedDataBufferPtr::reset ( DataBuffer buf,
unsigned  skip,
unsigned  size 
)
inline

Set to a single data buffer.

Parameters
bufis a filled-in data buffer. Takes ownership. Must be a single (non-chained) buffer.
skiphow many bytes to skip at the beginning
sizehow many bytes to take after skip bytes.

Definition at line 298 of file DataBuffer.hxx.

◆ size()

unsigned LinkedDataBufferPtr::size ( ) const
inline
Returns
how many bytes are filled in the current buffer.

Definition at line 451 of file DataBuffer.hxx.

◆ skip()

unsigned LinkedDataBufferPtr::skip ( ) const
inline
Returns
how many bytes to skip from the head buffer.

Definition at line 445 of file DataBuffer.hxx.

◆ tail()

DataBuffer * LinkedDataBufferPtr::tail ( ) const
inline
Returns
buffer that is at the tail.

Definition at line 439 of file DataBuffer.hxx.

◆ transfer_head()

LinkedDataBufferPtr LinkedDataBufferPtr::transfer_head ( size_t  len)
inline

Transfers the ownership of the prefix of this buffer.

The tail will remain in the current buffer chain as an extra reference. Any free space in the tail will also remain in the current bufferptr.

Parameters
lenhow many bytes at the beginning (starting at skip_) to transfer. Must reach into the tail buffer, meaning that at least one byte from the tail buffer must be transferred.
Returns
a new (moveable) LinkedDataBufferPtr that will get the ownership of the head. It will be non-extendible.

Definition at line 475 of file DataBuffer.hxx.

◆ try_append_from()

bool LinkedDataBufferPtr::try_append_from ( const LinkedDataBufferPtr o,
bool  add_link = false 
)
inline

Attempt to combine *this with o into a single LinkedDataBufferPtr this.

This tries to do *this += o. It will succeed if o.head() == this->tail() and the bytes in these buffers are back to back.

Parameters
oa LinkedDataBuffer with data payload.
add_linkcreates a tail-to-head link if none exist yet between *this and o.head_. This is fundamentally dangerous, do it only if there is no shared ownership of this->tail_.
Returns
true if append succeeded. If false, nothing was changed.

Definition at line 538 of file DataBuffer.hxx.

Member Data Documentation

◆ free_

int16_t LinkedDataBufferPtr::free_ {0}
private

If >= 0: How many free bytes are there in the tail buffer.

If < 0: non-appendable buffer, the -offset of the first byte in the tail buffer that's after the payload. In other words, -1 * the skip() of the next linked buffer. In other words, -1 * the end pointer in the tail buffer.

Definition at line 635 of file DataBuffer.hxx.

◆ head_

DataBuffer* LinkedDataBufferPtr::head_ {nullptr}
private

First buffer in the chain. This is the root of the ownership.

Definition at line 623 of file DataBuffer.hxx.

◆ size_

size_t LinkedDataBufferPtr::size_ {0}
private

How many bytes we have filled in (counting starts at head.data() + skip_).

Definition at line 628 of file DataBuffer.hxx.

◆ skip_

uint16_t LinkedDataBufferPtr::skip_ {0}
private

How many bytes to skip in the head buffer.

Definition at line 630 of file DataBuffer.hxx.

◆ tail_

DataBuffer* LinkedDataBufferPtr::tail_ {nullptr}
private

Last buffer in the chain. This is where we can extend the owned bytes.

Definition at line 625 of file DataBuffer.hxx.


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