F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ExternalFifoQueue.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \file ExternalFifoQueue.hpp
3 // \author bocchino
4 // \brief A FIFO queue with external storage
5 // ======================================================================
6 
7 #ifndef Fw_ExternalFifoQueue_HPP
8 #define Fw_ExternalFifoQueue_HPP
9 
13 #include "Fw/Types/ByteArray.hpp"
14 
15 namespace Fw {
16 
17 template <typename T>
18 class ExternalFifoQueue final : public FifoQueueBase<T> {
19  // ----------------------------------------------------------------------
20  // Friend class for testing
21  // ----------------------------------------------------------------------
22 
23  template <typename TT>
25 
26  public:
27  // ----------------------------------------------------------------------
28  // Public constructors and destructors
29  // ----------------------------------------------------------------------
30 
32  ExternalFifoQueue() = default;
33 
35  ExternalFifoQueue(T* items,
36  FwSizeType capacity
37  )
38  : FifoQueueBase<T>() {
39  this->setStorage(items, capacity);
40  }
41 
44  FwSizeType capacity
45  )
46  : FifoQueueBase<T>() {
47  this->setStorage(data, capacity);
48  }
49 
51  ExternalFifoQueue(const ExternalFifoQueue<T>& queue) : FifoQueueBase<T>() { *this = queue; }
52 
54  ~ExternalFifoQueue() override = default;
55 
56  public:
57  // ----------------------------------------------------------------------
58  // Public member functions
59  // ----------------------------------------------------------------------
60 
63  if (&queue != this) {
64  this->m_items = queue.m_items;
65  this->m_enqueueIndex = queue.m_enqueueIndex;
66  this->m_dequeueIndex = queue.m_dequeueIndex;
67  this->m_size = queue.m_size;
68  }
69  return *this;
70  }
71 
73  void clear() override {
74  this->m_enqueueIndex.setValue(0);
75  this->m_dequeueIndex.setValue(0);
76  this->m_size = 0;
77  }
78 
80  void setStorage(T* items,
81  FwSizeType capacity
82  ) {
83  this->m_items.setStorage(items, capacity);
84  if (capacity > 0) {
85  this->m_enqueueIndex.setModulus(capacity);
86  this->m_dequeueIndex.setModulus(capacity);
87  }
88  this->clear();
89  }
90 
92  void setStorage(ByteArray data,
93  FwSizeType capacity
94  ) {
95  this->m_items.setStorage(data, capacity);
96  if (capacity > 0) {
97  this->m_enqueueIndex.setModulus(capacity);
98  this->m_dequeueIndex.setModulus(capacity);
99  }
100  this->clear();
101  }
102 
105  Success enqueue(const T& e
106  ) override {
107  auto status = Success::FAILURE;
108  if (this->m_size < this->getCapacity()) {
109  const auto i = this->m_enqueueIndex.getValue();
110  this->m_items[i] = e;
111  (void)this->m_enqueueIndex.increment();
112  this->m_size++;
113  status = Success::SUCCESS;
114  }
115  return status;
116  }
117 
122  const T& at(FwSizeType index
123  ) const override {
124  FW_ASSERT(index < this->m_size, static_cast<FwAssertArgType>(index),
125  static_cast<FwAssertArgType>(this->m_size));
126  auto ci = this->m_dequeueIndex;
127  const auto i = ci.increment(index);
128  return this->m_items[i];
129  }
130 
134  ) override {
135  auto status = Success::FAILURE;
136  if (this->m_size > 0) {
137  e = this->at(0);
138  (void)this->m_dequeueIndex.increment();
139  this->m_size--;
140  status = Success::SUCCESS;
141  }
142  return status;
143  }
144 
147  FwSizeType getSize() const override { return this->m_size; }
148 
151  FwSizeType getCapacity() const override { return this->m_items.getSize(); }
152 
153  public:
154  // ----------------------------------------------------------------------
155  // Public static functions
156  // ----------------------------------------------------------------------
157 
161 
165  static constexpr FwSizeType getByteArraySize(FwSizeType capacity
166  ) {
167  return ExternalArray<T>::getByteArraySize(capacity);
168  }
169 
170  private:
171  // ----------------------------------------------------------------------
172  // Private member variables
173  // ----------------------------------------------------------------------
174 
176  ExternalArray<T> m_items = {};
177 
179  CircularIndex m_enqueueIndex = {};
180 
182  CircularIndex m_dequeueIndex = {};
183 
185  FwSizeType m_size = 0;
186 };
187 
188 } // namespace Fw
189 
190 #endif
Representing success.
PlatformSizeType FwSizeType
ExternalFifoQueue< T > & operator=(const ExternalFifoQueue< T > &queue)
operator=
FwSizeType getCapacity() const override
FwSizeType getValue() const
friend class ExternalFifoQueueTester
ExternalFifoQueue(T *items, FwSizeType capacity)
Constructor providing typed backing storage.
ExternalFifoQueue(const ExternalFifoQueue< T > &queue)
Copy constructor.
FwSizeType increment(FwSizeType amount=1)
ExternalFifoQueue()=default
Zero-argument constructor.
void setStorage(T *items, FwSizeType capacity)
Set the storage (typed data)
void setModulus(FwSizeType modulus)
Set the modulus.
static constexpr FwSizeType getByteArraySize(FwSizeType capacity)
~ExternalFifoQueue() override=default
Destructor.
FwSizeType getSize() const override
Success enqueue(const T &e) override
ExternalFifoQueue(ByteArray data, FwSizeType capacity)
Constructor providing untyped backing storage.
A variable-length byte array.
Definition: ByteArray.hpp:23
Representing failure.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
void setValue(FwSizeType value)
Set the index value.
Success dequeue(T &e) override
void clear() override
Clear the queue.
void setStorage(ByteArray data, FwSizeType capacity)
Set the storage (untyped data)
static constexpr U8 getByteArrayAlignment()
static constexpr FwSizeType getByteArraySize(FwSizeType size)
Implementation of malloc based allocator.
static constexpr U8 getByteArrayAlignment()
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
const T & at(FwSizeType index) const override