Prusa MINI Firmware overview
CircularQueue< T, N > Class Template Reference

Circular Queue class. More...

#include <circularqueue.h>

Public Member Functions

 CircularQueue ()
 Class constructor. More...
 
dequeue ()
 Removes and returns a item from the queue. More...
 
bool enqueue (T const &item)
 Adds an item to the queue. More...
 
bool isEmpty ()
 Checks if the queue has no items. More...
 
bool isFull ()
 Checks if the queue is full. More...
 
uint8_t size ()
 Gets the queue size. More...
 
peek ()
 Gets the next item from the queue without removing it. More...
 
uint8_t count ()
 Gets the number of items on the queue. More...
 

Detailed Description

template<typename T, uint8_t N>
class CircularQueue< T, N >

Circular Queue class.

Marlin 3D Printer Firmware Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]

Based on Sprinter and grbl. Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Implementation of the classic ring buffer data structure

Constructor & Destructor Documentation

◆ CircularQueue()

template<typename T, uint8_t N>
CircularQueue< T, N >::CircularQueue ( )

Class constructor.

This class requires two template parameters, T defines the type of item this queue will handle and N defines the maximum number of items that can be stored on the queue.

54  {
55  buffer.size = N;
56  buffer.count = buffer.head = buffer.tail = 0;
57  }

Member Function Documentation

◆ dequeue()

template<typename T, uint8_t N>
T CircularQueue< T, N >::dequeue ( )

Removes and returns a item from the queue.

Removes the oldest item on the queue, pointed to by the buffer_t head field. The item is returned to the caller.

Returns
type T item
65  {
66  if (isEmpty()) return T();
67 
68  uint8_t index = buffer.head;
69 
70  --buffer.count;
71  if (++buffer.head == buffer.size)
72  buffer.head = 0;
73 
74  return buffer.queue[index];
75  }
Here is the caller graph for this function:

◆ enqueue()

template<typename T, uint8_t N>
bool CircularQueue< T, N >::enqueue ( T const item)

Adds an item to the queue.

Adds an item to the queue on the location pointed by the buffer_t tail variable. Returns false if no queue space is available.

Parameters
itemItem to be added to the queue
Returns
true if the operation was successful
84  {
85  if (isFull()) return false;
86 
87  buffer.queue[buffer.tail] = item;
88 
89  ++buffer.count;
90  if (++buffer.tail == buffer.size)
91  buffer.tail = 0;
92 
93  return true;
94  }

◆ isEmpty()

template<typename T, uint8_t N>
bool CircularQueue< T, N >::isEmpty ( )

Checks if the queue has no items.

Returns true if there are no items on the queue, false otherwise.

Returns
true if queue is empty
101 { return buffer.count == 0; }
Here is the caller graph for this function:

◆ isFull()

template<typename T, uint8_t N>
bool CircularQueue< T, N >::isFull ( )

Checks if the queue is full.

Returns true if the queue is full, false otherwise.

Returns
true if queue is full
108 { return buffer.count == buffer.size; }
Here is the caller graph for this function:

◆ size()

template<typename T, uint8_t N>
uint8_t CircularQueue< T, N >::size ( )

Gets the queue size.

Returns the maximum number of items a queue can have.

Returns
the queue size
115 { return buffer.size; }

◆ peek()

template<typename T, uint8_t N>
T CircularQueue< T, N >::peek ( void  )

Gets the next item from the queue without removing it.

Returns the next item in the queue without removing it or updating the pointers.

Returns
first item in the queue
123 { return buffer.queue[buffer.head]; }

◆ count()

template<typename T, uint8_t N>
uint8_t CircularQueue< T, N >::count ( )

Gets the number of items on the queue.

Returns the current number of items stored on the queue.

Returns
number of items in the queue
130 { return buffer.count; }
CircularQueue::isFull
bool isFull()
Checks if the queue is full.
Definition: circularqueue.h:108
CircularQueue::isEmpty
bool isEmpty()
Checks if the queue has no items.
Definition: circularqueue.h:101
uint8_t
const uint8_t[]
Definition: 404_html.c:3