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
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/// @brief Destroys the object(s) located at the given memory address @c std::destroy_at back ported from C++20.
56/// @tparam T the type to destroy.
57/// The pointer @c ptr identifies the object(s) to destroy.
58/// @post If @c T is not an array type, equivalent to having called the destructor on the object pointed to by @c ptr.
59/// If @c T is an array type, recursively destroys elements of @c *ptr in order.
60/// @note Unlike @c std::destroy_at, constexpr evaluation is not supported as dynamic memory allocation (even using
61/// placement new) is not supported until C++20.
62// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
63// object used in multiple TUs."
64// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
65// object used in multiple TUs."
66ARENE_CPP14_INLINE_VARIABLE(destroy_at_detail::do_destroy_at, destroy_at);
67// parasoft-end-suppress AUTOSAR-M7_3_3-a
68// parasoft-end-suppress CERT_CPP-DCL59-a
69
70} // namespace base
71} // namespace arene
72#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MEMORY_DESTROY_AT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
Definition accumulate.hpp:36
constexpr auto addressof(T &value) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:24