F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
LanguageHelpers.hpp
Go to the documentation of this file.
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 {
35 template <typename T>
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  void* base_pointer = reinterpret_cast<void*>(array.bytes);
41  FW_ASSERT(base_pointer != nullptr);
42  FW_ASSERT((reinterpret_cast<PlatformPointerCastType>(base_pointer) % alignof(T)) == 0);
43  FW_ASSERT(array.size >= (sizeof(T) * arraySize));
44  T* type_pointer = static_cast<T*>(base_pointer);
45  for (FwSizeType index = 0; index < arraySize; index++) {
46  new (&type_pointer[index]) T();
47  }
48  return type_pointer;
49 }
50 
61 template <typename T>
62 void arrayPlacementDestruct(T* arrayPointer, FwSizeType arraySize) {
63  static_assert(!std::is_array<T>::value, "Cannot use arrayPlacementDestruct new for arrays of arrays");
64  FW_ASSERT(arrayPointer != nullptr);
65  for (FwSizeType index = 0; index < arraySize; index++) {
66  arrayPointer[index].~T();
67  }
68 }
69 } // namespace Fw
70 #endif // FW_TYPES_LANGUAGE_HELPERS_HPP_
PlatformSizeType FwSizeType
void arrayPlacementDestruct(T *arrayPointer, FwSizeType arraySize)
placement delete for arrays
A variable-length byte array.
Definition: ByteArray.hpp:23
U8 *const bytes
The bytes.
Definition: ByteArray.hpp:40
Implementation of malloc based allocator.
T * arrayPlacementNew(Fw::ByteArray array, FwSizeType arraySize)
placement new for arrays
#define FW_ASSERT(...)
Definition: Assert.hpp:14
const FwSizeType size
The size.
Definition: ByteArray.hpp:43