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

Statically allocated FIFO queue with fixed maximum capacity. More...

#include <queue.hpp>

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

Public Member Functions

Result< void, AllocationErrorinit ()
 Initializes Queue with capacity.
 
Result< void, AllocationErrorinit (std::initializer_list< T > init)
 Initializes Queue with initial items.
 
size_t size () const noexcept
 
size_t capacity () const noexcept
 
Optional< T > peek () const noexcept
 Peeks at first element of Queue.
 
Optional< T > pop () noexcept
 Removes and returns the next element in the Queue.
 
Result< void, CollectionAddNoAllocationErrorpush (const T &elem) noexcept
 Tries to add an element to the Queue Fails if Queue is empty.
 
bool is_full () const noexcept
 Checks if ring buffer is full.
 
bool is_empty () const noexcept
 Checks if ring buffer is empty.
 

Detailed Description

template<typename T, size_t Capacity>
struct mtcore::FixedQueue< T, Capacity >

Statically allocated FIFO queue with fixed maximum capacity.

Template Parameters
TElement to store
CapacityMaximum capacity of queue

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

Definition at line 156 of file queue.hpp.

Member Function Documentation

◆ capacity()

template<typename T, size_t Capacity>
size_t mtcore::FixedQueue< T, Capacity >::capacity ( ) const
inlinenodiscardnoexcept
Returns
Capacity of Queue

Definition at line 182 of file queue.hpp.

182{ return Capacity; }
Statically allocated FIFO queue with fixed maximum capacity.
Definition queue.hpp:156
Here is the caller graph for this function:

◆ init() [1/2]

template<typename T, size_t Capacity>
Result< void, AllocationError > mtcore::FixedQueue< T, Capacity >::init ( )
inline

Initializes Queue with capacity.

Returns
success or failure

Definition at line 165 of file queue.hpp.

165{ return buffer.init(); }
Here is the caller graph for this function:

◆ init() [2/2]

template<typename T, size_t Capacity>
Result< void, AllocationError > mtcore::FixedQueue< T, Capacity >::init ( std::initializer_list< T > init)
inline

Initializes Queue with initial items.

Parameters
initInitial elements
Returns
success or failure

Definition at line 172 of file queue.hpp.

172{ return buffer.init(init); }
Result< void, AllocationError > init()
Initializes Queue with capacity.
Definition queue.hpp:165
Here is the call graph for this function:

◆ is_empty()

template<typename T, size_t Capacity>
bool mtcore::FixedQueue< T, Capacity >::is_empty ( ) const
inlinenodiscardnoexcept

Checks if ring buffer is empty.

Returns
true if ring buffer is empty

Definition at line 225 of file queue.hpp.

225{ return size() == 0; }
size_t size() const noexcept
Definition queue.hpp:177
Here is the call graph for this function:

◆ is_full()

template<typename T, size_t Capacity>
bool mtcore::FixedQueue< T, Capacity >::is_full ( ) const
inlinenodiscardnoexcept

Checks if ring buffer is full.

Returns
true if ring buffer is full

Definition at line 219 of file queue.hpp.

219{ return size() >= Capacity; }
Here is the call graph for this function:

◆ peek()

template<typename T, size_t Capacity>
Optional< T > mtcore::FixedQueue< T, Capacity >::peek ( ) const
inlinenoexcept

Peeks at first element of Queue.

Returns
First elem or nullopt

Definition at line 188 of file queue.hpp.

188 {
189 if (buffer.size() > 0) {
190 return buffer.at(0);
191 }
192 return nullopt;
193 }

◆ pop()

template<typename T, size_t Capacity>
Optional< T > mtcore::FixedQueue< T, Capacity >::pop ( )
inlinenoexcept

Removes and returns the next element in the Queue.

Returns
Next elem in Queue or nullopt

Definition at line 199 of file queue.hpp.

199 {
200 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
201 return buffer.pop_front();
202 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
size_t capacity() const noexcept
Definition queue.hpp:182
Here is the call graph for this function:

◆ push()

template<typename T, size_t Capacity>
Result< void, CollectionAddNoAllocationError > mtcore::FixedQueue< T, Capacity >::push ( const T & elem)
inlinenoexcept

Tries to add an element to the Queue Fails if Queue is empty.

Parameters
elemElement to add
Returns
Success or failure

Definition at line 210 of file queue.hpp.

210 {
211 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
212 return buffer.push_back(elem);
213 }
Here is the call graph for this function:

◆ size()

template<typename T, size_t Capacity>
size_t mtcore::FixedQueue< T, Capacity >::size ( ) const
inlinenodiscardnoexcept
Returns
Size of Queue

Definition at line 177 of file queue.hpp.

177{ return buffer.size(); }
Here is the caller graph for this function:

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