GCC Code Coverage Report


Directory: ./
File: Fw/LanguageHelpers.hpp
Date: 2026-09-03 21:13:48
Exec Total Coverage
Lines: 14 14 100.0%
Functions: 5 5 100.0%
Branches: 4 4 100.0%

Line Branch Exec Source
1 // ======================================================================
2 // \title LanguageHelpers.hpp
3 // \author lestarch
4 // \brief hpp file for C++ language helper functions
5 //
6 // \copyright
7 // Copyright (C) 2025 California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 // ======================================================================
11 #ifndef FW_TYPES_LANGUAGE_HELPERS_HPP_
12 #define FW_TYPES_LANGUAGE_HELPERS_HPP_
13 #include <new>
14 #include <type_traits>
15 #include "Fw/Types/Assert.hpp"
16 #include "Fw/Types/ByteArray.hpp"
17 namespace Fw {
18 //! \brief placement new for arrays
19 //!
20 //! C++ as a language does not guaranteed that placement new for a C++ array of length N will fit within a memory
21 //! region of size N *sizeof(T). Moreover, there are some compilers whose implementation of placement new for arrays
22 //! do not guarantee this property.
23 //!
24 //! This function provides a helper for placement new for arrays that guarantees that the array will fit within the
25 //! provided memory region. It checks that the provided memory region is large enough to hold the array (N * sizeof(T)
26 //! and that the alignment of the provided memory region is sufficient for the type T. It also checks that the provided
27 //! memory region is non-null.
28 //!
29 //! \warning this function cannot be used for arrays of arrays (i.e. T cannot be an array type).
30 //!
31 //! \tparam T the type of the array elements
32 //! \param array the byte array to use for placement new (pair of bytes pointer and size)
33 //! \param arraySize the number of elements in the array
34 //! \return a pointer to the array of type T
35 template <typename T>
36 4552 T* arrayPlacementNew(Fw::ByteArray array, FwSizeType arraySize) {
37 static_assert(!std::is_array<T>::value, "Cannot use arrayPlacementNew new for arrays of arrays");
38 static_assert(std::is_constructible<T>::value,
39 "Cannot use arrayPlacementNew on types without a default zero-argument constructor");
40 4552 void* base_pointer = reinterpret_cast<void*>(array.bytes);
41 4552 FW_ASSERT(base_pointer != nullptr);
42 4552 FW_ASSERT((reinterpret_cast<PlatformPointerCastType>(base_pointer) % alignof(T)) == 0);
43 4552 FW_ASSERT(array.size >= (sizeof(T) * arraySize));
44 4552 T* type_pointer = static_cast<T*>(base_pointer);
45
2/2
✓ Branch 0 taken 150174 times.
✓ Branch 1 taken 4552 times.
154726 for (FwSizeType index = 0; index < arraySize; index++) {
46 150174 new (&type_pointer[index]) T();
47 }
48 4552 return type_pointer;
49 }
50
51 //! \brief placement delete for arrays
52 //!
53 //! This is the partner of tha above function that performs the destructor operation on every element of type T in the
54 //! array. This assumes that all elements have been constructed.
55 //!
56 //! \warning this function cannot be used for arrays of arrays (i.e. T cannot be an array type).
57 //!
58 //! \tparam T the type of the array elements
59 //! \param arrayPointer pointer to an array of type T
60 //! \param arraySize the number of elements in the array
61 template <typename T>
62 1643 void arrayPlacementDestruct(T* arrayPointer, FwSizeType arraySize) {
63 static_assert(!std::is_array<T>::value, "Cannot use arrayPlacementDestruct new for arrays of arrays");
64 1643 FW_ASSERT(arrayPointer != nullptr);
65
2/2
✓ Branch 0 taken 18271 times.
✓ Branch 1 taken 1643 times.
19914 for (FwSizeType index = 0; index < arraySize; index++) {
66 18271 arrayPointer[index].~T();
67 }
68 1643 }
69 } // namespace Fw
70 #endif // FW_TYPES_LANGUAGE_HELPERS_HPP_
71