F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
ArrayFIFOBuffer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title ArrayFIFOBuffer.cpp
3 // \author mereweth
4 // \brief ArrayFIFOBuffer implementation
5 //
6 // \copyright
7 // Copyright (C) 2017 California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include "Fw/Types/Assert.hpp"
14 #include "Fw/Types/BasicTypes.hpp"
16 #include <new> // For placement new
17 
18 namespace Svc {
19 
20 // ----------------------------------------------------------------------
21 // Constructors
22 // ----------------------------------------------------------------------
23 
24 BufferAccumulator::ArrayFIFOBuffer ::ArrayFIFOBuffer()
25  : m_elements(nullptr),
26  m_capacity(0),
27  m_enqueueIndex(0),
28  m_dequeueIndex(0),
29  m_size(0)
30 {
31 }
32 
33 BufferAccumulator::ArrayFIFOBuffer ::~ArrayFIFOBuffer() {}
34 
35 // ----------------------------------------------------------------------
36 // Public functions
37 // ----------------------------------------------------------------------
38 
39 void BufferAccumulator::ArrayFIFOBuffer ::init(Fw::Buffer* const elements,
40  NATIVE_UINT_TYPE capacity) {
41  this->m_elements = elements;
42  this->m_capacity = capacity;
43 
44  // Construct all elements
45  for (NATIVE_UINT_TYPE idx = 0; idx < capacity; idx++) {
46  new (&this->m_elements[idx]) Fw::Buffer;
47  }
48 }
49 
50 bool BufferAccumulator::ArrayFIFOBuffer ::enqueue(const Fw::Buffer& e) {
51 
52  if (this->m_elements == nullptr) {
53  return false;
54  }
55 
56  bool status;
57  if (this->m_size < this->m_capacity) {
58  // enqueueIndex is unsigned, no need to compare with 0
59  FW_ASSERT(m_enqueueIndex < this->m_capacity, m_enqueueIndex);
60  this->m_elements[this->m_enqueueIndex] = e;
61  this->m_enqueueIndex = (this->m_enqueueIndex + 1) % this->m_capacity;
62  status = true;
63  this->m_size++;
64  } else {
65  status = false;
66  }
67 
68  return status;
69 }
70 
71 bool BufferAccumulator::ArrayFIFOBuffer ::dequeue(Fw::Buffer& e) {
72 
73  if (this->m_elements == nullptr) {
74  return false;
75  }
76 
77  FW_ASSERT(this->m_elements);
78  bool status;
79 
80  if (this->m_size > 0) {
81  // dequeueIndex is unsigned, no need to compare with 0
82  FW_ASSERT(m_dequeueIndex < this->m_capacity, m_dequeueIndex);
83  e = this->m_elements[this->m_dequeueIndex];
84  this->m_dequeueIndex = (this->m_dequeueIndex + 1) % this->m_capacity;
85  this->m_size--;
86  status = true;
87  } else {
88  status = false;
89  }
90 
91  return status;
92 }
93 
94 U32 BufferAccumulator::ArrayFIFOBuffer ::getSize() const {
95  return this->m_size;
96 }
97 
98 U32 BufferAccumulator::ArrayFIFOBuffer ::getCapacity() const {
99  return this->m_capacity;
100 }
101 
102 } // namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
C++ header for working with basic fprime types.