F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ExternalStack.hpp
Go to the documentation of this file.
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 
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 
31  ExternalStack() = default;
32 
34  ExternalStack(T* items,
35  FwSizeType capacity
36  )
37  : StackBase<T>() {
38  this->setStorage(items, capacity);
39  }
40 
43  FwSizeType capacity
44  )
45  : StackBase<T>() {
46  this->setStorage(data, capacity);
47  }
48 
50  ExternalStack(const ExternalStack<T>& stack) : StackBase<T>() { *this = stack; }
51 
53  ~ExternalStack() override = default;
54 
55  public:
56  // ----------------------------------------------------------------------
57  // Public member functions
58  // ----------------------------------------------------------------------
59 
62  if (&stack != this) {
63  this->m_items = stack.m_items;
64  this->m_size = stack.m_size;
65  }
66  return *this;
67  }
68 
70  void clear() override { this->m_size = 0; }
71 
73  void setStorage(T* items,
74  FwSizeType capacity
75  ) {
76  this->m_items.setStorage(items, capacity);
77  this->clear();
78  }
79 
81  void setStorage(ByteArray data,
82  FwSizeType capacity
83  ) {
84  this->m_items.setStorage(data, capacity);
85  this->clear();
86  }
87 
90  Success push(const T& e
91  ) override {
92  auto status = Success::FAILURE;
93  if (this->m_size < this->getCapacity()) {
94  this->m_items[this->m_size] = e;
95  this->m_size++;
96  status = Success::SUCCESS;
97  }
98  return status;
99  }
100 
106  const T& at(FwSizeType index
107  ) const override {
108  FW_ASSERT(index < this->m_size, static_cast<FwAssertArgType>(index),
109  static_cast<FwAssertArgType>(this->m_size));
110  return this->m_items[this->m_size - 1 - index];
111  }
112 
115  Success pop(T& e
116  ) override {
117  auto status = Success::FAILURE;
118  if (this->m_size > 0) {
119  e = this->at(0);
120  this->m_size--;
121  status = Success::SUCCESS;
122  }
123  return status;
124  }
125 
128  FwSizeType getSize() const override { return this->m_size; }
129 
132  FwSizeType getCapacity() const override { return this->m_items.getSize(); }
133 
134  public:
135  // ----------------------------------------------------------------------
136  // Public static functions
137  // ----------------------------------------------------------------------
138 
142 
146  static constexpr FwSizeType getByteArraySize(FwSizeType capacity
147  ) {
148  return ExternalArray<T>::getByteArraySize(capacity);
149  }
150 
151  private:
152  // ----------------------------------------------------------------------
153  // Private member variables
154  // ----------------------------------------------------------------------
155 
157  ExternalArray<T> m_items = {};
158 
160  FwSizeType m_size = 0;
161 };
162 
163 } // namespace Fw
164 
165 #endif
void setStorage(ByteArray data, FwSizeType capacity)
Set the storage (untyped data)
Success push(const T &e) override
Representing success.
PlatformSizeType FwSizeType
void clear() override
Clear the stack.
ExternalStack(ByteArray data, FwSizeType capacity)
Constructor providing untyped backing storage.
static constexpr FwSizeType getByteArraySize(FwSizeType capacity)
ExternalStack(T *items, FwSizeType capacity)
Constructor providing typed backing storage.
FwSizeType getSize() const override
void setStorage(T *items, FwSizeType capacity)
Set the storage (typed data)
~ExternalStack() override=default
Destructor.
ExternalStack< T > & operator=(const ExternalStack< T > &stack)
operator=
friend class ExternalStackTester
ExternalStack(const ExternalStack< T > &stack)
Copy constructor.
A variable-length byte array.
Definition: ByteArray.hpp:23
Representing failure.
const T & at(FwSizeType index) const override
static constexpr U8 getByteArrayAlignment()
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
ExternalStack()=default
Zero-argument constructor.
FwSizeType getCapacity() const override
static constexpr U8 getByteArrayAlignment()
static constexpr FwSizeType getByteArraySize(FwSizeType size)
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
Success pop(T &e) override