Belle II Software  release-08-01-10
CharBuffer Class Reference

dynamic character buffer that knows its size. More...

#include <MsgHandler.h>

Collaboration diagram for CharBuffer:

Public Member Functions

 CharBuffer (size_t initial_capacity=0)
 Constructor, with the initial capacity of the buffer to allocate (in bytes). More...
 
void add (const void *data, size_t len)
 copy data to end of buffer, expanding buffer if needed. More...
 
char * data ()
 return raw pointer. More...
 
size_t size () const
 return buffer size (do not access data() beyond this)
 
void clear ()
 reset (without deallocating)
 
void resize (size_t size)
 resize, similar to std::vector<char>::resize in that it will copy the existing buffer to a new, larger buffer if needed but it will not initialize any elements beyond the existing elements
 

Private Attributes

std::unique_ptr< char[]> m_data
 data buffer.
 
size_t m_capacity {0}
 size of allocated memory in m_data
 
size_t m_size {0}
 current size, <= m_capacity
 

Detailed Description

dynamic character buffer that knows its size.

This is similar to std::vector<char> but compared with std::vector<char> this saves unnecessary calls to memset() when adding a block. (std::vector initialises memory on allocation, this class does not).

Definition at line 30 of file MsgHandler.h.

Constructor & Destructor Documentation

◆ CharBuffer()

CharBuffer ( size_t  initial_capacity = 0)
inlineexplicit

Constructor, with the initial capacity of the buffer to allocate (in bytes).

size() will remain zero until buffer is filled.

Definition at line 38 of file MsgHandler.h.

38  : m_data{initial_capacity > 0 ? new char[initial_capacity] : nullptr}
39  {}
std::unique_ptr< char[]> m_data
data buffer.
Definition: MsgHandler.h:31

Member Function Documentation

◆ add()

void add ( const void *  data,
size_t  len 
)
inline

copy data to end of buffer, expanding buffer if needed.

This can invalidate previous pointers obtained by data() if the buffer needs to be expanded.

Definition at line 43 of file MsgHandler.h.

◆ data()

char* data ( )
inline

return raw pointer.

Should not be read beyond size() bytes and becomes invalid on add() or resize()

Definition at line 52 of file MsgHandler.h.


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