uvco 0.1
Loading...
Searching...
No Matches
uvco::BoundedQueue< T > Class Template Reference

#include <bounded_queue.h>

Public Member Functions

 BoundedQueue (size_t capacity)
template<typename U>
requires std::convertible_to<U, T>
void put (U &&elem)
 Push an item to the queue.
get ()
 Pop an item from the queue.
unsigned size () const
 Current number of contained items.
unsigned capacity () const
 Maximum number of contained items.
bool empty () const
 size() == 0
bool hasSpace () const
 size() < capacity()
void forEach (std::function< void(T &)> function)

Private Attributes

std::vector< T > queue_ {}
unsigned head_ = 0
unsigned tail_ = 0
unsigned size_ = 0

Detailed Description

template<typename T>
class uvco::BoundedQueue< T >

A bounded FIFO queue based on a contiguous array.

Warning: only for internal use. The put()/get() interface is not safe in Release mode binaries; BoundedQueue is only intended to be used as part of Channel<T>.

Constructor & Destructor Documentation

◆ BoundedQueue()

template<typename T>
uvco::BoundedQueue< T >::BoundedQueue ( size_t capacity)
inlineexplicit
26{ queue_.reserve(capacity); }
std::vector< T > queue_
Definition bounded_queue.h:46
unsigned capacity() const
Maximum number of contained items.
Definition bounded_queue.h:37

Member Function Documentation

◆ capacity()

template<typename T>
unsigned uvco::BoundedQueue< T >::capacity ( ) const
inlinenodiscard

Maximum number of contained items.

37{ return queue_.capacity(); }

◆ empty()

template<typename T>
bool uvco::BoundedQueue< T >::empty ( ) const
inlinenodiscard

size() == 0

39{ return size() == 0; }
unsigned size() const
Current number of contained items.
Definition bounded_queue.h:35

◆ forEach()

template<typename T>
void uvco::BoundedQueue< T >::forEach ( std::function< void(T &)> function)
83 {
84 unsigned idx = tail_;
85 for (unsigned i = 0; i < size_; ++i) {
86 function(queue_.at(idx));
87 idx = (idx + 1) % capacity();
88 }
89}
Definition bounded_queue.h:24
unsigned size_
Definition bounded_queue.h:51
unsigned tail_
Definition bounded_queue.h:50

◆ get()

template<typename T>
T uvco::BoundedQueue< T >::get ( )
inline

Pop an item from the queue.

72 {
73 if (empty()) [[unlikely]] {
74 throw UvcoException(UV_EAGAIN, "queue is empty");
75 }
77 tail_ = tail_ % capacity();
78 --size_;
79 return element;
80}
bool empty() const
size() == 0
Definition bounded_queue.h:39

◆ hasSpace()

template<typename T>
bool uvco::BoundedQueue< T >::hasSpace ( ) const
inlinenodiscard

size() < capacity()

41{ return size() < capacity(); }

◆ put()

template<typename T>
requires std::convertible_to<U, T>
template<typename U>
requires std::convertible_to<U, T>
void uvco::BoundedQueue< T >::put ( U && elem)
inline

Push an item to the queue.

58{
59 if (!hasSpace()) {
60 throw UvcoException(UV_EBUSY, "queue is full");
61 }
62 if (queue_.size() < capacity()) {
64 queue_.push_back(std::forward<U>(elem));
65 } else {
67 }
68 head_ = (head_ + 1) % capacity();
69 ++size_;
70}
bool hasSpace() const
size() < capacity()
Definition bounded_queue.h:41
unsigned head_
Definition bounded_queue.h:48

◆ size()

template<typename T>
unsigned uvco::BoundedQueue< T >::size ( ) const
inlinenodiscard

Current number of contained items.

35{ return size_; }

Member Data Documentation

◆ head_

template<typename T>
unsigned uvco::BoundedQueue< T >::head_ = 0
private

◆ queue_

template<typename T>
std::vector<T> uvco::BoundedQueue< T >::queue_ {}
private
46{};

◆ size_

template<typename T>
unsigned uvco::BoundedQueue< T >::size_ = 0
private

◆ tail_

template<typename T>
unsigned uvco::BoundedQueue< T >::tail_ = 0
private

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