MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::FixedRingBuffer< T, Capacity > Struct Template Reference

Represents a ring buffer with static memory allocation Can be used as a FIFO or LIFO queue Allows memory reuse by wrapping pointers Cannot be dynamically resized. More...

#include <ring_buffer.hpp>

Inheritance diagram for mtcore::FixedRingBuffer< T, Capacity >:

Public Types

using Elem = T
 

Public Member Functions

decltype(auto) ptr_iter () noexcept
 Gets a pointer iterator to values.
 
decltype(auto) ptr_iter () const noexcept
 Gets a constant pointer iterator to values.
 
decltype(auto) iter () noexcept
 Gets an iterator that returns copies of values.
 
void init ()
 Initializes a new fixed ring buffer.
 
void init (std::initializer_list< T > list)
 Initializes a new fixed ring buffer with specific elements.
 
const T & operator[] (size_t i) const noexcept
 Gets an element at a specific index.
 
T & operator[] (size_t i) noexcept
 Gets an element at a specific index.
 
size_t size () const noexcept
 Gets current size of the buffer.
 
const T & at (size_t i) const noexcept
 Gets an element at a specific index.
 
T & at (size_t i) noexcept
 Gets an element at a specific index.
 
Result< void, CollectionAddNoAllocationErrorpush_back (const T &elem)
 Adds an element to the back of the buffer.
 
Result< void, CollectionAddNoAllocationErrorpush_front (const T &elem)
 Adds an element to the front of the buffer.
 
Optional< T > pop_front ()
 Removes an element from the front.
 
Optional< T > pop_back ()
 Removes an element from the back.
 

Detailed Description

template<typename T, size_t Capacity = 10>
struct mtcore::FixedRingBuffer< T, Capacity >

Represents a ring buffer with static memory allocation Can be used as a FIFO or LIFO queue Allows memory reuse by wrapping pointers Cannot be dynamically resized.

Accessing elements is \(O(1)\) Adding elements is \(O(1)\) Removing elements is \(O(1)\)

Template Parameters
TType of element stored
CapacityMax capacity

Definition at line 356 of file ring_buffer.hpp.

Member Typedef Documentation

◆ Elem

template<typename T, size_t Capacity = 10>
using mtcore::FixedRingBuffer< T, Capacity >::Elem = T

Definition at line 364 of file ring_buffer.hpp.

Member Function Documentation

◆ at() [1/2]

template<typename T, size_t Capacity = 10>
const T & mtcore::FixedRingBuffer< T, Capacity >::at ( size_t i) const
inlinenodiscardnoexcept

Gets an element at a specific index.

Parameters
iIndex
Returns
Reference to element at index

Definition at line 423 of file ring_buffer.hpp.

