| 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 | 2 | ExternalFifoQueue() = default; | |
| 33 | |||
| 34 | //! Constructor providing typed backing storage | ||
| 35 | 32 | ExternalFifoQueue(T* items, //!< The items | |
| 36 | FwSizeType capacity //!< The capacity | ||
| 37 | ) | ||
| 38 | 32 | : FifoQueueBase<T>() { | |
| 39 |
1/1✓ Branch 4 taken 32 times.
|
32 | this->setStorage(items, capacity); |
| 40 | 32 | } | |
| 41 | |||
| 42 | //! Constructor providing untyped backing storage | ||
| 43 | 1 | ExternalFifoQueue(ByteArray data, //!< The data | |
| 44 | FwSizeType capacity //!< The capacity | ||
| 45 | ) | ||
| 46 | 1 | : FifoQueueBase<T>() { | |
| 47 |
1/1✓ Branch 4 taken 1 times.
|
1 | this->setStorage(data, capacity); |
| 48 | 1 | } | |
| 49 | |||
| 50 | //! Copy constructor | ||
| 51 |
1/1✓ Branch 29 taken 1 times.
|
1 | ExternalFifoQueue(const ExternalFifoQueue<T>& queue) : FifoQueueBase<T>() { *this = queue; } |
| 52 | |||
| 53 | //! Destructor | ||
| 54 | 72 | ~ExternalFifoQueue() override = default; | |
| 55 | |||
| 56 | public: | ||
| 57 | // ---------------------------------------------------------------------- | ||
| 58 | // Public member functions | ||
| 59 | // ---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | //! operator= | ||
| 62 | 2 | ExternalFifoQueue<T>& operator=(const ExternalFifoQueue<T>& queue) { | |
| 63 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (&queue != this) { |
| 64 | 2 | this->m_items = queue.m_items; | |
| 65 | 2 | this->m_enqueueIndex = queue.m_enqueueIndex; | |
| 66 | 2 | this->m_dequeueIndex = queue.m_dequeueIndex; | |
| 67 | 2 | this->m_size = queue.m_size; | |
| 68 | } | ||
| 69 | 2 | return *this; | |
| 70 | } | ||
| 71 | |||
| 72 | //! Clear the queue | ||
| 73 | 284 | void clear() override { | |
| 74 | 284 | this->m_enqueueIndex.setValue(0); | |
| 75 | 284 | this->m_dequeueIndex.setValue(0); | |
| 76 | 284 | this->m_size = 0; | |
| 77 | 284 | } | |
| 78 | |||
| 79 | //! Set the storage (typed data) | ||
| 80 | 32 | void setStorage(T* items, //!< The items | |
| 81 | FwSizeType capacity //!< The capacity | ||
| 82 | ) { | ||
| 83 | 32 | this->m_items.setStorage(items, capacity); | |
| 84 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (capacity > 0) { |
| 85 | 32 | this->m_enqueueIndex.setModulus(capacity); | |
| 86 | 32 | this->m_dequeueIndex.setModulus(capacity); | |
| 87 | } | ||
| 88 | 32 | this->clear(); | |
| 89 | 32 | } | |
| 90 | |||
| 91 | //! Set the storage (untyped data) | ||
| 92 | 1 | void setStorage(ByteArray data, //!< The data | |
| 93 | FwSizeType capacity //!< The capacity | ||
| 94 | ) { | ||
| 95 | 1 | this->m_items.setStorage(data, capacity); | |
| 96 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (capacity > 0) { |
| 97 | 1 | this->m_enqueueIndex.setModulus(capacity); | |
| 98 | 1 | this->m_dequeueIndex.setModulus(capacity); | |
| 99 | } | ||
| 100 | 1 | this->clear(); | |
| 101 | 1 | } | |
| 102 | |||
| 103 | //! Enqueue an element (push on the right) | ||
| 104 | //! \return SUCCESS if element enqueued | ||
| 105 | 11938 | Success enqueue(const T& e //!< The element (output) | |
| 106 | ) override { | ||
| 107 | 11938 | auto status = Success::FAILURE; | |
| 108 |
3/3✓ Branch 8 taken 11938 times.
✓ Branch 10 taken 11936 times.
✓ Branch 11 taken 2 times.
|
11938 | if (this->m_size < this->getCapacity()) { |
| 109 |
1/1✓ Branch 4 taken 11936 times.
|
11936 | const auto i = this->m_enqueueIndex.getValue(); |
| 110 |
1/1✓ Branch 4 taken 11936 times.
|
11936 | this->m_items[i] = e; |
| 111 |
1/1✓ Branch 4 taken 11936 times.
|
11936 | (void)this->m_enqueueIndex.increment(); |
| 112 | 11936 | this->m_size++; | |
| 113 | 11936 | status = Success::SUCCESS; | |
| 114 | } | ||
| 115 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 11938 times.
✓ Branch 3 taken 11938 times.
|
23876 | 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 | 13042 | const T& at(FwSizeType index //!< The index | |
| 123 | ) const override { | ||
| 124 | 13042 | FW_ASSERT(index < this->m_size, static_cast<FwAssertArgType>(index), | |
| 125 | static_cast<FwAssertArgType>(this->m_size)); | ||
| 126 |
1/1✓ Branch 7 taken 13042 times.
|
13042 | auto ci = this->m_dequeueIndex; |
| 127 |
1/1✓ Branch 1 taken 13042 times.
|
13042 | const auto i = ci.increment(index); |
| 128 |
1/1✓ Branch 6 taken 13042 times.
|
26084 | return this->m_items[i]; |
| 129 | } | ||
| 130 | |||
| 131 | //! Dequeue an element (remove from the left) | ||
| 132 | //! \return SUCCESS if element dequeued | ||
| 133 | 613 | Success dequeue(T& e //!< The element (output) | |
| 134 | ) override { | ||
| 135 | 613 | auto status = Success::FAILURE; | |
| 136 |
2/2✓ Branch 4 taken 247 times.
✓ Branch 5 taken 366 times.
|
613 | if (this->m_size > 0) { |
| 137 |
1/1✓ Branch 4 taken 247 times.
|
247 | e = this->at(0); |
| 138 |
1/1✓ Branch 4 taken 247 times.
|
247 | (void)this->m_dequeueIndex.increment(); |
| 139 | 247 | this->m_size--; | |
| 140 | 247 | status = Success::SUCCESS; | |
| 141 | } | ||
| 142 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 613 times.
✓ Branch 3 taken 613 times.
|
1226 | return status; |
| 143 | } | ||
| 144 | |||
| 145 | //! Get the size (number of items stored in the queue) | ||
| 146 | //! \return The size | ||
| 147 | 17154 | 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 | 15804 | 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 |