GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/ExternalStack.hpp
Date: 2026-09-03 22:12:29
Exec Total Coverage
Lines: 26 26 100.0%
Functions: 9 9 100.0%
Branches: 7 9 77.8%

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 2 ExternalStack() = default;
32
33 //! Constructor providing typed backing storage
34 ExternalStack(T* items, //!< The items
35 FwSizeType capacity //!< The capacity
36 )
37 : StackBase<T>() {
38 this->setStorage(items, capacity);
39 }
40
41 //! Constructor providing untyped backing storage
42 ExternalStack(ByteArray data, //!< The data
43 FwSizeType capacity //!< The capacity
44 )
45 : StackBase<T>() {
46 this->setStorage(data, capacity);
47 }
48
49 //! Copy constructor
50 ExternalStack(const ExternalStack<T>& stack) : StackBase<T>() { *this = stack; }
51
52 //! Destructor
53 4 ~ExternalStack() override = default;
54
55 public:
56 // ----------------------------------------------------------------------
57 // Public member functions
58 // ----------------------------------------------------------------------
59
60 //! operator=
61 ExternalStack<T>& operator=(const ExternalStack<T>& stack) {
62 if (&stack != this) {
63 this->m_items = stack.m_items;
64 this->m_size = stack.m_size;
65 }
66 return *this;
67 }
68
69 //! Clear the stack
70 5 void clear() override { this->m_size = 0; }
71
72 //! Set the storage (typed data)
73 2 void setStorage(T* items, //!< The items
74 FwSizeType capacity //!< The capacity
75 ) {
76 2 this->m_items.setStorage(items, capacity);
77 2 this->clear();
78 2 }
79
80 //! Set the storage (untyped data)
81 void setStorage(ByteArray data, //!< The data
82 FwSizeType capacity //!< The capacity
83 ) {
84 this->m_items.setStorage(data, capacity);
85 this->clear();
86 }
87
88 //! Push an element (push on the right)
89 //! \return SUCCESS if element pushed
90 404 Success push(const T& e //!< The element (output)
91 ) override {
92 404 auto status = Success::FAILURE;
93
2/3
✓ Branch 1 taken 404 times.
✓ Branch 3 taken 404 times.
✗ Branch 4 not taken.
404 if (this->m_size < this->getCapacity()) {
94
1/1
✓ Branch 1 taken 404 times.
404 this->m_items[this->m_size] = e;
95 404 this->m_size++;
96 404 status = Success::SUCCESS;
97 }
98
1/1
✓ Branch 1 taken 404 times.
808 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 104 const T& at(FwSizeType index //!< The index
107 ) const override {
108 104 FW_ASSERT(index < this->m_size, static_cast<FwAssertArgType>(index),
109 static_cast<FwAssertArgType>(this->m_size));
110 104 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 104 Success pop(T& e //!< The element (output)
116 ) override {
117 104 auto status = Success::FAILURE;
118
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 if (this->m_size > 0) {
119
1/1
✓ Branch 1 taken 104 times.
104 e = this->at(0);
120 104 this->m_size--;
121 104 status = Success::SUCCESS;
122 }
123
1/1
✓ Branch 1 taken 104 times.
208 return status;
124 }
125
126 //! Get the size (number of items stored in the stack)
127 //! \return The size
128 104 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 404 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 static constexpr FwSizeType getByteArraySize(FwSizeType capacity //!< The capacity
147 ) {
148 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