Template Class RingBuffer

Class Documentation

template<typename T>
class RingBuffer

Ring (or circular) buffers are sequence containers with static sizes.

They allow for the individual elements to be accessed directly through random access iterators. They allow for insertion/deletion at either end of the buffer. Inserting to a full buffer will pop an element off the other end. Elements are not guaranteed to be stored in contiguous memory (due to the wrap around nature of the underlying buffer).

Public Types

using value_type = T

Type of elements stored in the ring buffer.

using iterator = RingBufferIterator<T>

Type of iterator to ring buffer element.

using const_iterator = RingBufferIterator<const T>

Type of const_iterator to ring buffer element.

using reverse_iterator = std::reverse_iterator<iterator>

Type of reverse_iterator to ring buffer element.

using const_reverse_iterator = std::reverse_iterator<const_iterator>

Type of const_reverse_iterator to ring buffer element.

Public Functions

inline explicit RingBuffer(std::size_t capacity)

Default constructor.

Parameters

capacity – number of elements the ring buffer can hold

inline ~RingBuffer()

Default destructor.

inline RingBuffer(const RingBuffer &other)

Copy constructor.

Parameters

other – buffer to copy

inline RingBuffer &operator=(const RingBuffer &other)

Copy assignment operator.

Parameters

other – buffer to copy

Returns

Buffer (by reference)

inline RingBuffer(RingBuffer &&other)

Move constructor.

Parameters

other – buffer to move from

inline RingBuffer &operator=(RingBuffer &&other)

Move assignment operator.

Parameters

other – buffer to move from

Returns

Buffer (by reference)

inline std::size_t size() const
Returns

The number of elements in the container.

inline bool empty() const
Returns

true if the container size is 0, false otherwise.

inline bool full() const
Returns

true if the container is at capacity.

inline T &front()
Returns

Reference to the first element in the container.

inline const T &front() const
Returns

Constant reference to the first element in the container.

inline T &back()
Returns

Reference to the last element in the container.

inline const T &back() const
Returns

Constant reference to the last element in the container.

inline void pop_front()

Removes the first element in the container, effectively reducing its size by one.

This destroys the removed element.

inline void pop_back()

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

This destroys the removed element.

inline void push_front(const T &value)

Inserts a new element at the beginning of the container, right before its current first element.

The content of value is copied to the inserted element. This effectively increases the container size by one unless the container is full in which case the element at the back of the container is removed and destroyed.

Parameters

value – to push

inline void push_front(T &&value)

Inserts a new element at the beginning of the container, right before its current first element.

The content of value is moved to the inserted element. This effectively increases the container size by one unless the container is full in which case the element at the back of the container is removed and destroyed.

Parameters

value – to push

inline void push_back(const T &value)

Inserts a new element at the end of the container, right after its current last element.

The content of value is copied to the inserted element. This effectively increases the container size by one unless the container is full in which case the element at the beginning of the container is removed and destroyed.

Parameters

value – to push

inline void push_back(T &&value)

Inserts a new element at the end of the container, right after its current last element.

The content of value is moved to the inserted element. This effectively increases the container size by one unless the container is full in which case the element at the beginning of the container is removed and destroyed.

Parameters

value – to push

inline iterator erase(const_iterator first, const_iterator last)

Removes from the container the range of elements in [first, last).

The range includes all the elements between first and last, including the element pointed by first but not the one pointed by last. This effectively reduces the container size by the number of elements removed, which are destroyed. Return Value: An iterator pointing to the location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the container. Exception: Only supports erasing elements from the beginning or end of the container. If the specified range is in the middle of the container an exception will be thrown.

Parameters
  • first – the first element to remove

  • last – one past the last element to remove

Returns

Element after the last element removed

inline iterator begin()

If the container is empty, the returned iterator value shall not be dereferenced.

Returns

An iterator pointing to the first element in the container.

inline const_iterator begin() const

If the container is empty, the returned iterator value shall not be dereferenced.

Returns

A constant iterator pointing to the first element in the container.

inline const_iterator cbegin() const

If the container is empty, the returned iterator value shall not be dereferenced.

Returns

A constant iterator pointing to the first element in the container.

inline iterator end()

The past-the-end element is the theoretical element that would follow the last element in the container. It does not point to any element, and thus shall not be dereferenced.

Returns

An iterator pointing to the past-the-end element in the container.

inline const_iterator end() const

The past-the-end element is the theoretical element that would follow the last element in the container. It does not point to any element, and thus shall not be dereferenced.

Returns

A constant iterator pointing to the past-the-end element in the container.

inline const_iterator cend() const

The past-the-end element is the theoretical element that would follow the last element in the container. It does not point to any element, and thus shall not be dereferenced.

Returns

A constant iterator pointing to the past-the-end element in the container.

inline reverse_iterator rbegin()

Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. rbegin points to the element right before the one that would be pointed to by member end. If the container is empty, the returned iterator value shall not be dereferenced.

Returns

A reverse iterator pointing to the last element in the container.

inline const_reverse_iterator rbegin() const

Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. rbegin points to the element right before the one that would be pointed to by member end. If the container is empty, the returned iterator value shall not be dereferenced.

Returns

A constant reverse iterator pointing to the last element in the container.

inline const_reverse_iterator crbegin() const

Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. rbegin points to the element right before the one that would be pointed to by member end. If the container is empty, the returned iterator value shall not be dereferenced.

Returns

A constant reverse iterator pointing to the last element in the container.

inline reverse_iterator rend()

The before-the-beginning element is the theoretical element that would preceed the first element in the container. It does not point to any element, and thus shall not be dereferenced.

Returns

A reverse iterator pointing to the before-the-beginning element in the container.

inline const_reverse_iterator rend() const

The before-the-beginning element is the theoretical element that would preceed the first element in the container. It does not point to any element, and thus shall not be dereferenced.

Returns

A constant reverse iterator pointing to the before-the-beginning element in the container.

inline const_reverse_iterator crend() const

The before-the-beginning element is the theoretical element that would preceed the first element in the container. It does not point to any element, and thus shall not be dereferenced.

Returns

A constant reverse iterator pointing to the before-the-beginning element in the container.