tlx
Loading...
Searching...
No Matches
RingBuffer< Type, Allocator > Class Template Reference

A ring (circular) buffer of static (non-growing) size. More...

#include <ring_buffer.hpp>

Public Types

using value_type
 
using allocator_type
 
using alloc_traits
 
typedef Type & reference
 
typedef const Type & const_reference
 
typedef Type * pointer
 
typedef const Type * const_pointer
 
using size_type
 
using difference_type
 

Public Member Functions

 RingBuffer (const Allocator &alloc=allocator_type()) noexcept
 
 RingBuffer (size_t max_size, const Allocator &alloc=allocator_type())
 
 RingBuffer (const RingBuffer &rb)
 copy-constructor: create new ring buffer
 
RingBufferoperator= (const RingBuffer &rb)
 copyable: create new ring buffer
 
 RingBuffer (RingBuffer &&rb) noexcept
 move-constructor: move buffer
 
RingBufferoperator= (RingBuffer &&rb) noexcept
 move-assignment operator: default
 
 ~RingBuffer ()
 
void allocate (size_t max_size)
 allocate buffer
 
void deallocate ()
 deallocate buffer
 
Modifiers
void push_back (const value_type &t)
 add element at the end
 
void push_back (value_type &&t)
 add element at the end
 
template<typename... Args>
void emplace_back (Args &&... args)
 emplace element at the end
 
void push_front (const value_type &t)
 add element at the beginning
 
void push_front (value_type &&t)
 add element at the beginning
 
template<typename... Args>
void emplace_front (Args &&... args)
 emplace element at the beginning
 
void pop_front ()
 remove element at the beginning
 
void pop_back ()
 remove element at the end
 
void clear ()
 reset buffer contents
 
void copy_to (std::vector< value_type > *out) const
 copy all element into the vector
 
void move_to (std::vector< value_type > *out)
 move all element from the RingBuffer into the vector
 
Element access
reference operator[] (size_type i) noexcept
 Returns a reference to the i-th element.
 
const_reference operator[] (size_type i) const noexcept
 Returns a reference to the i-th element.
 
reference front () noexcept
 Returns a reference to the first element.
 
const_reference front () const noexcept
 Returns a reference to the first element.
 
reference back () noexcept
 Returns a reference to the last element.
 
const_reference back () const noexcept
 Returns a reference to the last element.
 
Capacity
size_type size () const noexcept
 return the number of items in the buffer
 
size_t max_size () const noexcept
 return the maximum number of items in the buffer.
 
size_t capacity () const noexcept
 return actual capacity of the ring buffer.
 
bool empty () const noexcept
 returns true if no items are in the buffer
 
Serialization Methods for cereal
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 

Protected Attributes

size_t max_size_
 target max_size of circular buffer prescribed by the user.
 
allocator_type alloc_
 used allocator
 
size_t capacity_
 capacity of data buffer.
 
size_t mask_
 one-bits mask for calculating modulo of capacity using AND-mask.
 
Type * data_
 the circular buffer of static size.
 
size_type begin_
 iterator at current begin of ring buffer
 
size_type end_
 iterator at current begin of ring buffer
 

Detailed Description

template<typename Type, class Allocator = std::allocator<Type>>
class tlx::RingBuffer< Type, Allocator >

A ring (circular) buffer of static (non-growing) size.

Due to many modulo operations with capacity_, the capacity is rounded up to the next power of two, even for powers of two! This is because otherwise size() == end - begin == 0 after filling the ring buffer, and adding another size_ member requires more book-keeping.

Definition at line 36 of file ring_buffer.hpp.

Member Typedef Documentation

◆ alloc_traits

template<typename Type , class Allocator = std::allocator<Type>>
using alloc_traits

Definition at line 42 of file ring_buffer.hpp.

◆ allocator_type

template<typename Type , class Allocator = std::allocator<Type>>
using allocator_type

Definition at line 40 of file ring_buffer.hpp.

◆ const_pointer

template<typename Type , class Allocator = std::allocator<Type>>
const Type* const_pointer

Definition at line 47 of file ring_buffer.hpp.

◆ const_reference

template<typename Type , class Allocator = std::allocator<Type>>
const Type& const_reference

Definition at line 45 of file ring_buffer.hpp.

◆ difference_type

template<typename Type , class Allocator = std::allocator<Type>>
using difference_type

Definition at line 50 of file ring_buffer.hpp.

◆ pointer

template<typename Type , class Allocator = std::allocator<Type>>
Type* pointer

Definition at line 46 of file ring_buffer.hpp.

