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"
14#include "arene/base/detail/dynamic_extent.hpp"
15#include "arene/base/mdspan/detail/deduced_static_extent.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"
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,
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 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: Parameters are all used"
80 // parasoft-begin-suppress AUTOSAR-A8_4_2-a "False positive: Function does return a value"
81 // parasoft-begin-suppress CERT_C-MSC37-a "False positive: Function does return a value"
82 // parasoft-begin-suppress CERT_CPP-MSC52-a "False positive: Function does return a value"
83 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and
84 /// the extents of the dimensions
85 /// @tparam ElementType The type of the pointed-to objects
86 /// @tparam Integrals The types of the extents values
87 /// @param ptr The pointer to the objects
88 /// @param extent_values The extents of each of the dimensions
89 /// @return An @c mdspan referencing the pointed-to objects with a number of dimensions equal to the number of
90 /// supplied extents values, where each dimension has a static extent of @c extent_values::value where the supplied
91 /// extent value is an integral-constant-like or a dynamic extent otherwise, and the runtime extent values
92 /// are equal to the supplied values converted to @c std::size_t
93 /// @pre Each type in @c Integrals must be convertible to @c std::size_t
94 template <
95 typename ElementType,
96 typename... Integrals,
98 std::enable_if_t<sizeof...(Integrals) != 0>,
100 constexpr auto operator()(ElementType* ptr, Integrals... extent_values) const noexcept -> mdspan<
101 ElementType,
102 extents<std::size_t, mdspan_detail::deduced_static_extent_v<remove_cvref_t<Integrals>>...>> {
103 return {
104 ptr,
105 extents<std::size_t, mdspan_detail::deduced_static_extent_v<remove_cvref_t<Integrals>>...>{
106 std::move(extent_values)...
107 }
108 };
109 }
110 // parasoft-end-suppress CERT_C-MSC37-a
111 // parasoft-end-suppress CERT_CPP-MSC52-a
112 // parasoft-end-suppress AUTOSAR-A8_4_2-a
113 // parasoft-end-suppress AUTOSAR-A0_1_4-a
114 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
115 // parasoft-begin-suppress CERT_C-EXP37-a
116
117 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: extent_values used in return expression"
118 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
119 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and a
120 /// @c span holding the extents of the dimensions
121 /// @tparam ElementType The type of the pointed-to objects
122 /// @tparam DimensionType The type of the supplied dimensions
123 /// @tparam Rank The number of elements in the span
124 /// @param ptr The pointer to the objects
125 /// @param extent_values The extents of each of the dimensions
126 /// @return An @c mdspan referencing the pointed-to objects with a number of dimensions equal to the number of
127 /// supplied extents values, where each dimension has a dynamic extent, and the runtime extent values are equal to the
128 /// supplied values converted to @c std::size_t
129 /// @pre The @c DimensionType must be convertible to @c std::size_t
130 /// @pre The supplied @c Rank must not be @c dynamic_extent --- the rank of the resulting extents must be a fixed
131 /// number, so a dynamic-sized @c span cannot be used
132 template <
133 typename ElementType,
134 typename DimensionType,
135 std::size_t Rank,
137 std::enable_if_t<std::is_convertible<DimensionType, std::size_t>::value>,
139 constexpr auto operator()(ElementType* ptr, span<DimensionType, Rank> extent_values) const noexcept
140 -> mdspan<ElementType, dextents<std::size_t, Rank>> {
141 return {ptr, dextents<std::size_t, Rank>{extent_values}};
142 }
143 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
144 // parasoft-end-suppress AUTOSAR-A0_1_4-a
145
146 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: extent_values used in return expression"
147 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
148 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and a
149 /// @c array holding the extents of the dimensions
150 /// @tparam ElementType The type of the pointed-to objects
151 /// @tparam DimensionType The type of the supplied dimensions
152 /// @tparam Rank The number of elements in the span
153 /// @param ptr The pointer to the objects
154 /// @param extent_values The extents of each of the dimensions
155 /// @return An @c mdspan referencing the pointed-to objects with a number of dimensions equal to the number of
156 /// supplied extents values, where each dimension has a dynamic extent, and the runtime extent values are equal to the
157 /// supplied values converted to @c std::size_t
158 /// @pre The @c DimensionType must be convertible to @c std::size_t
159 template <
160 typename ElementType,
161 typename DimensionType,
162 std::size_t Rank,
163 constraints<std::enable_if_t<std::is_convertible<DimensionType, std::size_t>::value>> = nullptr>
164 constexpr auto operator()(ElementType* ptr, array<DimensionType, Rank> const& extent_values) const noexcept
165 -> mdspan<ElementType, dextents<std::size_t, Rank>> {
166 return {ptr, dextents<std::size_t, Rank>{extent_values}};
167 }
168 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
169 // parasoft-end-suppress AUTOSAR-A0_1_4-a
170
171 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and an
172 /// @c extents object
173 /// @tparam ElementType The type of the pointed-to objects
174 /// @tparam ExtentsType The type of the extents object
175 /// @param ptr The pointer to the objects
176 /// @param source_extents The extents
177 /// @return An @c mdspan referencing the pointed-to objects with dimensions specified by the source extents
178 /// @pre The @c ExtentsType must be an instance of @c arene::base::extents
179 template <
180 typename ElementType,
181 typename ExtentsType,
183 constexpr auto operator()(ElementType* ptr, ExtentsType const& source_extents) const noexcept
184 -> mdspan<ElementType, ExtentsType> {
185 return {ptr, source_extents};
186 }
187
188 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: There is no identifier 'mapping' being hidden"
189 /// @brief Helper function to create an @c mdspan with a default accessor from a pointer to an array of objects and a
190 /// layout mapping
191 /// @tparam ElementType The type of the pointed-to objects
192 /// @tparam MappingType The type of the mapping
193 /// @param ptr The pointer to the objects
194 /// @param mapping The mapping
195 /// @return An @c mdspan referencing the pointed-to objects with dimensions and layout specified by the mapping
196 /// @pre The @c MappingType must be a valid layout mapping
197 template <
198 typename ElementType,
199 typename MappingType,
201 constexpr auto operator()(ElementType* ptr, MappingType const& mapping) const noexcept
202 -> mdspan<ElementType, typename MappingType::extents_type, typename MappingType::layout_type> {
203 return {ptr, mapping};
204 }
205 // parasoft-end-suppress AUTOSAR-A2_10_1-a
206
207 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: There is no identifier 'mapping' being hidden"
208 /// @brief Helper function to create an @c mdspan from a handle to an array of objects, a layout mapping and an
209 /// accessor
210 /// @tparam MappingType The type of the mapping
211 /// @tparam AccessorType The type of the accessor
212 /// @param ptr The handle to the objects
213 /// @param mapping The mapping
214 /// @param accessor The accessor
215 /// @return An @c mdspan referencing the specified objects with dimensions and layout specified by the mapping, and
216 /// access policy specified by the accessor
217 /// @pre The @c MappingType must be a valid layout mapping
218 /// @pre The @c AccessorType must be a valid accessor policy
219 template <
220 typename MappingType,
221 typename AccessorType,
225 constexpr auto operator()(
226 typename AccessorType::data_handle_type const& ptr,
227 MappingType const& mapping,
228 AccessorType const& accessor
229 ) const noexcept
230 -> mdspan<
231 typename AccessorType::element_type,
232 typename MappingType::extents_type,
233 typename MappingType::layout_type,
234 AccessorType> {
235 return {ptr, mapping, accessor};
236 }
237 // parasoft-end-suppress AUTOSAR-A2_10_1-a
238};
239} // namespace make_mdspan_detail
240
241/// @brief Helper function to create an @c mdspan from a C array, or pointer to data and a set of extents for the
242/// dimensions. See the documentation for the function call operator overloads for the details.
243// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
244// object used in multiple TUs."
245// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
246// object used in multiple TUs."
247ARENE_CPP14_INLINE_VARIABLE(make_mdspan_detail::make_mdspan_impl, make_mdspan);
248// parasoft-end-suppress AUTOSAR-M7_3_3-a
249// parasoft-end-suppress CERT_CPP-DCL59-a
250
251} // namespace base
252} // namespace arene
253
254#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_MAKE_MDSPAN_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
static constexpr value_type value
Definition integral_constant.hpp:34
Definition accumulate.hpp:37
typename extents_detail::dextents_helper< IndexType, std::integral_constant< std::size_t, Rank > >::type dextents
Backport of std::dextents from C++23, a type representing the extents of a multidimensional array wit...
Definition extents.hpp:407
constexpr bool all_of_v
Trait which tests if every value in a parameter pack is true .
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24
Provides the all_of_v<bool...> trait.