Lunchbox  1.13.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::Buffer< T > Class Template Reference

A simple memory buffer with some helper functions. More...

#include <buffer.h>

+ Collaboration diagram for lunchbox::Buffer< T >:

Public Member Functions

 Buffer ()
 Construct a new, empty buffer. More...
 
 Buffer (const uint64_t size)
 Construct a new buffer of the given size. More...
 
 Buffer (Buffer &from)
 "Move" constructor, transfers ownership to new Buffer. More...
 
 ~Buffer ()
 Destruct the buffer. More...
 
void clear ()
 Flush the buffer, deleting all data. More...
 
T * pack ()
 Tighten the allocated memory to the size of the buffer. More...
 
Bufferoperator= (const Buffer &from)
 Assignment operator, copies data from Buffer. More...
 
T & operator[] (const uint64_t position)
 Direct access to the element at the given index. More...
 
const T & operator[] (const uint64_t position) const
 Direct const access to an element. More...
 
T * resize (const uint64_t newSize)
 Ensure that the buffer contains at least newSize elements. More...
 
void grow (const uint64_t newSize)
 Ensure that the buffer contains at least newSize elements. More...
 
T * reserve (const uint64_t newSize)
 Ensure that the buffer contains at least newSize elements. More...
 
T * reset (const uint64_t newSize)
 Set the buffer size and malloc enough memory. More...
 
void setZero ()
 Set the buffer content to 0. More...
 
void append (const T *data, const uint64_t size)
 Append elements to the buffer, increasing the size. More...
 
void append (const T &element)
 Append one element to the buffer. More...
 
void replace (const void *data, const uint64_t size)
 Replace the existing data with new data. More...
 
void replace (const Buffer &from)
 Replace the existing data. More...
 
void swap (Buffer &buffer)
 Swap the buffer contents with another Buffer. More...
 
T * getData ()
 
const T * getData () const
 
bool setSize (const uint64_t size)
 Set the size of the buffer without changing its allocation. More...
 
uint64_t getSize () const
 
uint64_t getNumBytes () const
 
bool isEmpty () const
 
uint64_t getMaxSize () const
 

Detailed Description

template<class T>
class lunchbox::Buffer< T >

A simple memory buffer with some helper functions.

std::vector does not implement optimizations for growing bitwise-movable vectors, i.e., it copy-constructs each element on growth.

This buffer just memcpy's elements, i.e., it should only be used for PODs since the copy constructor or assignment operator is not called on the copied elements. Primarily used for binary data, e.g., in eq::Image. The implementation works like a pool, that is, data is only released when the buffer is deleted or clear() is called.

Definition at line 43 of file buffer.h.

Constructor & Destructor Documentation

template<class T>
lunchbox::Buffer< T >::Buffer ( )
inline

Construct a new, empty buffer.

Version
1.0

Definition at line 47 of file buffer.h.

template<class T>
lunchbox::Buffer< T >::Buffer ( const uint64_t  size)
inlineexplicit

Construct a new buffer of the given size.

Version
1.0

Definition at line 50 of file buffer.h.

References lunchbox::Buffer< T >::reset().

+ Here is the call graph for this function:

template<class T >
lunchbox::Buffer< T >::Buffer ( Buffer< T > &  from)
explicit

"Move" constructor, transfers ownership to new Buffer.

Version
1.0

Definition at line 21 of file buffer.ipp.

template<class T>
lunchbox::Buffer< T >::~Buffer ( )
inline

Destruct the buffer.

Version
1.0

Definition at line 57 of file buffer.h.

References lunchbox::Buffer< T >::clear().

+ Here is the call graph for this function:

Member Function Documentation

template<class T >
void lunchbox::Buffer< T >::append ( const T *  data,
const uint64_t  size 
)

Append elements to the buffer, increasing the size.

Version
1.0

Definition at line 82 of file buffer.ipp.

template<class T >
void lunchbox::Buffer< T >::append ( const T &  element)

Append one element to the buffer.

Version
1.0

Definition at line 92 of file buffer.ipp.

template<class T>
void lunchbox::Buffer< T >::clear ( )
inline

Flush the buffer, deleting all data.

Version
1.0

Definition at line 60 of file buffer.h.

Referenced by lunchbox::Buffer< T >::~Buffer().

+ Here is the caller graph for this function:

