F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CircularIndex.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \file CircularIndex.hpp
3 // \author bocchino
4 // \brief An index value that wraps around modulo an integer
5 // ======================================================================
6 
7 #ifndef Fw_CircularIndex_HPP
8 #define Fw_CircularIndex_HPP
9 
10 #include "Fw/FPrimeBasicTypes.hpp"
11 #include "Fw/Types/Assert.hpp"
12 
13 namespace Fw {
14 
15 class CircularIndex final {
16  public:
17  // ----------------------------------------------------------------------
18  // Public constructors and destructors
19  // ----------------------------------------------------------------------
20 
22  CircularIndex() : m_value(0), m_modulus(1) {}
23 
25  explicit CircularIndex(FwSizeType modulus,
26  FwSizeType value = 0
27  )
28  : m_modulus(modulus) {
29  FW_ASSERT(modulus > 0);
30  this->setValue(value);
31  }
32 
34  CircularIndex(const CircularIndex& ci) { *this = ci; }
35 
37  ~CircularIndex() = default;
38 
39  public:
40  // ----------------------------------------------------------------------
41  // Public functions
42  // ----------------------------------------------------------------------
43 
46  if (this != &ci) {
47  this->m_value = ci.m_value;
48  this->m_modulus = ci.m_modulus;
49  }
50  return *this;
51  }
52 
55  FwSizeType getValue() const {
56  FW_ASSERT(this->m_value < this->m_modulus);
57  return this->m_value;
58  }
59 
61  void setValue(FwSizeType value
62  ) {
63  FW_ASSERT(this->m_modulus > 0);
64  this->m_value = value % this->m_modulus;
65  }
66 
69  FW_ASSERT(this->m_value < this->m_modulus);
70  return this->m_modulus;
71  }
72 
74  void setModulus(FwSizeType modulus
75  ) {
76  this->m_modulus = modulus;
77  this->setValue(this->m_value);
78  }
79 
83  ) {
84  FW_ASSERT(this->m_modulus > 0);
85  const FwSizeType offset = amount % m_modulus;
86  this->setValue(this->m_value + offset);
87  return this->m_value;
88  }
89 
93  ) {
94  FW_ASSERT(this->m_modulus > 0);
95  const FwSizeType offset = amount % this->m_modulus;
96  this->setValue(this->m_value + this->m_modulus - offset);
97  return this->m_value;
98  }
99 
100  private:
101  // ----------------------------------------------------------------------
102  // Private member variables
103  // ----------------------------------------------------------------------
104 
106  FwSizeType m_value;
107 
109  FwSizeType m_modulus;
110 };
111 
112 } // namespace Fw
113 
114 #endif
PlatformSizeType FwSizeType
FwSizeType getValue() const
CircularIndex()
Zero-argument constructor.
CircularIndex(FwSizeType modulus, FwSizeType value=0)
Constructor with specified members.
FwSizeType getModulus() const
Get the modulus.
CircularIndex & operator=(const CircularIndex &ci)
operator=
FwSizeType increment(FwSizeType amount=1)
void setModulus(FwSizeType modulus)
Set the modulus.
CircularIndex(const CircularIndex &ci)
Copy constructor.
~CircularIndex()=default
Destructor.
void setValue(FwSizeType value)
Set the index value.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
FwSizeType decrement(FwSizeType amount=1)