Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
destroy_at.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::destroy_at"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_DESTROY_AT_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_DESTROY_AT_HPP_
8
9// IWYU pragma: private, include "arene/base/memory.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12#include "arene/base/compiler_support/cpp14_inline.hpp"
13#include "arene/base/constraints/constraints.hpp"
14#include "arene/base/stdlib_choice/addressof.hpp"
15#include "arene/base/stdlib_choice/enable_if.hpp"
16#include "arene/base/stdlib_choice/is_array.hpp"
17
18namespace arene {
19namespace base {
20
21namespace destroy_at_detail {
22
23/// @brief Implementation helper for @c arene::base::destroy_at.
24class do_destroy_at {
25 public:
26 // parasoft-begin-suppress AUTOSAR-M0_1_8-a "False positive: there is an external side effect"
27 /// @brief Overload for non array types.
28 /// @see arene::base::destroy_at
29 /// @tparam T The type
30 /// @param ptr A pointer to the value
31 template <typename T, constraints<std::enable_if_t<!std::is_array<T>::value>> = nullptr>
32 void operator()(T* ptr) const noexcept {
33 ptr->~T();
34 }
35 // parasoft-end-suppress AUTOSAR-M0_1_8-a
36
37 /// @brief Overload for array types.
38 /// @see arene::base::destroy_at
39 /// @tparam T The array type
40 /// @param ptr A pointer to the array
41 template <typename T, constraints<std::enable_if_t<std::is_array<T>::value>> = nullptr>
42 // parasoft-begin-suppress AUTOSAR-A5_0_3-a-2 "False positive: ptr has only one level of indirection"
43 void operator()(T* ptr) const noexcept {
44 // parasoft-end-suppress AUTOSAR-A5_0_3-a-2
45 // parasoft-begin-suppress AUTOSAR-A18_1_1-a-2 "Iteration through C array provided by caller"
46 for (auto& element : *ptr) {
47 // parasoft-end-suppress AUTOSAR-A18_1_1-a-2
48 (*this)(std::addressof(element));
49 }
50 }
51};
52
53} // namespace destroy_at_detail
54
55/// @def arene::base::destroy_at
56/// @brief Destroys the object(s) located at the given memory address @c std::destroy_at back ported from C++20.
57/// @tparam T the type to destroy.
58/// @param ptr a pointer to the object(s) to be destroyed.
59/// @post If @c T is not an array type, equivalent to having called the destructor on the object pointed to by @c ptr.
60/// If @c T is an array type, recursively destroys elements of @c *ptr in order.
61/// @note Unlike @c std::destroy_at, constexpr evaluation is not supported as dynamic memory allocation (even using
62/// placement new) is not supported until C++20.
63// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
64// object used in multiple TUs."
65// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
66// object used in multiple TUs."
67ARENE_CPP14_INLINE_VARIABLE(destroy_at_detail::do_destroy_at, destroy_at);
68// parasoft-end-suppress AUTOSAR-M7_3_3-a
69// parasoft-end-suppress CERT_CPP-DCL59-a
70
71} // namespace base
72} // namespace arene
73#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_DESTROY_AT_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(destroy_at_detail::do_destroy_at, destroy_at)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10