423 {
424 ensure(i < count, "OUT OF BOUNDS ACCESS");
425 auto index = i + head;
426 if (index >= elements.size()) {
427 index -= elements.size();
428 }
429 return elements[index];
430 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Represents a ring buffer with static memory allocation Can be used as a FIFO or LIFO queue Allows mem...
size_t size() const noexcept
Gets current size of the buffer.
Here is the caller graph for this function:

◆ at() [2/2]

template<typename T, size_t Capacity = 10>
T & mtcore::FixedRingBuffer< T, Capacity >::at ( size_t i)
inlinenodiscardnoexcept

Gets an element at a specific index.

Parameters
iIndex
Returns
Reference to element at index

Definition at line 437 of file ring_buffer.hpp.

437 {
438 ensure(i < count, "OUT OF BOUNDS ACCESS");
439 auto index = i + head;
440 if (index >= elements.size()) {
441 index -= elements.size();
442 }
443 return elements[index];
444 }

◆ init() [1/2]

template<typename T, size_t Capacity = 10>
void mtcore::FixedRingBuffer< T, Capacity >::init ( )
inline

Initializes a new fixed ring buffer.

Definition at line 387 of file ring_buffer.hpp.

387{};

◆ init() [2/2]

template<typename T, size_t Capacity = 10>
void mtcore::FixedRingBuffer< T, Capacity >::init ( std::initializer_list< T > list)
inline

Initializes a new fixed ring buffer with specific elements.

Definition at line 392 of file ring_buffer.hpp.

392 {
393 ensure(list.size() < Capacity, "TOO MANY ELEMENTS PROVIDED");
394 for (const auto &e: list) {
395 (void) push_back(e);
396 }
397 }
Result< void, CollectionAddNoAllocationError > push_back(const T &elem)
Here is the call graph for this function:

◆ iter()

template<typename T, size_t Capacity = 10>
decltype(auto) mtcore::FixedRingBuffer< T, Capacity >::iter ( )
inlinenodiscardnoexcept

Gets an iterator that returns copies of values.

Returns
Value iterator

Definition at line 382 of file ring_buffer.hpp.

382{ return iter::val(*this); }
ValIter< T > val(const T &r)
Generic value iterator that uses the operator[] and incrementing indexes to iterate over a collection...
Definition iter.hpp:114
Here is the call graph for this function:

◆ operator[]() [1/2]

template<typename T, size_t Capacity = 10>
const T & mtcore::FixedRingBuffer< T, Capacity >::operator[] ( size_t i) const
inlinenodiscardnoexcept

Gets an element at a specific index.

Parameters
iIndex
Returns
Reference to element at index

Definition at line 404 of file ring_buffer.hpp.

404{ return at(i); }
const T & at(size_t i) const noexcept
Here is the call graph for this function:

◆ operator[]() [2/2]

template<typename T, size_t Capacity = 10>
T & mtcore::FixedRingBuffer< T, Capacity >::operator[] ( size_t i)
inlinenodiscardnoexcept

Gets an element at a specific index.

Parameters
iIndex
Returns
Reference to element at index

Definition at line 411 of file ring_buffer.hpp.

411{ return at(i); }
Here is the call graph for this function:

◆ pop_back()

template<typename T, size_t Capacity = 10>
Optional< T > mtcore::FixedRingBuffer< T, Capacity >::pop_back ( )
inlinenodiscard

Removes an element from the back.

Returns
Optional with the value removed, or nullopt if no value was removed

Definition at line 509 of file ring_buffer.hpp.

509 {
510 if (count == 0) {
511 return nullopt;
512 }
513
514 auto new_tail = tail;
515 if (new_tail == 0) {
516 new_tail = elements.size() - 1;
517 }
518 else {
519 new_tail--;
520 }
521
522 auto res = elements[tail];
523 --count;
524 tail = new_tail;
525 return res;
526 }

◆ pop_front()

template<typename T, size_t Capacity = 10>
Optional< T > mtcore::FixedRingBuffer< T, Capacity >::pop_front ( )
inlinenodiscard

Removes an element from the front.

Returns
Optional with the value removed, or nullopt if no value was removed

Definition at line 491 of file ring_buffer.hpp.

491 {
492 if (count == 0) {
493 return nullopt;
494 }
495
496 auto res = std::move(elements[head++]);
497
498 count--;
499 if (head == elements.size()) {
500 head = 0;
501 }
502 return res;
503 }

◆ ptr_iter() [1/2]

template<typename T, size_t Capacity = 10>
decltype(auto) mtcore::FixedRingBuffer< T, Capacity >::ptr_iter ( ) const
inlinenodiscardnoexcept

Gets a constant pointer iterator to values.

Returns
constant pointer iterator

Definition at line 376 of file ring_buffer.hpp.

376{ return iter::const_ptr(*this); }
ConstPtrIter< T > const_ptr(const T &r)
Generic constant pointer iterator that uses the operator[] and incrementing indexes to iterate over a...
Definition iter.hpp:128
Here is the call graph for this function:

◆ ptr_iter() [2/2]

template<typename T, size_t Capacity = 10>
decltype(auto) mtcore::FixedRingBuffer< T, Capacity >::ptr_iter ( )
inlinenodiscardnoexcept

Gets a pointer iterator to values.

Returns
Pointer iterator

Definition at line 370 of file ring_buffer.hpp.

370{ return iter::ptr(*this); }
PtrIter< T > ptr(T &r)
Generic pointer iterator that uses the operator[] and incrementing indexes to iterate over a collecti...
Definition iter.hpp:101
Here is the call graph for this function:

◆ push_back()

template<typename T, size_t Capacity = 10>
Result< void, CollectionAddNoAllocationError > mtcore::FixedRingBuffer< T, Capacity >::push_back ( const T & elem)
inlinenodiscard

Adds an element to the back of the buffer.

Parameters
elemElement to add
Returns
Result indicating success or error

Definition at line 451 of file ring_buffer.hpp.

451 {
452 if (count >= elements.size()) {
454 }
455
456 elements[tail++] = elem;
457 count++;
458 if (tail == elements.size()) {
459 tail = 0;
460 }
461 return success();
462 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
Here is the call graph for this function:
Here is the caller graph for this function:

◆ push_front()

template<typename T, size_t Capacity = 10>
Result< void, CollectionAddNoAllocationError > mtcore::FixedRingBuffer< T, Capacity >::push_front ( const T & elem)
inlinenodiscard

Adds an element to the front of the buffer.

Parameters
elemElement to add
Returns
Result indicating success or error

Definition at line 469 of file ring_buffer.hpp.

469 {
470 if (count >= elements.size()) {
472 }
473
474 auto new_head = head;
475 if (new_head == 0) {
476 new_head = elements.size() - 1;
477 }
478 else {
479 new_head--;
480 }
482 count++;
483 head = new_head;
484 return success();
485 }
Here is the call graph for this function:

◆ size()

template<typename T, size_t Capacity = 10>
size_t mtcore::FixedRingBuffer< T, Capacity >::size ( ) const
inlinenodiscardnoexcept

Gets current size of the buffer.

Definition at line 416 of file ring_buffer.hpp.

416{ return count; }

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