| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file ExternalArray.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief A bounds-checked array with external memory | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_ExternalArray_HPP | ||
| 8 | #define Fw_ExternalArray_HPP | ||
| 9 | |||
| 10 | #include <cstdint> | ||
| 11 | #include <new> | ||
| 12 | #include <type_traits> | ||
| 13 | |||
| 14 | #include "Fw/FPrimeBasicTypes.hpp" | ||
| 15 | #include "Fw/Types/Assert.hpp" | ||
| 16 | #include "Fw/Types/ByteArray.hpp" | ||
| 17 | |||
| 18 | namespace Fw { | ||
| 19 | |||
| 20 | template <typename T> | ||
| 21 | class ExternalArray final { | ||
| 22 | // ---------------------------------------------------------------------- | ||
| 23 | // Static assertions | ||
| 24 | // ---------------------------------------------------------------------- | ||
| 25 | |||
| 26 | static_assert(std::is_assignable<T&, T>::value, "T must be assignable to T&"); | ||
| 27 | |||
| 28 | public: | ||
| 29 | // ---------------------------------------------------------------------- | ||
| 30 | // Public constructors and destructors | ||
| 31 | // ---------------------------------------------------------------------- | ||
| 32 | |||
| 33 | //! Zero-argument constructor | ||
| 34 | 14 | ExternalArray() {} | |
| 35 | |||
| 36 | //! Constructor providing typed backing storage. | ||
| 37 | //! elements must point to at least size elements of type T. | ||
| 38 | 1 | ExternalArray(T* elements, //!< The elements | |
| 39 | FwSizeType size //!< The array size | ||
| 40 | ) | ||
| 41 | 1 | : m_elements(elements), m_size(size) {} | |
| 42 | |||
| 43 | //! Constructor providing untyped backing storage. | ||
| 44 | //! data must be aligned according to getByteArrayAlignment(). | ||
| 45 | //! data must contain at least getByteArraySize(size) bytes. | ||
| 46 | ExternalArray(ByteArray data, //!< The data | ||
| 47 | FwSizeType size //!< The array size | ||
| 48 | ) { | ||
| 49 | this->setStorage(data, size); | ||
| 50 | } | ||
| 51 | |||
| 52 | //! Copy constructor | ||
| 53 | ExternalArray(const ExternalArray<T>& a) : m_elements(a.m_elements), m_size(a.m_size) {} | ||
| 54 | |||
| 55 | //! Destructor | ||
| 56 | 17 | ~ExternalArray() { this->releaseStorage(); } | |
| 57 | |||
| 58 | public: | ||
| 59 | // ---------------------------------------------------------------------- | ||
| 60 | // Public member functions | ||
| 61 | // ---------------------------------------------------------------------- | ||
| 62 | |||
| 63 | //! Subscript operator | ||
| 64 | //! \return The element at index i | ||
| 65 | 10064 | T& operator[](const FwSizeType i //!< The subscript index | |
| 66 | ) { | ||
| 67 | 10064 | FW_ASSERT(this->m_elements != nullptr); | |
| 68 | 10064 | FW_ASSERT(i < this->m_size, static_cast<FwAssertArgType>(i)); | |
| 69 | 10064 | return this->m_elements[i]; | |
| 70 | } | ||
| 71 | |||
| 72 | //! Const subscript operator | ||
| 73 | //! \return The element at index i | ||
| 74 | 14900 | const T& operator[](const FwSizeType i //!< The subscript index | |
| 75 | ) const { | ||
| 76 | 14900 | FW_ASSERT(this->m_elements != nullptr); | |
| 77 | 14900 | FW_ASSERT(i < this->m_size, static_cast<FwAssertArgType>(i)); | |
| 78 | 14900 | return this->m_elements[i]; | |
| 79 | } | ||
| 80 | |||
| 81 | //! Copy assignment operator | ||
| 82 | //! \return *this | ||
| 83 | ExternalArray<T>& operator=(const ExternalArray<T>& a) { | ||
| 84 | if (&a != this) { | ||
| 85 | this->setStorage(a.m_elements, a.m_size); | ||
| 86 | } | ||
| 87 | return *this; | ||
| 88 | } | ||
| 89 | |||
| 90 | //! Copy the data from a | ||
| 91 | void copyDataFrom(const ExternalArray<T>& a) { | ||
| 92 | const FwSizeType size = FW_MIN(this->m_size, a.m_size); | ||
| 93 | for (FwSizeType i = 0; i < size; i++) { | ||
| 94 | (*this)[i] = a[i]; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | //! Get a mutable pointer to the elements | ||
| 99 | //! \return A mutable pointer to the elements | ||
| 100 | ✗ | T* getElements() { return this->m_elements; } | |
| 101 | |||
| 102 | //! Get a const pointer to the elements | ||
| 103 | //! \return A const pointer to the elements | ||
| 104 | const T* getElements() const { return this->m_elements; } | ||
| 105 | |||
| 106 | //! Get the size | ||
| 107 | //! \return The size | ||
| 108 | 2242 | FwSizeType getSize() const { return this->m_size; } | |
| 109 | |||
| 110 | //! Set the backing storage (typed data) | ||
| 111 | 14 | void setStorage(T* elements, //!< The array elements | |
| 112 | FwSizeType size //!< The size | ||
| 113 | ) { | ||
| 114 | // Check that elements is not null if the array is nonempty | ||
| 115 | 14 | FW_ASSERT((elements != nullptr) || (size == 0), static_cast<FwAssertArgType>(size)); | |
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
14 | if (elements == this->m_elements) { |
| 117 | // The incoming pointer aliases the existing storage, so do not | ||
| 118 | // release the storage or change the ownership flag | ||
| 119 | ✗ | this->m_size = size; | |
| 120 | } else { | ||
| 121 | 14 | this->releaseStorage(); | |
| 122 | 14 | this->m_elements = elements; | |
| 123 | 14 | this->m_size = size; | |
| 124 | 14 | this->m_destroyElementsOnRelease = false; | |
| 125 | } | ||
| 126 | 14 | } | |
| 127 | |||
| 128 | //! Set the backing storage (untyped data) | ||
| 129 | //! Data must be aligned for T and must contain at least getByteArraySize(size) bytes. | ||
| 130 | void setStorage(ByteArray data, //!< The data | ||
| 131 | FwSizeType size //!< The array size | ||
| 132 | ) { | ||
| 133 | // Check that data.bytes is not null | ||
| 134 | FW_ASSERT(data.bytes != nullptr); | ||
| 135 | // Check that data.bytes is properly aligned | ||
| 136 | FW_ASSERT(reinterpret_cast<uintptr_t>(data.bytes) % alignof(T) == 0); | ||
| 137 | // Check that data.size is large enough to hold the array | ||
| 138 | // The division form is used because size * sizeof(T) can overflow FwSizeType | ||
| 139 | FW_ASSERT(size <= data.size / sizeof(T)); | ||
| 140 | // Release the backing storage | ||
| 141 | this->releaseStorage(); | ||
| 142 | // Initialize the array members | ||
| 143 | this->m_elements = reinterpret_cast<T*>(data.bytes); | ||
| 144 | // Construct the array members in place | ||
| 145 | // This step ensures that each array element holds a valid object | ||
| 146 | // into which we can assign data | ||
| 147 | for (FwSizeType i = 0; i < size; i++) { | ||
| 148 | // This code trips an alignment check in clang-tidy | ||
| 149 | // However the alignment has been checked by FW_ASSERT above | ||
| 150 | (void)new (&this->m_elements[i]) T(); // NOLINT | ||
| 151 | } | ||
| 152 | // Set the size | ||
| 153 | this->m_size = size; | ||
| 154 | // Destroy elements on release of storage | ||
| 155 | this->m_destroyElementsOnRelease = true; | ||
| 156 | } | ||
| 157 | |||
| 158 | public: | ||
| 159 | // ---------------------------------------------------------------------- | ||
| 160 | // Public static functions | ||
| 161 | // ---------------------------------------------------------------------- | ||
| 162 | |||
| 163 | //! Get the alignment of the storage for an ExternalArray | ||
| 164 | //! \return The alignment | ||
| 165 | static constexpr U8 getByteArrayAlignment() { return alignof(T); } | ||
| 166 | |||
| 167 | //! Get the size of the storage for an ExternalArray of the specified size, | ||
| 168 | //! as a byte array | ||
| 169 | //! \return The byte array size | ||
| 170 | static constexpr FwSizeType getByteArraySize(FwSizeType size //!< The size | ||
| 171 | ) { | ||
| 172 | return size * sizeof(T); | ||
| 173 | } | ||
| 174 | |||
| 175 | private: | ||
| 176 | // ---------------------------------------------------------------------- | ||
| 177 | // Private member functions | ||
| 178 | // ---------------------------------------------------------------------- | ||
| 179 | |||
| 180 | //! Release the backing storage | ||
| 181 | 32 | void releaseStorage() { | |
| 182 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
|
32 | if ((this->m_elements != nullptr) && this->m_destroyElementsOnRelease) { |
| 183 | ✗ | for (FwSizeType i = 0; i < this->m_size; i++) { | |
| 184 | ✗ | this->m_elements[i].~T(); | |
| 185 | } | ||
| 186 | ✗ | this->m_destroyElementsOnRelease = false; | |
| 187 | } | ||
| 188 | 32 | } | |
| 189 | |||
| 190 | private: | ||
| 191 | // ---------------------------------------------------------------------- | ||
| 192 | // Private member variables | ||
| 193 | // ---------------------------------------------------------------------- | ||
| 194 | |||
| 195 | //! The array elements | ||
| 196 | T* m_elements = nullptr; | ||
| 197 | |||
| 198 | //! The size | ||
| 199 | FwSizeType m_size = 0; | ||
| 200 | |||
| 201 | //! Whether to destroy the array elements when the backing storage is released | ||
| 202 | bool m_destroyElementsOnRelease = false; | ||
| 203 | }; | ||
| 204 | |||
| 205 | } // namespace Fw | ||
| 206 | |||
| 207 | #endif | ||
| 208 |