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<typenameT>classGenericQueueT
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_tcapacity);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_tconst&count;Int32
The number of elements contained in the GenericQueue<T>.
Gets a value that indicates whether the GenericQueue<T> is empty.
boolisEmpty();bool
true if the GenericQueue<T> is empty; otherwise, false.
Gets a value that indicates whether the GenericQueue<T> is full.
boolisFull();bool
true if the GenericQueue<T> is full; otherwise, false.
Removes all objects from the GenericQueue<T>.
voidclear();The following code example demonstrates several methods of the GenericQueue<T> class, including the Clear property.
#include"GenericQueue.h"// Define the maximum of queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
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);
}
voidloop () {
}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>.
Tdequeue();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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
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("'");
}
voidloop () {
}Dequeuing 'one'
Adds an object to the end of the GenericQueue<T>.
voidEnqueue(Titem);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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
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_tindex=0; index<numbers.count; index++) {
Serial.println(numbers[index]);
}
}
voidloop () {
}The list of the numbers:
one
two
three
four
five
Performs the specified action on each element of the GenericQueue<T>.
voidforEach(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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
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);
}
voidloop () {
}
voidAction(size_tindex, Stringnumber) {
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.
voidonStateChanged(QueueEventCallbackfunction)function QueueEventCallback
A callback function when state changed.
voidfunction(QueueEventArgse, Titem)e QueueEventArgs
An QueueEventArgs that contains the event data.
enumQueueState {
DEQUEUE,
ENQUEUE
};
typedefstruct {
size_tindex;
size_tsize;
QueueStatestate;
} 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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
Serial.begin(115200);
numbers.onStateChanged(OnStateChanged); // Add a callback function when the state changes.numbers.enqueue("one");
numbers.enqueue("two");
uint8_tcount=numbers.count;
for (uint8_tindex=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.
}
}
voidloop () {
}
voidOnStateChanged(QueueEventArgse, Stringnumber) {
switch (e.state) {
caseDEQUEUE:
Serial.print("Dequeuing '");
Serial.print(number);
Serial.println("'");
break;
caseENQUEUE:
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.
Tpeek();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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
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("'");
}
voidloop () {
}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.
booltryDequeue(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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
Serial.println("The list of each number:");
numbers.forEach(Action);
uint8_tcount=numbers.count;
Serial.print("\nCount: ");
Serial.println(count);
count++;
for (uint8_tindex=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.Stringresult;
boolsuccessful=numbers.tryDequeue(&result);
Serial.print(successful ? "\nSuccessfully" : "\nUnsuccessfully");
Serial.print(" \tTry Dequeuing");
if (successful) {
Serial.print(" '");
Serial.print(result);
Serial.print ("'");
}
}
}
voidloop () {
}
voidAction(size_tindex, Stringnumber) {
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>.
booltryPeek(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 queuesconstsize_tnumberOfQueues=5;
// Initializes a new queue of the numbers// that are empty and have the specified 5-capacity.GenericQueue<String>numbers(numberOfQueues);
voidsetup() {
Serial.begin(115200);
numbers.enqueue("one");
numbers.enqueue("two");
Serial.println("The list of each number:");
numbers.forEach(Action);
uint8_tcount=numbers.count;
Serial.print("\nCount: ");
Serial.println(count);
count++;
for (uint8_tindex=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.Stringresult;
boolsuccessful=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 ("'");
}
}
}
voidloop () {
}
voidAction(size_tindex, Stringnumber) {
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