F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Array.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \file Array.hpp
3 // \author bocchino
4 // \brief A statically-sized, bounds checked array
5 // ======================================================================
6 
7 #ifndef Fw_Array_HPP
8 #define Fw_Array_HPP
9 
10 #include <initializer_list>
11 
13 #include "Fw/FPrimeBasicTypes.hpp"
14 #include "Fw/Types/Assert.hpp"
15 
16 namespace Fw {
17 
18 template <typename T, FwSizeType S>
19 class Array final {
20  // ----------------------------------------------------------------------
21  // Static assertions
22  // ----------------------------------------------------------------------
23 
24  static_assert(std::is_default_constructible<T>::value, "T must be default constructible");
25  static_assert(S > 0, "array size must be greater than zero");
26 
27  public:
28  // ----------------------------------------------------------------------
29  // Types
30  // ----------------------------------------------------------------------
31 
33  using Elements = T[S];
34 
35  public:
36  // ----------------------------------------------------------------------
37  // Public constructors and destructors
38  // ----------------------------------------------------------------------
39 
41  Array() = default;
42 
44  Array(const std::initializer_list<T>& il
45  ) {
46  *this = il;
47  }
48 
50  Array(const Elements& elements
51  ) {
52  *this = elements;
53  }
54 
56  explicit Array(const T& element
57  ) {
58  *this = element;
59  }
60 
62  Array(const Array<T, S>& a
63  ) {
64  *this = a;
65  }
66 
68  ~Array() = default;
69 
70  public:
71  // ----------------------------------------------------------------------
72  // Public member functions
73  // ----------------------------------------------------------------------
74 
78  ) {
79  FW_ASSERT(i < S, static_cast<FwAssertArgType>(i));
80  return this->m_elements[i];
81  }
82 
85  const T& operator[](FwSizeType i
86  ) const {
87  FW_ASSERT(i < S, static_cast<FwAssertArgType>(i));
88  return this->m_elements[i];
89  }
90 
93  Array<T, S>& operator=(const std::initializer_list<T>& il
94  ) {
95  // Since we are required to use C++11, this has to be a runtime check
96  // In C++14, it can be a static check
97  FW_ASSERT(il.size() == S, static_cast<FwAssertArgType>(il.size()), static_cast<FwAssertArgType>(S));
98  FwSizeType i = 0;
99  for (const auto& e : il) {
100  FW_ASSERT(i < S, static_cast<FwAssertArgType>(i), static_cast<FwAssertArgType>(S));
101  this->m_elements[i] = e;
102  i++;
103  }
104  return *this;
105  }
106 
109  Array<T, S>& operator=(const Elements& elements
110  ) {
111  for (FwSizeType i = 0; i < S; i++) {
112  this->m_elements[i] = elements[i];
113  }
114  return *this;
115  }
116 
118  Array<T, S>& operator=(const T& element
119  ) {
120  for (FwSizeType i = 0; i < S; i++) {
121  this->m_elements[i] = element;
122  }
123  return *this;
124  }
125 
129  if (&a != this) {
130  for (FwSizeType i = 0; i < S; i++) {
131  this->m_elements[i] = a.m_elements[i];
132  }
133  }
134  return *this;
135  }
136 
139  Elements& getElements() { return this->m_elements; }
140 
143  const Elements& getElements() const { return this->m_elements; }
144 
146  // \return The ExternalArray
147  ExternalArray<T> asExternalArray() { return ExternalArray<T>(this->m_elements, S); }
148 
149  private:
150  // ----------------------------------------------------------------------
151  // Private member variables
152  // ----------------------------------------------------------------------
153 
155  Elements m_elements = {};
156 };
157 
158 } // namespace Fw
159 
160 #endif
PlatformSizeType FwSizeType
Array(const Array< T, S > &a)
Copy constructor.
Definition: Array.hpp:62
Array(const Elements &elements)
Primitive array constructor.
Definition: Array.hpp:50
Array< T, S > & operator=(const Elements &elements)
Definition: Array.hpp:109
Array< T, S > & operator=(const T &element)
operator= (single element)
Definition: Array.hpp:118
~Array()=default
Destructor.
T[S] Elements
The type of the elements array.
Definition: Array.hpp:33
Array(const T &element)
Single-element constructor.
Definition: Array.hpp:56
Array< T, S > & operator=(const std::initializer_list< T > &il)
Definition: Array.hpp:93
ExternalArray< T > asExternalArray()
Convert this array to an ExternalArray.
Definition: Array.hpp:147
Array(const std::initializer_list< T > &il)
Initializer list constructor.
Definition: Array.hpp:44
const Elements & getElements() const
Definition: Array.hpp:143
Array()=default
Zero-argument constructor.
Array< T, S > & operator=(const Array< T, S > &a)
Definition: Array.hpp:128
Elements & getElements()
Definition: Array.hpp:139
T & operator[](FwSizeType i)
Definition: Array.hpp:77
const T & operator[](FwSizeType i) const
Definition: Array.hpp:85
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.