F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
StackBase.hpp
Go to the documentation of this file.
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 
11 #include "Fw/Types/Assert.hpp"
13 
14 namespace Fw {
15 
16 template <typename T>
17 class StackBase : public SizedContainer {
18  private:
19  // ----------------------------------------------------------------------
20  // Private constructors
21  // ----------------------------------------------------------------------
22 
25  StackBase(const StackBase<T>&) = delete;
26 
27  protected:
28  // ----------------------------------------------------------------------
29  // Protected constructors and destructors
30  // ----------------------------------------------------------------------
31 
34 
36  virtual ~StackBase() = default;
37 
38  private:
39  // ----------------------------------------------------------------------
40  // Private member functions
41  // ----------------------------------------------------------------------
42 
46  StackBase<T>& operator=(const StackBase<T>&) = delete;
47 
48  public:
49  // ----------------------------------------------------------------------
50  // Public member functions
51  // ----------------------------------------------------------------------
52 
58  virtual const T& at(FwSizeType index
59  ) const = 0;
60 
62  void copyDataFrom(const StackBase<T>& 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 
77  virtual Success push(const T& e
78  ) = 0;
79 
83  Success peek(T& e,
84  FwSizeType index = 0
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 
96  virtual Success pop(T& e
97  ) = 0;
98 };
99 
100 } // namespace Fw
101 
102 #endif
Representing success.
PlatformSizeType FwSizeType
virtual FwSizeType getSize() const =0
void copyDataFrom(const StackBase< T > &stack)
Copy data from another stack.
Definition: StackBase.hpp:62
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:92
Success peek(T &e, FwSizeType index=0) const
Definition: StackBase.hpp:83
virtual FwSizeType getCapacity() const =0
Representing failure.
virtual Success push(const T &e)=0
virtual ~StackBase()=default
Destructor.
virtual Success pop(T &e)=0
virtual const T & at(FwSizeType index) const =0
virtual void clear()=0
Clear the container.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
StackBase()
Zero-argument constructor.
Definition: StackBase.hpp:33