Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
make_mdspan.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "This header defines the make_mdspan callable object"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_MAKE_MDSPAN_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_MAKE_MDSPAN_HPP_
9
10// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
11#include "arene/base/array/array.hpp"
12#include "arene/base/compiler_support/cpp14_inline.hpp"
13#include "arene/base/constraints/constraints.hpp"
14#include "arene/base/detail/dynamic_extent.hpp"
15#include "arene/base/mdspan/detail/deduced_static_extent.hpp"
16#include "arene/base/mdspan/extents.hpp"
17#include "arene/base/mdspan/is_accessor_policy.hpp"
18#include "arene/base/mdspan/is_layout_mapping.hpp"
19#include "arene/base/mdspan/mdspan.hpp"
20#include "arene/base/span/span.hpp"
21#include "arene/base/stdlib_choice/cstddef.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/extent.hpp"
24#include "arene/base/stdlib_choice/forward.hpp"
25#include "arene/base/stdlib_choice/is_array.hpp"
26#include "arene/base/stdlib_choice/is_convertible.hpp"
27#include "arene/base/stdlib_choice/is_pointer.hpp"
28#include "arene/base/stdlib_choice/move.hpp"
29#include "arene/base/stdlib_choice/rank.hpp"
30#include "arene/base/stdlib_choice/remove_all_extents.hpp"
31#include "arene/base/stdlib_choice/remove_cv.hpp"
32#include "arene/base/stdlib_choice/remove_pointer.hpp"
33#include "arene/base/stdlib_choice/remove_reference.hpp"
34#include "arene/base/type_traits/all_of.hpp"
35#include "arene/base/type_traits/remove_cvref.hpp"
36// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
37
38namespace arene {
39namespace base {
40
41namespace make_mdspan_detail {
42/// @brief Class to define the @c make_mdspan callable object. Consists entirely of overloads of the function call
43/// operator.
44class make_mdspan_impl {
45 public:
46 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "Constrained via SFINAE, permitted by A13-3-1 Permit #1"
47 /// @brief Helper function to create an @c mdspan from a C array, with automatically deduced extents and default
48 /// accessor
49 /// @tparam CArray The type of the array
50 /// @param array A reference to the array
51 /// @return An @c mdspan referencing the elements of the array as a single-dimensional span with the single extent
52 /// equal to the extent of the array
53 /// @pre The array must be a single-dimensional array with known bounds
54 template <
55 typename CArray,
56 constraints<std::enable_if_t<std::is_array<CArray>::value>, std::enable_if_t<std::rank<CArray>::value == 1>> =
57 nullptr>
58 constexpr auto operator()(CArray& array) const noexcept
59 -> mdspan<std::remove_all_extents_t<CArray>, extents<std::size_t, std::extent<CArray, 0>::value>> {
60 return {array, extents<std::size_t, std::extent<CArray, 0>::value>{}};
61 }
62 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
63
64 /// @brief Helper function to create an @c mdspan from a pointer to a single object, with zero-dimensional extents and
65 /// a default accessor
66 /// @tparam Pointer The pointer type
67 /// @param ptr The pointer to the object
68 /// @return An @c mdspan referencing the pointed-to object as a zero-dimensional span
69 template <
70 typename Pointer,
71 constraints<std::enable_if_t<std::is_pointer<std::remove_reference_t<Pointer>>::value>> = nullptr>
72 constexpr auto operator()(Pointer&& ptr) const noexcept
73 -> mdspan<std::remove_pointer_t<std::remove_reference_t<Pointer>>, extents<std::size_t>> {
74 return {std::forward<Pointer>(ptr), extents<std::size_t>{}};
75 }
76
77 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: All arguments are named"
78 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
79 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and
80 /// the extents of the dimensions
81 /// @tparam ElementType The type of the pointed-to objects
82 /// @tparam Integrals The types of the extents values
83 /// @param ptr The pointer to the objects
84 /// @param extent_values The extents of each of the dimensions
85 /// @return An @c mdspan referencing the pointed-to objects with a number of dimensions equal to the number of
86 /// supplied extents values, where each dimension has a static extent of @c extent_values::value where the supplied
87 /// extent value is an integral-constant-like or a dynamic extent otherwise, and the runtime extent values
88 /// are equal to the supplied values converted to @c std::size_t
89 /// @pre Each type in @c Integrals must be convertible to @c std::size_t
90 template <
91 typename ElementType,
92 typename... Integrals,
93 constraints<
94 std::enable_if_t<sizeof...(Integrals) != 0>,
95 std::enable_if_t<all_of_v<std::is_convertible<Integrals, std::size_t>::value...>>> = nullptr>
96 constexpr auto operator()(ElementType* ptr, Integrals... extent_values) const noexcept -> mdspan<
97 ElementType,
98 extents<std::size_t, mdspan_detail::deduced_static_extent_v<remove_cvref_t<Integrals>>...>> {
99 return {
100 ptr,
101 extents<std::size_t, mdspan_detail::deduced_static_extent_v<remove_cvref_t<Integrals>>...>{
102 std::move(extent_values)...
103 }
104 };
105 }
106 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
107 // parasoft-begin-suppress CERT_C-EXP37-a
108
109 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: extent_values used in return expression"
110 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
111 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and a
112 /// @c span holding the extents of the dimensions
113 /// @tparam ElementType The type of the pointed-to objects
114 /// @tparam DimensionType The type of the supplied dimensions
115 /// @tparam Rank The number of elements in the span
116 /// @param ptr The pointer to the objects
117 /// @param extent_values The extents of each of the dimensions
118 /// @return An @c mdspan referencing the pointed-to objects with a number of dimensions equal to the number of
119 /// supplied extents values, where each dimension has a dynamic extent, and the runtime extent values are equal to the
120 /// supplied values converted to @c std::size_t
121 /// @pre The @c DimensionType must be convertible to @c std::size_t
122 /// @pre The supplied @c Rank must not be @c dynamic_extent --- the rank of the resulting extents must be a fixed
123 /// number, so a dynamic-sized @c span cannot be used
124 template <
125 typename ElementType,
126 typename DimensionType,
127 std::size_t Rank,
128 constraints<
129 std::enable_if_t<std::is_convertible<DimensionType, std::size_t>::value>,
130 std::enable_if_t<Rank != dynamic_extent>> = nullptr>
131 constexpr auto operator()(ElementType* ptr, span<DimensionType, Rank> extent_values) const noexcept
132 -> mdspan<ElementType, dextents<std::size_t, Rank>> {
133 return {ptr, dextents<std::size_t, Rank>{extent_values}};
134 }
135 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
136 // parasoft-end-suppress AUTOSAR-A0_1_4-a
137
138 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: extent_values used in return expression"
139 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
140 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and a
141 /// @c array holding the extents of the dimensions
142 /// @tparam ElementType The type of the pointed-to objects
143 /// @tparam DimensionType The type of the supplied dimensions
144 /// @tparam Rank The number of elements in the span
145 /// @param ptr The pointer to the objects
146 /// @param extent_values The extents of each of the dimensions
147 /// @return An @c mdspan referencing the pointed-to objects with a number of dimensions equal to the number of
148 /// supplied extents values, where each dimension has a dynamic extent, and the runtime extent values are equal to the
149 /// supplied values converted to @c std::size_t
150 /// @pre The @c DimensionType must be convertible to @c std::size_t
151 template <
152 typename ElementType,
153 typename DimensionType,
154 std::size_t Rank,
155 constraints<std::enable_if_t<std::is_convertible<DimensionType, std::size_t>::value>> = nullptr>
156 constexpr auto operator()(ElementType* ptr, array<DimensionType, Rank> const& extent_values) const noexcept
157 -> mdspan<ElementType, dextents<std::size_t, Rank>> {
158 return {ptr, dextents<std::size_t, Rank>{extent_values}};
159 }
160 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
161 // parasoft-end-suppress AUTOSAR-A0_1_4-a
162
163 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and an
164 /// @c extents object
165 /// @tparam ElementType The type of the pointed-to objects
166 /// @tparam ExtentsType The type of the extents object
167 /// @param ptr The pointer to the objects
168 /// @param source_extents The extents
169 /// @return An @c mdspan referencing the pointed-to objects with dimensions specified by the source extents
170 /// @pre The @c ExtentsType must be an instance of @c arene::base::extents
171 template <
172 typename ElementType,
173 typename ExtentsType,
174 constraints<std::enable_if_t<is_extents_v<ExtentsType>>> = nullptr>
175 constexpr auto operator()(ElementType* ptr, ExtentsType const& source_extents) const noexcept
176 -> mdspan<ElementType, ExtentsType> {
177 return {ptr, source_extents};
178 }
179
180 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: There is no identifier 'mapping' being hidden"
181 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and a
182 /// layout mapping
183 /// @tparam ElementType The type of the pointed-to objects
184 /// @tparam MappingType The type of the mapping
185 /// @param ptr The pointer to the objects
186 /// @param mapping The mapping
187 /// @return An @c mdspan referencing the pointed-to objects with dimensions and layout specified by the mapping
188 /// @pre The @c MappingType must be a valid layout mapping
189 template <
190 typename ElementType,
191 typename MappingType,
192 constraints<std::enable_if_t<is_layout_mapping_v<MappingType>>> = nullptr>
193 constexpr auto operator()(ElementType* ptr, MappingType const& mapping) const noexcept
194 -> mdspan<ElementType, typename MappingType::extents_type, typename MappingType::layout_type> {
195 return {ptr, mapping};
196 }
197 // parasoft-end-suppress AUTOSAR-A2_10_1-a
198
199 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: There is no identifier 'mapping' being hidden"
200 /// @brief Helper function to create an @c mdspan from a handle to an array of objects, a layout mapping and an
201 /// accessor
202 /// @tparam MappingType The type of the mapping
203 /// @tparam AccessorType The type of the accessor
204 /// @param ptr The handle to the objects
205 /// @param mapping The mapping
206 /// @param accessor The accessor
207 /// @return An @c mdspan referencing the specified objects with dimensions and layout specified by the mapping, and
208 /// access policy specified by the accessor
209 /// @pre The @c MappingType must be a valid layout mapping
210 /// @pre The @c AccessorType must be a valid accessor policy
211 template <
212 typename MappingType,
213 typename AccessorType,
214 constraints<
215 std::enable_if_t<is_layout_mapping_v<MappingType>>,
216 std::enable_if_t<is_accessor_policy_v<AccessorType>>> = nullptr>
217 constexpr auto operator()(
218 typename AccessorType::data_handle_type const& ptr,
219 MappingType const& mapping,
220 AccessorType const& accessor
221 ) const noexcept
222 -> mdspan<
223 typename AccessorType::element_type,
224 typename MappingType::extents_type,
225 typename MappingType::layout_type,
226 AccessorType> {
227 return {ptr, mapping, accessor};
228 }
229 // parasoft-end-suppress AUTOSAR-A2_10_1-a
230};
231} // namespace make_mdspan_detail
232
233/// @def arene::base::make_mdspan
234/// @brief Helper function to create an @c mdspan from a C array, or pointer to data and a set of extents for the
235/// dimensions. See the documentation for the function call operator overloads for the details.
236// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
237// object used in multiple TUs."
238// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
239// object used in multiple TUs."
240ARENE_CPP14_INLINE_VARIABLE(make_mdspan_detail::make_mdspan_impl, make_mdspan);
241// parasoft-end-suppress AUTOSAR-M7_3_3-a
242// parasoft-end-suppress CERT_CPP-DCL59-a
243
244} // namespace base
245} // namespace arene
246
247#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_MAKE_MDSPAN_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(make_mdspan_detail::make_mdspan_impl, make_mdspan)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10