GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/Array.hpp
Date: 2026-09-03 22:12:29
Exec Total Coverage
Lines: 11 15 73.3%
Functions: 3 6 50.0%
Branches: 2 2 100.0%

Line Branch Exec Source
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
12 #include "Fw/DataStructures/ExternalArray.hpp"
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
32 //! The type of the elements array
33 using Elements = T[S];
34
35 public:
36 // ----------------------------------------------------------------------
37 // Public constructors and destructors
38 // ----------------------------------------------------------------------
39
40 //! Zero-argument constructor
41 Array() = default;
42
43 //! Initializer list constructor
44 Array(const std::initializer_list<T>& il //!< The initializer list
45 ) {
46 *this = il;
47 }
48
49 //! Primitive array constructor
50 Array(const Elements& elements //!< The array elements
51 ) {
52 *this = elements;
53 }
54
55 //! Single-element constructor
56 3 explicit Array(const T& element //!< The element
57 3 ) {
58 3 *this = element;
59 3 }
60
61 //! Copy constructor
62 Array(const Array<T, S>& a //!< The array to copy
63 ) {
64 *this = a;
65 }
66
67 //! Destructor
68 ~Array() = default;
69
70 public:
71 // ----------------------------------------------------------------------
72 // Public member functions
73 // ----------------------------------------------------------------------
74
75 //! Subscript operator
76 //! \return The element at index i
77 T& operator[](FwSizeType i //!< The subscript index
78 ) {
79 FW_ASSERT(i < S, static_cast<FwAssertArgType>(i));
80 return this->m_elements[i];
81 }
82
83 //! Const subscript operator
84 //! \return The element at index i
85 30 const T& operator[](FwSizeType i //!< The subscript index
86 ) const {
87 30 FW_ASSERT(i < S, static_cast<FwAssertArgType>(i));
88 30 return this->m_elements[i];
89 }
90
91 //! operator= (initializer list)
92 //! \return *this
93 Array<T, S>& operator=(const std::initializer_list<T>& il //!< The initializer list
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
107 //! operator= (primitive array)
108 //! \return *this
109 Array<T, S>& operator=(const Elements& elements //!< The array elements
110 ) {
111 for (FwSizeType i = 0; i < S; i++) {
112 this->m_elements[i] = elements[i];
113 }
114 return *this;
115 }
116
117 //! operator= (single element)
118 3 Array<T, S>& operator=(const T& element //!< The element
119 ) {
120
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3 times.
33 for (FwSizeType i = 0; i < S; i++) {
121 30 this->m_elements[i] = element;
122 }
123 3 return *this;
124 }
125
126 //! operator= (copy assignment)
127 //! \return *this
128 Array<T, S>& operator=(const Array<T, S>& a) {
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
137 //! Get a mutable reference to the elements
138 //! \return A mutable reference to the elements
139 Elements& getElements() { return this->m_elements; }
140
141 //! Get a const reference to the elements
142 //! \return A const reference to the elements
143 const Elements& getElements() const { return this->m_elements; }
144
145 //! Convert this array to an ExternalArray
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
154 //! The array elements
155 Elements m_elements = {};
156 };
157
158 } // namespace Fw
159
160 #endif
161