Belle II Software development
CharBuffer Class Reference

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

#include <MsgHandler.h>

Public Member Functions

 CharBuffer (size_t initial_capacity=0)
 Constructor, with the initial capacity of the buffer to allocate (in bytes).
 
void add (const void *data, size_t len)
 copy data to end of buffer, expanding buffer if needed.
 
char * data ()
 return raw pointer.
 
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.

44 {
45 auto old_size = m_size;
46 resize(m_size + len);
47 memcpy(m_data.get() + old_size, data, len);
48 }
size_t m_size
current size, <= m_capacity
Definition: MsgHandler.h:33
char * data()
return raw pointer.
Definition: MsgHandler.h:52
void resize(size_t size)
resize, similar to std::vector<char>::resize in that it will copy the existing buffer to a new,...
Definition: MsgHandler.h:60

◆ clear()

void clear ( )
inline

reset (without deallocating)

Definition at line 56 of file MsgHandler.h.

56{ m_size = 0; }

◆ 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.

52{ return m_data.get(); }

◆ resize()

void resize ( size_t  size)
inline

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

Definition at line 60 of file MsgHandler.h.

61 {
62 if (size > m_capacity) {
63 // grow by basically doubling each time, except if the size we need to
64 // grow to is even more than that
65 m_capacity = std::max(size, m_capacity * 2);
66 //c++14: auto newbuf = std::make_unique<char[]>(m_capacity);
67 std::unique_ptr<char[]> newbuf{new char[m_capacity]};
68 memcpy(newbuf.get(), m_data.get(), m_size);
69 std::swap(m_data, newbuf);
70 }
71 m_size = size;
72 }
size_t size() const
return buffer size (do not access data() beyond this)
Definition: MsgHandler.h:54
size_t m_capacity
size of allocated memory in m_data
Definition: MsgHandler.h:32

◆ size()

size_t size ( ) const
inline

return buffer size (do not access data() beyond this)

Definition at line 54 of file MsgHandler.h.

54{ return m_size; }

Member Data Documentation

◆ m_capacity

size_t m_capacity {0}
private

size of allocated memory in m_data

Definition at line 32 of file MsgHandler.h.

◆ m_data

std::unique_ptr<char[]> m_data
private

data buffer.

Definition at line 31 of file MsgHandler.h.

◆ m_size

size_t m_size {0}
private

current size, <= m_capacity

Definition at line 33 of file MsgHandler.h.


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