◆ reference

template<typename Type , class Allocator = std::allocator<Type>>
Type& reference

Definition at line 44 of file ring_buffer.hpp.

◆ size_type

template<typename Type , class Allocator = std::allocator<Type>>
using size_type

Definition at line 49 of file ring_buffer.hpp.

◆ value_type

template<typename Type , class Allocator = std::allocator<Type>>
using value_type

Definition at line 39 of file ring_buffer.hpp.

Constructor & Destructor Documentation

◆ RingBuffer() [1/4]

template<typename Type , class Allocator = std::allocator<Type>>
RingBuffer ( const Allocator & alloc = allocator_type())
inlineexplicitnoexcept

Definition at line 57 of file ring_buffer.hpp.

◆ RingBuffer() [2/4]

template<typename Type , class Allocator = std::allocator<Type>>
RingBuffer ( size_t max_size,
const Allocator & alloc = allocator_type() )
inlineexplicit

Definition at line 61 of file ring_buffer.hpp.

◆ RingBuffer() [3/4]

template<typename Type , class Allocator = std::allocator<Type>>
RingBuffer ( const RingBuffer< Type, Allocator > & rb)
inline

copy-constructor: create new ring buffer

Definition at line 69 of file ring_buffer.hpp.

◆ RingBuffer() [4/4]

template<typename Type , class Allocator = std::allocator<Type>>
RingBuffer ( RingBuffer< Type, Allocator > && rb)
inlinenoexcept

move-constructor: move buffer

Definition at line 107 of file ring_buffer.hpp.

◆ ~RingBuffer()

template<typename Type , class Allocator = std::allocator<Type>>
~RingBuffer ( )
inline

Definition at line 138 of file ring_buffer.hpp.

Member Function Documentation

◆ allocate()

template<typename Type , class Allocator = std::allocator<Type>>
void allocate ( size_t max_size)
inline

allocate buffer

Definition at line 144 of file ring_buffer.hpp.

◆ back() [1/2]

template<typename Type , class Allocator = std::allocator<Type>>
const_reference back ( ) const
inlinenoexcept

Returns a reference to the last element.

Definition at line 279 of file ring_buffer.hpp.

◆ back() [2/2]

template<typename Type , class Allocator = std::allocator<Type>>
reference back ( )
inlinenoexcept

Returns a reference to the last element.

Definition at line 274 of file ring_buffer.hpp.

◆ capacity()

template<typename Type , class Allocator = std::allocator<Type>>
size_t capacity ( ) const
inlinenoexcept

return actual capacity of the ring buffer.

Definition at line 300 of file ring_buffer.hpp.

◆ clear()

template<typename Type , class Allocator = std::allocator<Type>>
void clear ( )
inline

reset buffer contents

Definition at line 227 of file ring_buffer.hpp.

◆ copy_to()

template<typename Type , class Allocator = std::allocator<Type>>
void copy_to ( std::vector< value_type > * out) const
inline

copy all element into the vector

Definition at line 233 of file ring_buffer.hpp.

◆ deallocate()

template<typename Type , class Allocator = std::allocator<Type>>
void deallocate ( )
inline

deallocate buffer

Definition at line 153 of file ring_buffer.hpp.

◆ emplace_back()

template<typename Type , class Allocator = std::allocator<Type>>
template<typename... Args>
void emplace_back ( Args &&... args)
inline

emplace element at the end

Definition at line 181 of file ring_buffer.hpp.

◆ emplace_front()

template<typename Type , class Allocator = std::allocator<Type>>
template<typename... Args>
void emplace_front ( Args &&... args)
inline

emplace element at the beginning

Definition at line 205 of file ring_buffer.hpp.

◆ empty()

template<typename Type , class Allocator = std::allocator<Type>>
bool empty ( ) const
inlinenoexcept

returns true if no items are in the buffer

Definition at line 305 of file ring_buffer.hpp.

◆ front() [1/2]

template<typename Type , class Allocator = std::allocator<Type>>
const_reference front ( ) const
inlinenoexcept

Returns a reference to the first element.

Definition at line 268 of file ring_buffer.hpp.

◆ front() [2/2]

template<typename Type , class Allocator = std::allocator<Type>>
reference front ( )
inlinenoexcept

Returns a reference to the first element.

Definition at line 263 of file ring_buffer.hpp.

◆ load()

template<typename Type , class Allocator = std::allocator<Type>>
template<class Archive >
void load ( Archive & ar)
inline

Definition at line 322 of file ring_buffer.hpp.

◆ max_size()

