| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file FifoQueue.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief A FIFO queue with internal storage | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_FifoQueue_HPP | ||
| 8 | #define Fw_FifoQueue_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/Array.hpp" | ||
| 11 | #include "Fw/DataStructures/ExternalFifoQueue.hpp" | ||
| 12 | |||
| 13 | namespace Fw { | ||
| 14 | |||
| 15 | template <typename T, FwSizeType C> | ||
| 16 | class FifoQueue final : public FifoQueueBase<T> { | ||
| 17 | // ---------------------------------------------------------------------- | ||
| 18 | // Static assertions | ||
| 19 | // ---------------------------------------------------------------------- | ||
| 20 | |||
| 21 | static_assert(std::is_default_constructible<T>::value, "T must be default constructible"); | ||
| 22 | static_assert(C > 0, "capacity must be greater than zero"); | ||
| 23 | |||
| 24 | // ---------------------------------------------------------------------- | ||
| 25 | // Friend class for testing | ||
| 26 | // ---------------------------------------------------------------------- | ||
| 27 | |||
| 28 | template <typename TT, FwSizeType CC> | ||
| 29 | friend class FifoQueueTester; | ||
| 30 | |||
| 31 | public: | ||
| 32 | // ---------------------------------------------------------------------- | ||
| 33 | // Public constructors and destructors | ||
| 34 | // ---------------------------------------------------------------------- | ||
| 35 | |||
| 36 | //! Zero-argument constructor | ||
| 37 |
1/1✓ Branch 12 taken 16 times.
|
32 | FifoQueue() : FifoQueueBase<T>(), m_extQueue(m_items, C) {} |
| 38 | |||
| 39 | //! Copy constructor | ||
| 40 |
2/2✓ Branch 12 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | FifoQueue(const FifoQueue<T, C>& queue) : FifoQueueBase<T>(), m_extQueue(m_items, C) { *this = queue; } |
| 41 | |||
| 42 | //! Destructor | ||
| 43 | 34 | ~FifoQueue() override = default; | |
| 44 | |||
| 45 | public: | ||
| 46 | // ---------------------------------------------------------------------- | ||
| 47 | // Public member functions | ||
| 48 | // ---------------------------------------------------------------------- | ||
| 49 | |||
| 50 | //! operator= | ||
| 51 | 2 | FifoQueue<T, C>& operator=(const FifoQueue<T, C>& queue) { | |
| 52 | 2 | this->m_extQueue.copyDataFrom(queue); | |
| 53 | 2 | return *this; | |
| 54 | } | ||
| 55 | |||
| 56 | //! Clear the queue | ||
| 57 | 250 | void clear() override { this->m_extQueue.clear(); } | |
| 58 | |||
| 59 | //! Enqueue an item (push on the right) | ||
| 60 | //! \return SUCCESS if item enqueued | ||
| 61 | 11904 | Success enqueue(const T& e //!< The item (output) | |
| 62 | ) override { | ||
| 63 | 11904 | return this->m_extQueue.enqueue(e); | |
| 64 | } | ||
| 65 | |||
| 66 | //! Dequeue an item (remove from the left) | ||
| 67 | //! \return SUCCESS if item dequeued | ||
| 68 | 642 | Success dequeue(T& e //!< The item (output) | |
| 69 | ) override { | ||
| 70 | 642 | return this->m_extQueue.dequeue(e); | |
| 71 | } | ||
| 72 | |||
| 73 | //! Get the size (number of items stored in the queue) | ||
| 74 | //! \return The size | ||
| 75 | 17092 | FwSizeType getSize() const override { return this->m_extQueue.getSize(); } | |
| 76 | |||
| 77 | //! Get the capacity (maximum number of items stored in the queue) | ||
| 78 | //! \return The capacity | ||
| 79 | 3744 | FwSizeType getCapacity() const override { return this->m_extQueue.getCapacity(); } | |
| 80 | |||
| 81 | //! Get an item at an index. | ||
| 82 | //! Indices go from left to right in the queue. | ||
| 83 | //! Fails an assertion if the index is out of range. | ||
| 84 | //! \return The item | ||
| 85 | 6400 | const T& at(FwSizeType index //!< The index | |
| 86 | ) const override { | ||
| 87 | 6400 | return this->m_extQueue.at(index); | |
| 88 | } | ||
| 89 | |||
| 90 | private: | ||
| 91 | // ---------------------------------------------------------------------- | ||
| 92 | // Private member variables | ||
| 93 | // ---------------------------------------------------------------------- | ||
| 94 | |||
| 95 | //! The external queue implementation | ||
| 96 | ExternalFifoQueue<T> m_extQueue = {}; | ||
| 97 | |||
| 98 | //! The array providing the backing memory for m_extQueue | ||
| 99 | T m_items[C] = {}; | ||
| 100 | }; | ||
| 101 | |||
| 102 | } // namespace Fw | ||
| 103 | |||
| 104 | #endif | ||
| 105 |