GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/ExternalFifoQueue.hpp
Date: 2026-09-23 22:11:34
Exec Total Coverage
Lines: 17 41 41.5%
Functions: 4 9 44.4%
Branches: 2 18 11.1%

Line Branch Exec Source
1 // ======================================================================
2 // \file ExternalFifoQueue.hpp
3 // \author bocchino
4 // \brief A FIFO queue with external storage
5 // ======================================================================
6
7 #ifndef Fw_ExternalFifoQueue_HPP
8 #define Fw_ExternalFifoQueue_HPP
9
10 #include "Fw/DataStructures/CircularIndex.hpp"
11 #include "Fw/DataStructures/ExternalArray.hpp"
12 #include "Fw/DataStructures/FifoQueueBase.hpp"
13 #include "Fw/Types/ByteArray.hpp"
14
15 namespace Fw {
16
17 template <typename T>
18 class ExternalFifoQueue final : public FifoQueueBase<T> {
19 // ----------------------------------------------------------------------
20 // Friend class for testing
21 // ----------------------------------------------------------------------
22
23 template <typename TT>
24 friend class ExternalFifoQueueTester;
25
26 public:
27 // ----------------------------------------------------------------------
28 // Public constructors and destructors
29 // ----------------------------------------------------------------------
30
31 //! Zero-argument constructor
32 ExternalFifoQueue() = default;
33
34 //! Constructor providing typed backing storage
35 1 ExternalFifoQueue(T* items, //!< The items
36 FwSizeType capacity //!< The capacity
37 )
38 1 : FifoQueueBase<T>() {
39
1/1
✓ Branch 1 taken 1 times.
1 this->setStorage(items, capacity);
40 1 }
41
42 //! Constructor providing untyped backing storage
43 ExternalFifoQueue(ByteArray data, //!< The data
44 FwSizeType capacity //!< The capacity
45 )
46 : FifoQueueBase<T>() {
47 this->setStorage(data, capacity);
48 }
49
50 //! Copy constructor
51 ExternalFifoQueue(const ExternalFifoQueue<T>& queue) : FifoQueueBase<T>() { *this = queue; }
52
53 //! Destructor
54 2 ~ExternalFifoQueue() override = default;
55
56 public:
57 // ----------------------------------------------------------------------
58 // Public member functions
59 // ----------------------------------------------------------------------
60
61 //! operator=
62 ExternalFifoQueue<T>& operator=(const ExternalFifoQueue<T>& queue) {
63 if (&queue != this) {
64 this->m_items = queue.m_items;
65 this->m_enqueueIndex = queue.m_enqueueIndex;
66 this->m_dequeueIndex = queue.m_dequeueIndex;
67 this->m_size = queue.m_size;
68 }
69 return *this;
70 }
71
72 //! Clear the queue
73 1 void clear() override {
74 1 this->m_enqueueIndex.setValue(0);
75 1 this->m_dequeueIndex.setValue(0);
76 1 this->m_size = 0;
77 1 }
78
79 //! Set the storage (typed data)
80 1 void setStorage(T* items, //!< The items
81 FwSizeType capacity //!< The capacity
82 ) {
83 1 this->m_items.setStorage(items, capacity);
84
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (capacity > 0) {
85 1 this->m_enqueueIndex.setModulus(capacity);
86 1 this->m_dequeueIndex.setModulus(capacity);
87 }
88 1 this->clear();
89 1 }
90
91 //! Set the storage (untyped data)
92 void setStorage(ByteArray data, //!< The data
93 FwSizeType capacity //!< The capacity
94 ) {
95 this->m_items.setStorage(data, capacity);
96 if (capacity > 0) {
97 this->m_enqueueIndex.setModulus(capacity);
98 this->m_dequeueIndex.setModulus(capacity);
99 }
100 this->clear();
101 }
102
103 //! Enqueue an element (push on the right)
104 //! \return SUCCESS if element enqueued
105 ✗ Success enqueue(const T& e //!< The element (output)
106 ) override {
107 ✗ auto status = Success::FAILURE;
108 ✗ if (this->m_size < this->getCapacity()) {
109 ✗ const auto i = this->m_enqueueIndex.getValue();
110 ✗ this->m_items[i] = e;
111 ✗ (void)this->m_enqueueIndex.increment();
112 ✗ this->m_size++;
113 ✗ status = Success::SUCCESS;
114 }
115 ✗ return status;
116 }
117
118 //! Get an item at an index.
119 //! Indices go from left to right in the queue.
120 //! Fails an assertion if the index is out of range.
121 //! \return The item
122 ✗ const T& at(FwSizeType index //!< The index
123 ) const override {
124 ✗ FW_ASSERT(index < this->m_size, static_cast<FwAssertArgType>(index),
125 static_cast<FwAssertArgType>(this->m_size));
126 ✗ auto ci = this->m_dequeueIndex;
127 ✗ const auto i = ci.increment(index);
128 ✗ return this->m_items[i];
129 }
130
131 //! Dequeue an element (remove from the left)
132 //! \return SUCCESS if element dequeued
133 ✗ Success dequeue(T& e //!< The element (output)
134 ) override {
135 ✗ auto status = Success::FAILURE;
136 ✗ if (this->m_size > 0) {
137 ✗ e = this->at(0);
138 ✗ (void)this->m_dequeueIndex.increment();
139 ✗ this->m_size--;
140 ✗ status = Success::SUCCESS;
141 }
142 ✗ return status;
143 }
144
145 //! Get the size (number of items stored in the queue)
146 //! \return The size
147 ✗ FwSizeType getSize() const override { return this->m_size; }
148
149 //! Get the capacity (maximum number of items stored in the queue)
150 //! \return The capacity
151 ✗ FwSizeType getCapacity() const override { return this->m_items.getSize(); }
152
153 public:
154 // ----------------------------------------------------------------------
155 // Public static functions
156 // ----------------------------------------------------------------------
157
158 //! Get the alignment of the storage for an ExternalFifoQueue
159 //! \return The alignment
160 static constexpr U8 getByteArrayAlignment() { return ExternalArray<T>::getByteArrayAlignment(); }
161
162 //! Get the size of the storage for an ExternalFifoQueue of the specified
163 //! capacity, as a byte array
164 //! \return The byte array size
165 static constexpr FwSizeType getByteArraySize(FwSizeType capacity //!< The capacity
166 ) {
167 return ExternalArray<T>::getByteArraySize(capacity);
168 }
169
170 private:
171 // ----------------------------------------------------------------------
172 // Private member variables
173 // ----------------------------------------------------------------------
174
175 //! The array for storing the queue items
176 ExternalArray<T> m_items = {};
177
178 //! The enqueue index
179 CircularIndex m_enqueueIndex = {};
180
181 //! The dequeue index
182 CircularIndex m_dequeueIndex = {};
183
184 //! The number of items on the queue
185 FwSizeType m_size = 0;
186 };
187
188 } // namespace Fw
189
190 #endif
191