Prusa MINI Firmware overview
circularqueue.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 #include <stdint.h>
25 
26 /**
27  * @brief Circular Queue class
28  * @details Implementation of the classic ring buffer data structure
29  */
30 template<typename T, uint8_t N>
32  private:
33 
34  /**
35  * @brief Buffer structure
36  * @details This structure consolidates all the overhead required to handle
37  * a circular queue such as the pointers and the buffer vector.
38  */
39  struct buffer_t {
40  uint8_t head;
41  uint8_t tail;
42  uint8_t count;
43  uint8_t size;
44  T queue[N];
45  } buffer;
46 
47  public:
48  /**
49  * @brief Class constructor
50  * @details This class requires two template parameters, T defines the type
51  * of item this queue will handle and N defines the maximum number of
52  * items that can be stored on the queue.
53  */
55  buffer.size = N;
56  buffer.count = buffer.head = buffer.tail = 0;
57  }
58 
59  /**
60  * @brief Removes and returns a item from the queue
61  * @details Removes the oldest item on the queue, pointed to by the
62  * buffer_t head field. The item is returned to the caller.
63  * @return type T item
64  */
65  T dequeue() {
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  }
76 
77  /**
78  * @brief Adds an item to the queue
79  * @details Adds an item to the queue on the location pointed by the buffer_t
80  * tail variable. Returns false if no queue space is available.
81  * @param item Item to be added to the queue
82  * @return true if the operation was successful
83  */
84  bool enqueue(T const &item) {
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  }
95 
96  /**
97  * @brief Checks if the queue has no items
98  * @details Returns true if there are no items on the queue, false otherwise.
99  * @return true if queue is empty
100  */
101  bool isEmpty() { return buffer.count == 0; }
102 
103  /**
104  * @brief Checks if the queue is full
105  * @details Returns true if the queue is full, false otherwise.
106  * @return true if queue is full
107  */
108  bool isFull() { return buffer.count == buffer.size; }
109 
110  /**
111  * @brief Gets the queue size
112  * @details Returns the maximum number of items a queue can have.
113  * @return the queue size
114  */
115  uint8_t size() { return buffer.size; }
116 
117  /**
118  * @brief Gets the next item from the queue without removing it
119  * @details Returns the next item in the queue without removing it
120  * or updating the pointers.
121  * @return first item in the queue
122  */
123  T peek() { return buffer.queue[buffer.head]; }
124 
125  /**
126  * @brief Gets the number of items on the queue
127  * @details Returns the current number of items stored on the queue.
128  * @return number of items in the queue
129  */
130  uint8_t count() { return buffer.count; }
131 };
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
queue
GCodeQueue queue
Definition: queue.cpp:28
CircularQueue::peek
T peek()
Gets the next item from the queue without removing it.
Definition: circularqueue.h:123
CircularQueue::size
uint8_t size()
Gets the queue size.
Definition: circularqueue.h:115
CircularQueue::dequeue
T dequeue()
Removes and returns a item from the queue.
Definition: circularqueue.h:65
CircularQueue::count
uint8_t count()
Gets the number of items on the queue.
Definition: circularqueue.h:130
uint8_t
const uint8_t[]
Definition: 404_html.c:3
CircularQueue
Circular Queue class.
Definition: circularqueue.h:31
CircularQueue::enqueue
bool enqueue(T const &item)
Adds an item to the queue.
Definition: circularqueue.h:84