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