template<typename Type , class Allocator = std::allocator<Type>>
size_t max_size ( ) const
inlinenoexcept

return the maximum number of items in the buffer.

Definition at line 295 of file ring_buffer.hpp.

◆ move_to()

template<typename Type , class Allocator = std::allocator<Type>>
void move_to ( std::vector< value_type > * out)
inline

move all element from the RingBuffer into the vector

Definition at line 239 of file ring_buffer.hpp.

◆ operator=() [1/2]

template<typename Type , class Allocator = std::allocator<Type>>
RingBuffer & operator= ( const RingBuffer< Type, Allocator > & rb)
inline

copyable: create new ring buffer

Definition at line 83 of file ring_buffer.hpp.

◆ operator=() [2/2]

template<typename Type , class Allocator = std::allocator<Type>>
RingBuffer & operator= ( RingBuffer< Type, Allocator > && rb)
inlinenoexcept

move-assignment operator: default

Definition at line 120 of file ring_buffer.hpp.

◆ operator[]() [1/2]

template<typename Type , class Allocator = std::allocator<Type>>
const_reference operator[] ( size_type i) const
inlinenoexcept

Returns a reference to the i-th element.

Definition at line 257 of file ring_buffer.hpp.

◆ operator[]() [2/2]

template<typename Type , class Allocator = std::allocator<Type>>
reference operator[] ( size_type i)
inlinenoexcept

Returns a reference to the i-th element.

Definition at line 252 of file ring_buffer.hpp.

◆ pop_back()

template<typename Type , class Allocator = std::allocator<Type>>
void pop_back ( )
inline

remove element at the end

Definition at line 220 of file ring_buffer.hpp.

◆ pop_front()

template<typename Type , class Allocator = std::allocator<Type>>
void pop_front ( )
inline

remove element at the beginning

Definition at line 213 of file ring_buffer.hpp.

◆ push_back() [1/2]

template<typename Type , class Allocator = std::allocator<Type>>
void push_back ( const value_type & t)
inline

add element at the end

Definition at line 165 of file ring_buffer.hpp.

◆ push_back() [2/2]

template<typename Type , class Allocator = std::allocator<Type>>
void push_back ( value_type && t)
inline

add element at the end

Definition at line 172 of file ring_buffer.hpp.

◆ push_front() [1/2]

template<typename Type , class Allocator = std::allocator<Type>>
void push_front ( const value_type & t)
inline

add element at the beginning

Definition at line 189 of file ring_buffer.hpp.

◆ push_front() [2/2]

template<typename Type , class Allocator = std::allocator<Type>>
void push_front ( value_type && t)
inline

add element at the beginning

Definition at line 196 of file ring_buffer.hpp.

◆ save()

template<typename Type , class Allocator = std::allocator<Type>>
template<class Archive >
void save ( Archive & ar) const
inline

Definition at line 315 of file ring_buffer.hpp.

◆ size()

template<typename Type , class Allocator = std::allocator<Type>>
size_type size ( ) const
inlinenoexcept

return the number of items in the buffer

Definition at line 290 of file ring_buffer.hpp.

Member Data Documentation

◆ alloc_

template<typename Type , class Allocator = std::allocator<Type>>
allocator_type alloc_
protected

used allocator

Definition at line 354 of file ring_buffer.hpp.

◆ begin_

template<typename Type , class Allocator = std::allocator<Type>>
size_type begin_
protected

iterator at current begin of ring buffer

Definition at line 367 of file ring_buffer.hpp.

◆ capacity_

template<typename Type , class Allocator = std::allocator<Type>>
size_t capacity_
protected

capacity of data buffer.

rounded up from max_size_ to next unequal power of two.

Definition at line 358 of file ring_buffer.hpp.

◆ data_

template<typename Type , class Allocator = std::allocator<Type>>
Type* data_
protected

the circular buffer of static size.

Definition at line 364 of file ring_buffer.hpp.

◆ end_

template<typename Type , class Allocator = std::allocator<Type>>
size_type end_
protected

iterator at current begin of ring buffer

Definition at line 370 of file ring_buffer.hpp.

◆ mask_

template<typename Type , class Allocator = std::allocator<Type>>
size_t mask_
protected

one-bits mask for calculating modulo of capacity using AND-mask.

Definition at line 361 of file ring_buffer.hpp.

◆ max_size_

template<typename Type , class Allocator = std::allocator<Type>>
size_t max_size_
protected

target max_size of circular buffer prescribed by the user.

Never equal to the data_.size(), which is rounded up to a power of two.

Definition at line 351 of file ring_buffer.hpp.


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