| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file Stack.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief A stack with internal storage | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_Stack_HPP | ||
| 8 | #define Fw_Stack_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/Array.hpp" | ||
| 11 | #include "Fw/DataStructures/ExternalStack.hpp" | ||
| 12 | |||
| 13 | namespace Fw { | ||
| 14 | |||
| 15 | template <typename T, FwSizeType C> | ||
| 16 | class Stack final : public StackBase<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 StackTester; | ||
| 30 | |||
| 31 | public: | ||
| 32 | // ---------------------------------------------------------------------- | ||
| 33 | // Public constructors and destructors | ||
| 34 | // ---------------------------------------------------------------------- | ||
| 35 | |||
| 36 | //! Zero-argument constructor | ||
| 37 |
1/1✓ Branch 12 taken 16 times.
|
32 | Stack() : StackBase<T>(), m_extStack(m_items, C) {} |
| 38 | |||
| 39 | //! Copy constructor | ||
| 40 |
2/2✓ Branch 12 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | Stack(const Stack<T, C>& stack) : StackBase<T>(), m_extStack(m_items, C) { *this = stack; } |
| 41 | |||
| 42 | //! Destructor | ||
| 43 | 34 | ~Stack() override = default; | |
| 44 | |||
| 45 | public: | ||
| 46 | // ---------------------------------------------------------------------- | ||
| 47 | // Public member functions | ||
| 48 | // ---------------------------------------------------------------------- | ||
| 49 | |||
| 50 | //! operator= | ||
| 51 | 2 | Stack<T, C>& operator=(const Stack<T, C>& stack) { | |
| 52 | 2 | this->m_extStack.copyDataFrom(stack); | |
| 53 | 2 | return *this; | |
| 54 | } | ||
| 55 | |||
| 56 | //! Clear the stack | ||
| 57 | 244 | void clear() override { this->m_extStack.clear(); } | |
| 58 | |||
| 59 | //! Push an item (push on the right) | ||
| 60 | //! \return SUCCESS if item pushed | ||
| 61 | 11892 | Success push(const T& e //!< The item (output) | |
| 62 | ) override { | ||
| 63 | 11892 | return this->m_extStack.push(e); | |
| 64 | } | ||
| 65 | |||
| 66 | //! Pop an item (remove from the right) | ||
| 67 | //! \return SUCCESS if item popped | ||
| 68 | 632 | Success pop(T& e //!< The item (output) | |
| 69 | ) override { | ||
| 70 | 632 | return this->m_extStack.pop(e); | |
| 71 | } | ||
| 72 | |||
| 73 | //! Get the size (number of items stored in the stack) | ||
| 74 | //! \return The size | ||
| 75 | 13396 | FwSizeType getSize() const override { return this->m_extStack.getSize(); } | |
| 76 | |||
| 77 | //! Get the capacity (maximum number of items stored in the stack) | ||
| 78 | //! \return The capacity | ||
| 79 | 3738 | FwSizeType getCapacity() const override { return this->m_extStack.getCapacity(); } | |
| 80 | |||
| 81 | //! Get an item at an index. | ||
| 82 | //! Index 0 is the rightmost (latest) element in the stack. | ||
| 83 | //! Increasing indices go from right to left. | ||
| 84 | //! Fails an assertion if the index is out of range. | ||
| 85 | //! \return The item | ||
| 86 | 7437 | const T& at(FwSizeType index //!< The index | |
| 87 | ) const override { | ||
| 88 | 7437 | return this->m_extStack.at(index); | |
| 89 | } | ||
| 90 | |||
| 91 | private: | ||
| 92 | // ---------------------------------------------------------------------- | ||
| 93 | // Private member variables | ||
| 94 | // ---------------------------------------------------------------------- | ||
| 95 | |||
| 96 | //! The external stack implementation | ||
| 97 | ExternalStack<T> m_extStack = {}; | ||
| 98 | |||
| 99 | //! The array providing the backing memory for m_extStack | ||
| 100 | T m_items[C] = {}; | ||
| 101 | }; | ||
| 102 | |||
| 103 | } // namespace Fw | ||
| 104 | |||
| 105 | #endif | ||
| 106 |