GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/StackBase.hpp
Date: 2026-09-03 22:12:29
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 // ======================================================================
2 // \title StackBase
3 // \author bocchino
4 // \brief An abstract base class template for a stack
5 // ======================================================================
6
7 #ifndef Fw_StackBase_HPP
8 #define Fw_StackBase_HPP
9
10 #include "Fw/DataStructures/SizedContainer.hpp"
11 #include "Fw/Types/Assert.hpp"
12 #include "Fw/Types/SuccessEnumAc.hpp"
13
14 namespace Fw {
15
16 template <typename T>
17 class StackBase : public SizedContainer {
18 private:
19 // ----------------------------------------------------------------------
20 // Private constructors
21 // ----------------------------------------------------------------------
22
23 //! Copy constructor deleted in the base class
24 //! Behavior depends on the implementation
25 StackBase(const StackBase<T>&) = delete;
26
27 protected:
28 // ----------------------------------------------------------------------
29 // Protected constructors and destructors
30 // ----------------------------------------------------------------------
31
32 //! Zero-argument constructor
33 2 StackBase() : SizedContainer() {}
34
35 //! Destructor
36 4 virtual ~StackBase() = default;
37
38 private:
39 // ----------------------------------------------------------------------
40 // Private member functions
41 // ----------------------------------------------------------------------
42
43 //! operator= deleted in the base class
44 //! Behavior depends on the implementation
45 //! We avoid virtual user-defined operators
46 StackBase<T>& operator=(const StackBase<T>&) = delete;
47
48 public:
49 // ----------------------------------------------------------------------
50 // Public member functions
51 // ----------------------------------------------------------------------
52
53 //! Get an item at an index.
54 //! Index 0 is the rightmost (latest) element in the stack.
55 //! Increasing indices go from right to left.
56 //! Fails an assertion if the index is out of range.
57 //! \return The item
58 virtual const T& at(FwSizeType index //!< The index
59 ) const = 0;
60
61 //! Copy data from another stack
62 void copyDataFrom(const StackBase<T>& stack //!< The stack
63 ) {
64 if (&stack != this) {
65 this->clear();
66 const FwSizeType size = FW_MIN(stack.getSize(), this->getCapacity());
67 for (FwSizeType i = 0; i < size; i++) {
68 const auto& e = stack.at(size - 1 - i);
69 const auto status = this->push(e);
70 FW_ASSERT(status == Fw::Success::SUCCESS, static_cast<FwAssertArgType>(status));
71 }
72 }
73 }
74
75 //! Push an item (add to the right)
76 //! \return SUCCESS if item pushed
77 virtual Success push(const T& e //!< The item (output)
78 ) = 0;
79
80 //! Peek an item at an index
81 //! Indices go from left to right in the range [0, size)
82 //! \return SUCCESS if item exists
83 Success peek(T& e, //!< The item (output)
84 FwSizeType index = 0 //!< The index (input)
85 ) const {
86 auto status = Success::FAILURE;
87 if (index < this->getSize()) {
88 e = this->at(index);
89 status = Success::SUCCESS;
90 }
91 return status;
92 }
93
94 //! Pop an item (remove from the right)
95 //! \return SUCCESS if item popped
96 virtual Success pop(T& e //!< The item (output)
97 ) = 0;
98 };
99
100 } // namespace Fw
101
102 #endif
103