MIRA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Protected Attributes | List of all members
Buffer< T, Alloc > Class Template Reference

Generic buffer class that can be used as a replacement for std::vector whenever copying and reallocation of internal array buffer needs to be avoided. More...

#include <utils/Buffer.h>

Public Types

typedef T value_type
 
typedef Alloc::pointer pointer
 
typedef Alloc::const_pointer const_pointer
 
typedef Alloc::reference reference
 
typedef Alloc::const_reference const_reference
 
typedef T * iterator
 
typedef const T * const_iterator
 
typedef std::size_t size_type
 
typedef ptrdiff_t difference_type
 
typedef Alloc allocator_type
 

Public Member Functions

 Buffer ()
 Default constructor constructing an empty buffer. More...
 
 Buffer (size_type size)
 
 Buffer (T *carray, size_type size)
 Use already allocated memory in carray with size size. More...
 
 Buffer (std::vector< T > &&other)
 Constructs a buffer that TAKES OVER all data from the specified vector. More...
 
 ~Buffer ()
 Destructor. More...
 
 Buffer (const Buffer &other)
 Copy constructor. More...
 
Bufferoperator= (const Buffer &other)
 Assignment. More...
 
void copy (const Buffer &other)
 Copies a buffer into this. More...
 
 Buffer (Buffer &&other)
 Move constructor. More...
 
Bufferoperator= (Buffer &&other)
 Move constructor. More...
 
void swap (Buffer &other)
 Swaps the content of this buffer with the other buffer. More...
 
bool operator== (const Buffer &other) const
 Checks for equality with other buffer. More...
 
bool operator!= (const Buffer &other) const
 Checks for inequality with other buffer. More...
 
iterator begin ()
 Returns an iterator referring to the first element in the vector container. More...
 
const_iterator begin () const
 Returns an iterator referring to the first element in the vector container. More...
 
iterator end ()
 Returns an iterator referring to the past-the-end element in the vector container. More...
 
const_iterator end () const
 Returns an iterator referring to the past-the-end element in the vector container. More...
 
size_type max_size () const
 Returns the maximum size of the buffer. More...
 
size_type size () const
 Returns the used size of the buffer set by resize() More...
 
size_type sizeInBytes () const
 Returns the used size in bytes. More...
 
bool empty () const
 Checks if the buffer is empty (used size == 0). More...
 
size_type capacity () const
 Returns the reserved size/capacity of the buffer (Its real size) set by reserve() More...
 
allocator_type get_allocator () const
 Returns the allocator. More...
 
void reserve (size_type reserve)
 Allocates new memory if reserved size < new reserved size. More...
 
void resize (size_type size)
 Resizes the buffer. More...
 
void push_back (const T &x)
 Adds a new element at the end of the vector, after its current last element. More...
 
void push_back (T *data, size_type size)
 Adds new elements to the end of the vector, after its current last element. More...
 
void push_back (const Buffer< T > &data)
 Adds new elements to the end of the vector, after its current last element. More...
 
void pop_back ()
 Removes the last element in the vector, effectively reducing the vector size by one. More...
 
void pop_back (size_type elements)
 Removes the last elements in the vector, effectively reducing the vector size by elements. More...
 
void pop_front ()
 Removes the first element in the vector by copying all remaining data to the front invalidating all iterators and references to the buffer. More...
 
void pop_front (size_type elements)
 Removes the first elements in the vector by copying all remaining data to the front invalidating all iterators and references to the buffer. More...
 
void clear ()
 All the elements of the vector are dropped. More...
 
reference operator[] (size_type n)
 Returns a reference to the element at position n in the vector container. More...
 
const_reference operator[] (size_type n) const
 Returns a reference to the element at position n in the vector container. More...
 
reference at (size_type n)
 The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception. More...
 
const_reference at (size_type n) const
 The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception. More...
 
reference front ()
 Returns a reference to the first element in the vector container. More...
 
const_reference front () const
 Returns a reference to the first element in the vector container. More...
 
reference back ()
 Returns a reference to the last element in the vector container. More...
 
const_reference back () const
 Returns a reference to the last element in the vector container. More...
 
pointer data ()
 Returns a pointer to the underlying data. More...
 
const_pointer data () const
 Returns a const pointer to the underlying data. More...
 

Protected Attributes

T * mBuffer
 Pointer to the data buffer. More...
 
size_type mSize
 The used elements of the buffer. More...
 
size_type mReserved
 The real size of the buffer. More...
 
bool mBufferCreated
 Was the buffer created (is it owned) by us. More...
 
Alloc mAllocator
 The allocator used to allocate new memory. More...
 
std::vector< T > * mTakenVector
 Used to take over the data from a vector without copying. More...
 

Detailed Description

template<typename T, typename Alloc = std::allocator<T>>
class mira::Buffer< T, Alloc >

Generic buffer class that can be used as a replacement for std::vector whenever copying and reallocation of internal array buffer needs to be avoided.

The Buffer has the following advantages over the std::vector:

Member Typedef Documentation

typedef T value_type
typedef Alloc::pointer pointer
typedef Alloc::const_pointer const_pointer
typedef Alloc::reference reference
typedef Alloc::const_reference const_reference
typedef T* iterator
typedef const T* const_iterator
typedef std::size_t size_type
typedef ptrdiff_t difference_type
typedef Alloc allocator_type

