A circular-array implementation of a generic queue library for Arduino.
The GenericQueue<T> class is a data structure that effectively manages a collection of objects by adhering to the first-in, first-out (FIFO) principle.
template <typename T>
class GenericQueue
T
Specifies the type of elements in the queue.
Initializes a new instance of the GenericQueue<T> class that is empty and has the specified initial capacity.
GenericQueue(size_t capacity);
capacity
size_t
The initial number of elements that the GenericQueue<T> can contain.
capacity
is less than zero
.
Initialize an integer queue containing 12 queues.
#include "GenericQueue.h"
// Initialize an integer queue containing 12 queues.
GenericQueue<int> numbers(12);
Gets the number of elements contained in the GenericQueue<T>.
size_t const& count;
Int32
The number of elements contained in the GenericQueue<T>.
Gets a value that indicates whether the GenericQueue<T> is empty
.
bool isEmpty();
bool
true if the GenericQueue<T> is empty
; otherwise, false.
Gets a value that indicates whether the GenericQueue<T> is full
.
bool isFull();
bool
true if the GenericQueue<T> is full
; otherwise, false.
Removes all objects from the GenericQueue<T>.
void clear();
The following code example demonstrates several methods of the GenericQueue<T> class, including the Clear
property.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
numbers.enqueue("three");
Serial.print("\nThe number of order contained in the queue = ");
Serial.println(numbers.count);
Serial.println("\nRemoves all objects from the Queue.");
numbers.clear(); // Removes all objects from the Queue.
Serial.print("The number of order contained in the queue = ");
Serial.println(numbers.count);
}
void loop () {
}
The number of order contained in the queue = 3
Removes all objects from the Queue.
The number of order contained in the queue = 0
Removes and returns the object at the beginning of the GenericQueue<T>.
T dequeue();
T
The object that is removed from the beginning of the GenericQueue<T>.
The GenericQueue<T> is empty.
The following code example demonstrates several methods of the GenericQueue<T> class, including the Dequeue
method.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
numbers.enqueue("three");
Serial.print("Dequeuing '");
Serial.print(numbers.dequeue()); // Removes and returns the object at the beginning of the numbers.
Serial.println("'");
}
void loop () {
}
Dequeuing 'one'
Adds an object to the end of the GenericQueue<T>.
void Enqueue(T item);
item
T
The object to add to the GenericQueue<T>. The value can be null for reference types.
The following code example demonstrates several methods of the GenericQueue<T> class, including the Enqueue
method.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
numbers.enqueue("three");
numbers.enqueue("four");
numbers.enqueue("five");
Serial.println("The list of the numbers:");
for (size_t index = 0; index < numbers.count; index++) {
Serial.println(numbers[index]);
}
}
void loop () {
}
The list of the numbers:
one
two
three
four
five
Performs the specified action on each element of the GenericQueue<T>.
void forEach(Action<size_t, T> action)
size_t
The index number of the elements of the GenericQueue<T>.
T
The type of the elements of the GenericQueue<T>.
action
Action<size_t, T>
The Action<size_t, T> to perform on each element of the GenericQueue<T>.
action is null
.
The following code example demonstrates several methods of the GenericQueue<T> class, including the ForEach
method to display each element in GenericQueue<T>.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
numbers.enqueue("three");
numbers.enqueue("four");
numbers.enqueue("five");
Serial.println("The list of each number:");
numbers.forEach(Action);
}
void loop () {
}
void Action(size_t index, String number) {
Serial.print("Index ");
Serial.print(index);
Serial.print(": ");
Serial.println(number);
}
The list of each number:
one
two
three
four
five
Raises the StateChanged event.
void onStateChanged(QueueEventCallback function)
function
QueueEventCallback
A callback function when state changed.
void function(QueueEventArgs e, T item)
e
QueueEventArgs
An QueueEventArgs that contains the event data.
enum QueueState {
DEQUEUE,
ENQUEUE
};
typedef struct {
size_t index;
size_t size;
QueueState state;
} QueueEventArgs;
item
T
The object at the state changed.
The following code example demonstrates several methods of the GenericQueue<T> class, including the onStateChanged
method.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.onStateChanged(OnStateChanged); // Add a callback function when the state changes.
numbers.enqueue("one");
numbers.enqueue("two");
uint8_t count = numbers.count;
for (uint8_t index = 0; index < count; index++) {
Serial.print("\nPeek at next item to dequeue: ");
Serial.println(numbers.peek()); // Returns the object at the beginning without removing it.
numbers.dequeue(); // Removes and returns the object at the beginning of the numbers.
}
}
void loop () {
}
void OnStateChanged(QueueEventArgs e, String number) {
switch (e.state) {
case DEQUEUE:
Serial.print("Dequeuing '");
Serial.print(number);
Serial.println("'");
break;
case ENQUEUE:
Serial.print("Enqueuing '");
Serial.print(number);
Serial.println("'");
break;
}
}
Enqueuing 'one'
Enqueuing 'two'
Peek at next item to dequeue: one
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Returns the object at the beginning of the GenericQueue<T> without removing it.
T peek();
T
The object at the beginning of the GenericQueue<T>.
The GenericQueue<T> is empty.
The following code example demonstrates several methods of the GenericQueue<T> class, including the Peek
method.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
numbers.enqueue("three");
Serial.print("Dequeuing '");
Serial.print(numbers.dequeue()); // Removes and returns the object at the beginning of the numbers.
Serial.println("'");
Serial.print("Peek at next item to dequeue: ");
Serial.println(numbers.peek()); // Returns the object at the beginning without removing it.
Serial.print("Dequeuing '");
Serial.print(numbers.dequeue()); // Removes and returns the object at the beginning of the numbers.
Serial.println("'");
}
void loop () {
}
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Removes the object at the beginning of the GenericQueue<T>, and copies it to the result parameter.
bool tryDequeue(T *result);
result
T
If present, the object at the beginning of the GenericQueue<T>; otherwise, the default value of T
.
boo
true
if an element was removed and returned from the beginning of the GenericQueue<T> successfully; otherwise, false
.
The following code example demonstrates several methods of the GenericQueue<T> class, including the TryDequeue
method.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
Serial.println("The list of each number:");
numbers.forEach(Action);
uint8_t count = numbers.count;
Serial.print("\nCount: ");
Serial.println(count);
count++;
for (uint8_t index = 0; index < count; index++ ) {
// Removes the numbers at the beginning and copies it to the result.
// Return true if an element was removed. Otherwise, false.
String result;
bool successful = numbers.tryDequeue(&result);
Serial.print(successful ? "\nSuccessfully" : "\nUnsuccessfully");
Serial.print(" \tTry Dequeuing");
if (successful) {
Serial.print(" '");
Serial.print(result);
Serial.print ("'");
}
}
}
void loop () {
}
void Action(size_t index, String number) {
Serial.print("Index ");
Serial.print(index);
Serial.print(": ");
Serial.println(number);
}
The list of each number:
Index 0: one
Index 1: two
Count: 2
Successfully Try Dequeuing 'one'
Successfully Try Dequeuing 'two'
Unsuccessfully Try Dequeuing
Returns a value that indicates whether there is an object at the beginning of the GenericQueue<T>, and if one is present, copies it to the result parameter. The object is not removed from the GenericQueue<T>.
bool tryPeek(T *result);
result
T
If present, the object at the beginning of the GenericQueue<T>; otherwise, the default value of T
.
boolean
true
if there is an object at the beginning of the GenericQueue<T>; false
if the GenericQueue<T> is empty.
The following code example demonstrates several methods of the GenericQueue<T> class, including the TryDequeue
method.
#include "GenericQueue.h"
// Define the maximum of queues
const size_t numberOfQueues = 5;
// Initializes a new queue of the numbers
// that are empty and have the specified 5-capacity.
GenericQueue<String> numbers(numberOfQueues);
void setup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
Serial.println("The list of each number:");
numbers.forEach(Action);
uint8_t count = numbers.count;
Serial.print("\nCount: ");
Serial.println(count);
count++;
for (uint8_t index = 0; index < count; index++ ) {
// Removes the numbers at the beginning and copies it to the result.
// Return true if an element was removed. Otherwise, false.
String result;
bool successful = numbers.tryPeek(&result);
Serial.print(successful ? "\nSuccessfully" : "\nUnsuccessfully");
Serial.print(" \tTry Peek at next item to dequeue");
if (successful) {
Serial.print(": '");
Serial.print(result);
Serial.print ("'");
Serial.print("\nDequeuing '");
Serial.print(numbers.dequeue()); // Removes and returns the object at the beginning of the numbers.
Serial.print ("'");
}
}
}
void loop () {
}
void Action(size_t index, String number) {
Serial.print("Index ");
Serial.print(index);
Serial.print(": ");
Serial.println(number);
}
The list of each number:
Index 0: one
Index 1: two
Count: 2
Successfully Try Peek at next item to dequeue: 'one'
Dequeuing 'one'
Successfully Try Peek at next item to dequeue: 'two'
Dequeuing 'two'
Unsuccessfully Try Peek at next item to dequeue