template<class T>
T* lunchbox::Buffer< T >::getData ( )
inline
Returns
a pointer to the data.
Version
1.0

Definition at line 134 of file buffer.h.

template<class T>
const T* lunchbox::Buffer< T >::getData ( ) const
inline
Returns
a const pointer to the data.
Version
1.0

Definition at line 137 of file buffer.h.

template<class T>
uint64_t lunchbox::Buffer< T >::getMaxSize ( ) const
inline
Returns
the maximum size of the buffer.
Version
1.0

Definition at line 159 of file buffer.h.

template<class T>
uint64_t lunchbox::Buffer< T >::getNumBytes ( ) const
inline
Returns
the current storage size.
Version
1.5.1

Definition at line 153 of file buffer.h.

template<class T>
uint64_t lunchbox::Buffer< T >::getSize ( ) const
inline
Returns
the current number of elements.
Version
1.0

Definition at line 150 of file buffer.h.

template<class T >
void lunchbox::Buffer< T >::grow ( const uint64_t  newSize)

Ensure that the buffer contains at least newSize elements.

Existing data is retained. The size is increased, if necessary.

Version
1.0

Definition at line 58 of file buffer.ipp.

template<class T>
bool lunchbox::Buffer< T >::isEmpty ( ) const
inline
Returns
true if the buffer is empty, false if not.
Version
1.0

Definition at line 156 of file buffer.h.

template<class T >
Buffer< T > & lunchbox::Buffer< T >::operator= ( const Buffer< T > &  from)

Assignment operator, copies data from Buffer.

Version
1.0

Definition at line 38 of file buffer.ipp.

template<class T>
T& lunchbox::Buffer< T >::operator[] ( const uint64_t  position)
inline

Direct access to the element at the given index.

Version
1.0

Definition at line 73 of file buffer.h.

template<class T>
const T& lunchbox::Buffer< T >::operator[] ( const uint64_t  position) const
inline

Direct const access to an element.

Version
1.0

Definition at line 77 of file buffer.h.

template<class T >
T * lunchbox::Buffer< T >::pack ( )

Tighten the allocated memory to the size of the buffer.

Returns
the new pointer to the first element.
Version
1.0

Definition at line 27 of file buffer.ipp.

template<class T >
void lunchbox::Buffer< T >::replace ( const void *  data,
const uint64_t  size 
)

Replace the existing data with new data.

Version
1.0

Definition at line 99 of file buffer.ipp.

template<class T>
void lunchbox::Buffer< T >::replace ( const Buffer< T > &  from)
inline

Replace the existing data.

Version
1.5.1

Definition at line 128 of file buffer.h.

References lunchbox::Buffer< T >::replace().

Referenced by lunchbox::Buffer< T >::replace().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
T * lunchbox::Buffer< T >::reserve ( const uint64_t  newSize)

Ensure that the buffer contains at least newSize elements.

Existing data is preserved.

Returns
the new pointer to the first element.
Version
1.0

Definition at line 64 of file buffer.ipp.

template<class T >
T * lunchbox::Buffer< T >::reset ( const uint64_t  newSize)

Set the buffer size and malloc enough memory.

Existing data may be deleted.

Returns
the new pointer to the first element.
Version
1.0

Definition at line 74 of file buffer.ipp.

Referenced by lunchbox::Buffer< T >::Buffer().

+ Here is the caller graph for this function:

template<class T >
T * lunchbox::Buffer< T >::resize ( const uint64_t  newSize)

Ensure that the buffer contains at least newSize elements.

Existing data is retained. The size is set.

Returns
the new pointer to the first element.
Version
1.0

Definition at line 44 of file buffer.ipp.

template<class T >
bool lunchbox::Buffer< T >::setSize ( const uint64_t  size)

Set the size of the buffer without changing its allocation.

This method only modifies the size parameter. If the current allocation of the buffer is too small, it asserts, returns false and does not change the size.

Version
1.0

Definition at line 124 of file buffer.ipp.

template<class T>
void lunchbox::Buffer< T >::setZero ( )
inline

Set the buffer content to 0.

Version
1.9.1

Definition at line 116 of file buffer.h.

template<class T >
void lunchbox::Buffer< T >::swap ( Buffer< T > &  buffer)

Swap the buffer contents with another Buffer.

Version
1.0

Definition at line 109 of file buffer.ipp.


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