MIRA
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) noexcept
 Move constructor. More...
 
Bufferoperator= (Buffer &&other) noexcept
 Move assignment. 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

◆ value_type

typedef T value_type

◆ pointer

typedef Alloc::pointer pointer

◆ const_pointer

typedef Alloc::const_pointer const_pointer

◆ reference

typedef Alloc::reference reference

◆ const_reference

typedef Alloc::const_reference const_reference

◆ iterator

typedef T* iterator

◆ const_iterator

typedef const T* const_iterator

◆ size_type

typedef std::size_t size_type

◆ difference_type

typedef ptrdiff_t difference_type

◆ allocator_type

typedef Alloc allocator_type

Constructor & Destructor Documentation

◆ Buffer() [1/6]

Buffer ( )
inline

Default constructor constructing an empty buffer.

◆ Buffer() [2/6]

Buffer ( size_type  size)
inline

◆ Buffer() [3/6]

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() [4/6]

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()

~Buffer ( )
inline

Destructor.

◆ Buffer() [5/6]

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

Copy constructor.

◆ Buffer() [6/6]

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

Move constructor.

Member Function Documentation

◆ operator=() [1/2]

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

Assignment.

◆ copy()

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

Copies a buffer into this.

◆ operator=() [2/2]

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

Move assignment.

◆ swap()

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

Swaps the content of this buffer with the other buffer.

◆ operator==()

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

Checks for equality with other buffer.

◆ operator!=()

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

Checks for inequality with other buffer.

◆ begin() [1/2]

iterator begin ( )
inline

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

◆ begin() [2/2]

const_iterator begin ( ) const
inline

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

◆ end() [1/2]

iterator end ( )
inline

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

◆ end() [2/2]

const_iterator end ( ) const
inline

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

◆ max_size()

size_type max_size ( ) const
inline

Returns the maximum size of the buffer.

◆ size()

size_type size ( ) const
inline

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

◆ sizeInBytes()

size_type sizeInBytes ( ) const
inline

Returns the used size in bytes.

◆ empty()

bool empty ( ) const
inline

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

◆ capacity()

size_type capacity ( ) const
inline

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

◆ get_allocator()

allocator_type get_allocator ( ) const
inline

Returns the allocator.

◆ reserve()

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.

◆ resize()

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.

◆ push_back() [1/3]

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.

◆ push_back() [2/3]

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.

◆ push_back() [3/3]

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.

◆ pop_back() [1/2]

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.

◆ pop_back() [2/2]

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.

◆ pop_front() [1/2]

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.

◆ pop_front() [2/2]

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.

◆ clear()

void clear ( )
inline

All the elements of the vector are dropped.

◆ operator[]() [1/2]

reference operator[] ( size_type  n)
inline

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

◆ operator[]() [2/2]

const_reference operator[] ( size_type  n) const
inline

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

◆ at() [1/2]

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.

◆ at() [2/2]

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.

◆ front() [1/2]

reference front ( )
inline

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

◆ front() [2/2]

const_reference front ( ) const
inline

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

◆ back() [1/2]

reference back ( )
inline

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

◆ back() [2/2]

const_reference back ( ) const
inline

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

◆ data() [1/2]

pointer data ( )
inline

Returns a pointer to the underlying data.

◆ data() [2/2]

const_pointer data ( ) const
inline

Returns a const pointer to the underlying data.

Member Data Documentation

◆ mBuffer

T* mBuffer
protected

Pointer to the data buffer.

◆ mSize

size_type mSize
protected

The used elements of the buffer.

◆ mReserved

size_type mReserved
protected

The real size of the buffer.

◆ mBufferCreated

bool mBufferCreated
protected

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

◆ mAllocator

Alloc mAllocator
protected

The allocator used to allocate new memory.

◆ mTakenVector

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: