| 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 | 17 | Array() = default; | |
| 42 | |||
| 43 | //! Initializer list constructor | ||
| 44 | 4 | Array(const std::initializer_list<T>& il //!< The initializer list | |
| 45 | 4 | ) { | |
| 46 | 4 | *this = il; | |
| 47 | 4 | } | |
| 48 | |||
| 49 | //! Primitive array constructor | ||
| 50 | 2 | Array(const Elements& elements //!< The array elements | |
| 51 | 2 | ) { | |
| 52 | 2 | *this = elements; | |
| 53 | 2 | } | |
| 54 | |||
| 55 | //! Single-element constructor | ||
| 56 | 9 | explicit Array(const T& element //!< The element | |
| 57 | 9 | ) { | |
| 58 | 9 | *this = element; | |
| 59 | 9 | } | |
| 60 | |||
| 61 | //! Copy constructor | ||
| 62 | 1 | Array(const Array<T, S>& a //!< The array to copy | |
| 63 | 1 | ) { | |
| 64 | 1 | *this = a; | |
| 65 | 1 | } | |
| 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 | 5662 | T& operator[](FwSizeType i //!< The subscript index | |
| 78 | ) { | ||
| 79 | 5662 | FW_ASSERT(i < S, static_cast<FwAssertArgType>(i)); | |
| 80 | 5662 | return this->m_elements[i]; | |
| 81 | } | ||
| 82 | |||
| 83 | //! Const subscript operator | ||
| 84 | //! \return The element at index i | ||
| 85 | 130 | const T& operator[](FwSizeType i //!< The subscript index | |
| 86 | ) const { | ||
| 87 | 130 | FW_ASSERT(i < S, static_cast<FwAssertArgType>(i)); | |
| 88 | 130 | return this->m_elements[i]; | |
| 89 | } | ||
| 90 | |||
| 91 | //! operator= (initializer list) | ||
| 92 | //! \return *this | ||
| 93 | 4 | 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 | 4 | FW_ASSERT(il.size() == S, static_cast<FwAssertArgType>(il.size()), static_cast<FwAssertArgType>(S)); | |
| 98 | 4 | FwSizeType i = 0; | |
| 99 |
2/2✓ Branch 5 taken 12 times.
✓ Branch 6 taken 4 times.
|
16 | for (const auto& e : il) { |
| 100 | 12 | FW_ASSERT(i < S, static_cast<FwAssertArgType>(i), static_cast<FwAssertArgType>(S)); | |
| 101 | 12 | this->m_elements[i] = e; | |
| 102 | 12 | i++; | |
| 103 | } | ||
| 104 | 4 | return *this; | |
| 105 | } | ||
| 106 | |||
| 107 | //! operator= (primitive array) | ||
| 108 | //! \return *this | ||
| 109 | 2 | Array<T, S>& operator=(const Elements& elements //!< The array elements | |
| 110 | ) { | ||
| 111 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | for (FwSizeType i = 0; i < S; i++) { |
| 112 | 6 | this->m_elements[i] = elements[i]; | |
| 113 | } | ||
| 114 | 2 | return *this; | |
| 115 | } | ||
| 116 | |||
| 117 | //! operator= (single element) | ||
| 118 | 9 | Array<T, S>& operator=(const T& element //!< The element | |
| 119 | ) { | ||
| 120 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 9 times.
|
64 | for (FwSizeType i = 0; i < S; i++) { |
| 121 | 55 | this->m_elements[i] = element; | |
| 122 | } | ||
| 123 | 9 | return *this; | |
| 124 | } | ||
| 125 | |||
| 126 | //! operator= (copy assignment) | ||
| 127 | //! \return *this | ||
| 128 | 2 | Array<T, S>& operator=(const Array<T, S>& a) { | |
| 129 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (&a != this) { |
| 130 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | for (FwSizeType i = 0; i < S; i++) { |
| 131 | 6 | this->m_elements[i] = a.m_elements[i]; | |
| 132 | } | ||
| 133 | } | ||
| 134 | 2 | return *this; | |
| 135 | } | ||
| 136 | |||
| 137 | //! Get a mutable reference to the elements | ||
| 138 | //! \return A mutable reference to the elements | ||
| 139 | 11 | 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 | 1 | 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 |