58 template<
typename T_,
typename TREF_ = const T_&>
class Queue
78 template<u
int8_t SIZE>
explicit Queue(
T (&buffer)[SIZE],
bool locked =
false)
79 : buffer_{buffer}, size_{SIZE}, locked_{locked} {}
212 template<u
int8_t SIZE> uint8_t
peek_(
T (&buffer)[SIZE])
const;
235 return tail_ == head_;
248 return (tail_ + 1) == (head_ ? head_ : size_);
263 return tail_ - head_ + (tail_ >= head_ ? 0 : size_);
280 return head_ - tail_ - 1 + (tail_ >= head_ ? size_ : 0);
313 synchronized return push_(item);
335 synchronized return pull_(item);
358 synchronized return peek_(item);
409 template<u
int8_t SIZE> uint8_t
peek(
T (&buffer)[SIZE])
const
411 synchronized return peek_(buffer);
422 synchronized return empty_();
434 synchronized return items_();
448 synchronized return free_();
461 synchronized return full_();
481 volatile uint8_t head_ = 0;
482 volatile uint8_t tail_ = 0;
488 if (empty_())
return false;
489 item = buffer_[head_];
495 size = (size <= items_()) ? size : items_();
501 const T* source = &buffer_[head_];
502 for (uint8_t i = 0; i < size; ++i) *buffer++ = *source++;
506 uint8_t part_size = size_ - head_;
507 if (part_size > size) part_size = size;
508 const T* source = &buffer_[head_];
509 for (uint8_t i = 0; i < part_size; ++i) *buffer++ = *source++;
511 if (size > part_size)
513 part_size = size - part_size;
515 for (uint8_t i = 0; i < part_size; ++i) *buffer++ = *source++;
522 template<
typename T,
typename TREF>
template<u
int8_t SIZE> uint8_t
Queue<T, TREF>::peek_(T (&buffer)[SIZE])
const
524 return peek_(&buffer[0], SIZE);
529 if (locked_ || full_())
return false;
530 buffer_[tail_] = item;
532 if (tail_ == size_) tail_ = 0;
538 if (empty_())
return false;
539 item = buffer_[head_];
541 if (head_ == size_) head_ = 0;
bool pull(T &item)
Pull an item from the beginning of this queue, if not empty, and copy it into item.
uint8_t peek(T *buffer, uint8_t size) const
Peek up to size items from the beginning of this queue, if not empty, and copy these into buffer arra...
uint8_t peek(T(&buffer)[SIZE]) const
Peek up to SIZE items from the beginning of this queue, if not empty, and copy these into buffer arra...
bool push(TREF item)
Push item to the end of this queue, provided there is still available space in its ring buffer.
void clear_()
Completely clear this queue.
bool empty_() const
Tell if this queue is currently empty.
uint8_t items_() const
Tell the current number of items currently present in this queue.
bool pull_(T &item)
Pull an item from the beginning of this queue, if not empty, and copy it into item.
TREF_ TREF
The constant reference type of items in this queue.
bool empty() const
Tell if this queue is currently empty.
T_ T
The type of items in this queue.
void clear()
Completely clear this queue.
uint8_t size() const
Get the maximum size of this queue.
bool peek(T &item) const
Peek an item from the beginning of this queue, if not empty, and copy it into item.
Queue(T(&buffer)[SIZE], bool locked=false)
Create a new queue, based on the provided buffer array.
bool full() const
Tell if this queue is currently full.
void lock()
Lock this queue, ie prevent pushing any data to it.
uint8_t free() const
Tell the current number of available locations for items to be pushed to this queue.
uint8_t peek_(T *buffer, uint8_t size) const
Peek up to size items from the beginning of this queue, if not empty, and copy these into buffer arra...
uint8_t free_() const
Tell the current number of available locations for items to be pushed to this queue.
uint8_t items() const
Tell the current number of items currently present in this queue.
void unlock()
Unlock this queue, ie allow pushing data to it.
uint8_t peek_(T(&buffer)[SIZE]) const
Peek up to SIZE items from the beginning of this queue, if not empty, and copy these into buffer arra...
bool full_() const
Tell if this queue is currently full.
bool push_(TREF item)
Push item to the end of this queue, provided there is still available space in its ring buffer.
bool peek_(T &item) const
Peek an item from the beginning of this queue, if not empty, and copy it into item.
bool is_locked() const
Check if this queue is locked, ie if pushing data to it is disabled.
Contains all FastArduino generic containers:
T peek(Queue< T, TREF > &queue)
Peek an item from the beginning of queue.
T pull(Queue< T, TREF > &queue)
Pull an item from the beginning of queue.
void yield()
Utility method used by many FastArduino API in order to "yield" some processor time; concretely it ju...
General utilities API that have broad application in programs.