GCC Code Coverage Report


Directory: Fw/DataStructures/
File: ExternalStack.hpp
Date: 2026-09-03 21:14:50
Exec Total Coverage
Lines: 46 46 100.0%
Functions: 26 26 100.0%
Branches: 15 18 83.3%

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 103 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 274 ~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 952 void clear() override { this->m_size = 0; }
71
72 //! Set the storage (typed data)
73 120 void setStorage(T* items, //!< The items
74 FwSizeType capacity //!< The capacity
75 ) {
76 120 this->m_items.setStorage(items, capacity);
77 120 this->clear();
78 120 }
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 558362 Success push(const T& e //!< The element (output)
91 ) override {
92 558362 auto status = Success::FAILURE;
93
3/3
✓ Branch 8 taken 558362 times.
✓ Branch 10 taken 558360 times.
✓ Branch 11 taken 2 times.
558362 if (this->m_size < this->getCapacity()) {
94
1/1
✓ Branch 8 taken 558360 times.
558360 this->m_items[this->m_size] = e;
95 558360 this->m_size++;
96 558360 status = Success::SUCCESS;
97 }
98
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 558362 times.
✓ Branch 3 taken 558362 times.
1116724 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 30890 const T& at(FwSizeType index //!< The index
107 ) const override {
108 30890 FW_ASSERT(index < this->m_size, static_cast<FwAssertArgType>(index),
109 static_cast<FwAssertArgType>(this->m_size));
110 30890 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 16375 Success pop(T& e //!< The element (output)
116 ) override {
117 16375 auto status = Success::FAILURE;
118
2/2
✓ Branch 4 taken 16012 times.
✓ Branch 5 taken 363 times.
16375 if (this->m_size > 0) {
119
1/1
✓ Branch 4 taken 16012 times.
16012 e = this->at(0);
120 16012 this->m_size--;
121 16012 status = Success::SUCCESS;
122 }
123
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 16375 times.
✓ Branch 3 taken 16375 times.
32750 return status;
124 }
125
126 //! Get the size (number of items stored in the stack)
127 //! \return The size
128 49769 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 562170 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