ExternalFifoQueue
ExternalFifoQueue is a final class template
defined in Fw/DataStructures.
It represents a FIFO queue with external storage.
Internally it maintains an ExternalArray for
storing the items on the queue.
1. Template Parameters
ExternalFifoQueue has the following template parameters.
| Kind | Name | Purpose |
|---|---|---|
typename |
T |
The type of an item on the queue |
2. Base Class
ExternalFifoQueue<T> is publicly derived from
FifoQueueBase<T>.
3. Private Member Variables
ExternalFifoQueue has the following private member variables.
| Name | Type | Purpose | Default Value |
|---|---|---|---|
m_items |
ExternalArray<T> |
The array for storing the queue items | C++ default initialization |
m_enqueueIndex |
CircularIndex |
The enqueue index | CircularIndex(m_items.size(), 0) |
m_dequeueIndex |
CircularIndex |
The dequeue index | CircularIndex(m_items.size(), 0) |
m_size |
FwSizeType |
The number of items on the queue | 0 |
classDiagram
FifoQueue *-- ExternalArray
FifoQueue *-- CircularIndex
FifoQueue *-- CircularIndex
4. Public Constructors and Destructors
4.1. Zero-Argument Constructor
Initialize each member variable with its default value.
Example:
4.2. Constructor Providing Typed Backing Storage
items must point to a primitive array of at least capacity
items of type T.
-
Call
setStorage(items, capacity). -
Initialize the other member variables with their default values.
Example:
constexpr FwSizeType capacity = 10;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
4.3. Constructor Providing Untyped Backing Storage
data must be aligned according to
getByteArrayAlignment() and must
contain at least getByteArraySize(capacity) bytes.
-
Call
setStorage(data, capacity). -
Initialize the other member variables with their default values.
Example:
constexpr FwSizeType capacity = 10;
constexpr U8 alignment = ExternalFifoQueue<U32>::getByteArrayAlignment();
constexpr FwSizeType byteArraySize = ExternalFifoQueue<U32>::getByteArraySize(capacity);
alignas(alignment) U8 bytes[byteArraySize];
ExternalFifoQueue<U32> queue(ByteArray(&bytes[0], sizeof bytes), capacity);
4.4. Copy Constructor
Set *this = queue.
Example:
constexpr FwSizeType capacity = 3;
U32 items[capacity];
// Call the constructor providing backing storage
ExternalFifoQueue<U32> q1(items, capacity);
// Enqueue an item
U32 value = 42;
(void) q1.enqueue(value);
// Call the copy constructor
ExternalFifoQueue<U32> q2(q1);
ASSERT_EQ(q2.getSize(), 1);
4.5. Destructor
Defined as = default.
5. Public Member Functions
5.1. operator=
-
If
&queue != this-
Set
m_items = queue.m_items. -
Set
m_enqueueIndex = queue.m_enqueueIndex. -
Set
m_dequeueIndex = queue.m_dequeueIndex. -
Set
m_size = queue.m_size.
-
-
Return
*this.
Example:
constexpr FwSizeType capacity = 3;
U32 items[capacity];
// Call the constructor providing backing storage
ExternalFifoQueue<U32> q1(items, capacity);
// Enqueue an item
U32 value = 42;
(void) q1.enqueue(value);
// Call the default constructor
ExternalFifoQueue q2;
ASSERT_EQ(q2.getSize(), 0);
// Call the copy assignment operator
q2 = q1;
ASSERT_EQ(q2.getSize(), 1);
5.2. clear
-
Call
m_enqueueIndex.setValue(0). -
Call
m_dequeueIndex.setValue(0). -
Set
m_size = 0.
Example:
constexpr FwSizeType capacity = 10;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
const auto status = queue.enqueue(3);
ASSERT_EQ(queue.getSize(), 1);
queue.clear();
ASSERT_EQ(queue.getSize(), 0);
5.3. setStorage (Typed Data)
items must point to a primitive array of at least capacity
items of type T.
-
Call
m_items.setStorage(items, capacity). -
If
capacity > 0-
Call
this->m_enqueueIndex.setModulus(capacity). -
Call
this->m_dequeueIndex.setModulus(capacity).
-
-
Call
this->clear().
Example:
constexpr FwSizeType capacity = 10;
ExternalFifoQueue<U32> queue;
U32 items[capacity];
queue.setStorage(items, capacity);
5.4. setStorage (Untyped Data)
data must be aligned according to
getByteArrayAlignment() and must
contain at least getByteArraySize(capacity) bytes.
-
Call
m_items.setStorage(data, capacity). -
If
capacity > 0-
Call
this->m_enqueueIndex.setModulus(capacity). -
Call
this->m_dequeueIndex.setModulus(capacity).
-
-
Call
this->clear().
Example:
constexpr FwSizeType capacity = 10;
constexpr U8 alignment = ExternalFifoQueue<U32>::getByteArrayAlignment();
constexpr FwSizeType byteArraySize = ExternalFifoQueue<U32>::getByteArraySize(capacity);
alignas(alignment) U8 bytes[byteArraySize];
ExternalFifoQueue<U32> queue;
queue.setStorage(ByteArray(&bytes[0], sizeof bytes), capacity);
5.5. enqueue
-
Set
status = Success::FAILURE. -
If
m_size < getCapacity()then-
Set
i = m_enqueueIndex.getValue(). -
Set
m_items[i] = e. -
Call
m_enqueueIndex.increment(). -
Increment
m_size.
-
-
Return
status.
Example:
constexpr FwSizeType capacity = 3;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
ASSERT_EQ(queue.getSize(), 0);
auto status = queue.enqueue(42);
ASSERT_EQ(status, Success::SUCCESS);
ASSERT_EQ(queue.getSize(), 1);
5.6. at
-
Assert
index < m_size. -
Set
ci = m_dequeueIndex. -
Set
i = ci.increment(index). -
Return
m_items[i].
Example:
constexpr FwSizeType capacity = 3;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
const auto status = queue.enqueue(3);
ASSERT_EQ(status, Success::SUCCESS);
ASSERT_EQ(queue.at(0), 3);
ASSERT_DEATH(queue.at(1), "Assert");
5.7. dequeue
-
Set
status = Success::FAILURE. -
If
m_size > 0then-
Set
i = m_dequeueIndex.getValue(). -
Set
e = m_items[i]. -
Call
m_dequeueIndex.increment(). -
Decrement
m_size.
-
-
Return
status.
Example:
constexpr FwSizeType capacity = 3;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
U32 val;
auto status = queue.dequeue(val);
ASSERT_EQ(status, Success::FAILURE);
status = queue.enqueue(42);
ASSERT_EQ(status, Success::SUCCESS);
status = queue.dequeue(val);
ASSERT_EQ(status, Success::SUCCESS);
ASSERT_EQ(val, 42);
5.8. getSize
Return m_size.
Example:
constexpr FwSizeType capacity = 10;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
auto size = queue.getSize();
ASSERT_EQ(size, 0);
const auto status = queue.enqueue(3);
ASSERT_EQ(status, Success::SUCCESS);
size = queue.getSize();
ASSERT_EQ(size, 1);
5.9. getCapacity
Return m_items.getSize().
Example:
constexpr FwSizeType capacity = 10;
U32 items[capacity];
ExternalFifoQueue<U32> queue(items, capacity);
ASSERT_EQ(queue.getCapacity(), capacity);
6. Public Static Functions
6.1. getByteArrayAlignment
Return ExternalArray<T>::getByteArrayAlignment().
6.2. getByteArraySize
Return ExternalArray<T>::getByteArraySize(capacity).