Constructor & Destructor Documentation

Buffer ( )
inline

Default constructor constructing an empty buffer.

Buffer ( size_type  size)
inline
Buffer ( T *  carray,
size_type  size 
)
inline

Use already allocated memory in carray with size size.

Buffer does not take ownership of carray. No data is copied unless calling reserve or resize with size > size of carray.

Buffer ( std::vector< T > &&  other)
inline

Constructs a buffer that TAKES OVER all data from the specified vector.

The content of the vector will be destroyed and will be completely moved to the created buffer without any necessary copying. Please note, that the vector is passed as rvalue reference. In order to move the content of a vector into the newly created buffer, you can construct the buffer as follows:

std::vector<int> myvec;
... fill myvec ...
// construct a new Buffer and take over all data of myvec, by "moving"
// its content into the buffer:
Buffer<int> mybuf(std::move(myvec));
// myvec will now be empty, and its whole content is moved into the
// buffer mybuf.
~Buffer ( )
inline

Destructor.

Buffer ( const Buffer< T, Alloc > &  other)
inline

Copy constructor.

Buffer ( Buffer< T, Alloc > &&  other)
inline

Move constructor.

Member Function Documentation

Buffer& operator= ( const Buffer< T, Alloc > &  other)
inline

Assignment.

void copy ( const Buffer< T, Alloc > &  other)
inline

Copies a buffer into this.

Buffer& operator= ( Buffer< T, Alloc > &&  other)
inline

Move constructor.

void swap ( Buffer< T, Alloc > &  other)
inline

Swaps the content of this buffer with the other buffer.

bool operator== ( const Buffer< T, Alloc > &  other) const
inline

Checks for equality with other buffer.

bool operator!= ( const Buffer< T, Alloc > &  other) const
inline

Checks for inequality with other buffer.

iterator begin ( )
inline

Returns an iterator referring to the first element in the vector container.

const_iterator begin ( ) const
inline

Returns an iterator referring to the first element in the vector container.

iterator end ( )
inline

Returns an iterator referring to the past-the-end element in the vector container.

const_iterator end ( ) const
inline

Returns an iterator referring to the past-the-end element in the vector container.

size_type max_size ( ) const
inline

Returns the maximum size of the buffer.

size_type size ( ) const
inline

Returns the used size of the buffer set by resize()

size_type sizeInBytes ( ) const
inline

Returns the used size in bytes.

bool empty ( ) const
inline

Checks if the buffer is empty (used size == 0).

size_type capacity ( ) const
inline

Returns the reserved size/capacity of the buffer (Its real size) set by reserve()

allocator_type get_allocator ( ) const
inline

Returns the allocator.

void reserve ( size_type  reserve)
inline

Allocates new memory if reserved size < new reserved size.

Otherwise allocated memory stays the same. Reserved memory is the smallest power of two >= new reserved size.

void resize ( size_type  size)
inline

Resizes the buffer.

Sets only size to given size if buffer reserved size is >= new size. Allocates more memory and copies existing data if reserved size < new size If size is increased, new fields are uninitialized and need to be filled e.g. by copying elements into them.

void push_back ( const T &  x)
inline

Adds a new element at the end of the vector, after its current last element.

The content of this new element is initialized to a copy of x.

void push_back ( T *  data,
size_type  size 
)
inline

Adds new elements to the end of the vector, after its current last element.

The content of these new elements is initialized to a copy of data.

void push_back ( const Buffer< T > &  data)
inline

Adds new elements to the end of the vector, after its current last element.

The content of these new elements is initialized to a copy of data.

void pop_back ( )
inline

Removes the last element in the vector, effectively reducing the vector size by one.

Does not free any memory but an iterator to the removed element will become invalid.

void pop_back ( size_type  elements)
inline

Removes the last elements in the vector, effectively reducing the vector size by elements.

Does not free any memory but iterators to removed elements will become invalid.

void pop_front ( )
inline

Removes the first element in the vector by copying all remaining data to the front invalidating all iterators and references to the buffer.

void pop_front ( size_type  elements)
inline

Removes the first elements in the vector by copying all remaining data to the front invalidating all iterators and references to the buffer.

void clear ( )
inline

All the elements of the vector are dropped.

reference operator[] ( size_type  n)
inline

Returns a reference to the element at position n in the vector container.

const_reference operator[] ( size_type  n) const
inline

Returns a reference to the element at position n in the vector container.

reference at ( size_type  n)
inline

The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception.

const_reference at ( size_type  n) const
inline

The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception.

reference front ( )
inline

Returns a reference to the first element in the vector container.

const_reference front ( ) const
inline

Returns a reference to the first element in the vector container.

reference back ( )
inline

Returns a reference to the last element in the vector container.

const_reference back ( ) const
inline

Returns a reference to the last element in the vector container.

pointer data ( )
inline

Returns a pointer to the underlying data.

const_pointer data ( ) const
inline

Returns a const pointer to the underlying data.

Member Data Documentation

T* mBuffer
protected

Pointer to the data buffer.

size_type mSize
protected

The used elements of the buffer.

size_type mReserved
protected

The real size of the buffer.

bool mBufferCreated
protected

Was the buffer created (is it owned) by us.

Alloc mAllocator
protected

The allocator used to allocate new memory.

std::vector<T>* mTakenVector
protected

Used to take over the data from a vector without copying.


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