Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
submdspan_canonicalize_slices.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines submdspan_canonicalize_slices"
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_SUBMDSPAN_CANONICALIZE_SLICES_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_CANONICALIZE_SLICES_HPP_
9
14#include "arene/base/mdspan/detail/canonical_slice.hpp"
15#include "arene/base/mdspan/detail/is_valid_submdspan_slice_for.hpp"
16#include "arene/base/mdspan/detail/mandate_each_argtype_is_submdspan_slice.hpp"
17#include "arene/base/mdspan/detail/mandate_each_argtype_is_valid_submdspan_slice_type_for.hpp"
18#include "arene/base/mdspan/detail/to_array.hpp"
20#include "arene/base/stdlib_choice/cstddef.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/ignore.hpp"
23#include "arene/base/stdlib_choice/logical_and.hpp"
24#include "arene/base/stdlib_choice/move.hpp"
25#include "arene/base/stdlib_choice/tuple.hpp"
27#include "arene/base/tuple/detail/tuple_transform_reduce.hpp"
28#include "arene/base/tuple/detail/tuple_zip.hpp"
32
33// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
34// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
35
36namespace arene {
37namespace base {
38
39namespace submdspan_canonicalize_slices_detail {
40
41/// @brief convert slices to canonical form: a @c std::tuple of slices where
42/// each is @c full_extent_t, a single index, or @c extent_slice
43class submdspan_canonicalize_slices_fn {
44 public:
45 // parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: no commented-out code, this is documentation"
46 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameter 'src' is used"
47 /// @brief convert slices to canonical form
48 /// @tparam IndexType index type
49 /// @tparam Extents static extent values
50 /// @tparam SliceSpecifiers slice types
51 /// @param src @c extents to slice
52 /// @param slices @c submdspan slice specifiers
53 ///
54 /// @return @c std::tuple of canonical slices where the canonical form of a
55 /// value @c s with type @c S will be:
56 /// * @c static_cast<full_extent_t>(std::move(s)) if the slice is convertible
57 /// to @c full_extent_t; otherwise
58 /// * @c std::integral_constant<IndexType, S::value> if the slice is
59 /// integral-constant-like; otherwise
60 /// * @c IndexType(std::move(s)) if the slice is convertible to @c IndexType;
61 /// otherwise
62 /// * a specialization of @c extent_slice if @c S is a specialization of
63 /// @c extent_slice or @c range_slice; otherwise
64 /// * a specialization of @c extent_slice if @c S is index-pair-like
65 ///
66 /// @note Constraints <br>
67 /// * <c> sizeof...(Extents) == sizeof...(SliceSpecifiers) </c>
68 /// @note Mandates <br>
69 /// * each type @c S in @c SliceSpecifiers... is a valid submdspan slice for
70 /// @c IndexType
71 /// * the canonical form of each @c S in @c SliceSpecifiers... is a valid
72 /// submdspan slice type for associated static extent of @c src
73 // parasoft-end-suppress AUTOSAR-A2_7_2-a
74 template <
75 class IndexType,
76 std::size_t... Extents,
77 class... SliceSpecifiers,
78 constraints<std::enable_if_t<sizeof...(Extents) == sizeof...(SliceSpecifiers)>> = nullptr,
79 std::nullptr_t = mdspan_detail::mandate_each_argtype_is_submdspan_slice<IndexType, SliceSpecifiers...>()>
80 constexpr auto operator()(extents<IndexType, Extents...> const& src, SliceSpecifiers... slices) const noexcept( //
81 all_of_v<is_nothrow_invocable_v<mdspan_detail::canonical_slice_fn<IndexType>, SliceSpecifiers&&>...>
82 ) ->
83 std::tuple<mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers>...>
84 {
85 // GCC8 doesn't like having this in the template parameter list so we'll make it an ignored variable in the body.
86 std::ignore = mdspan_detail::mandate_each_argtype_is_valid_submdspan_slice_type_for<IndexType, Extents...>(
87 type_list<SliceSpecifiers...>{}
88 );
89
90 auto const canonicalized = std::tuple<mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers>...>{
91 mdspan_detail::canonical_slice<IndexType>(std::move(slices))...
92 };
93
94 // Reference elements prevent tuple_transform_reduce from copying slices through tuple_zip,
95 // avoiding the Clang 20 x86-64 miscompilation in https://github.com/llvm/llvm-project/issues/205291.
96 using slice_refs = std::tuple<mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers> const&...>;
97 // parasoft-begin-suppress AUTOSAR-M0_1_3-a "False positive: 'refs' is passed to tuple_zip and 'validate_slice' is
98 // moved into tuple_transform_reduce"
99 slice_refs refs{canonicalized};
100 auto zipped = tuple_detail::tuple_zip(refs, mdspan_detail::to_array(src));
101 auto validate_slice = arene::base::bind_front(apply, mdspan_detail::is_valid_submdspan_slice_for);
102 // parasoft-end-suppress AUTOSAR-M0_1_3-a
104 tuple_detail::tuple_transform_reduce(std::move(zipped), true, std::logical_and<>{}, std::move(validate_slice))
105 );
106
107 return canonicalized;
108 }
109 // parasoft-end-suppress AUTOSAR-A0_1_4-a
110 // parasoft-end-suppress AUTOSAR-A2_7_2-a
111};
112} // namespace submdspan_canonicalize_slices_detail
113
114// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variables are initialized"
115
116// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
117// object used in multiple TUs."
118// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
119// object used in multiple TUs."
120/// @brief transforms @c submdspan slice arguments to canonical form
121///
122/// @see submdspan_canonicalize_slices_detail::submdspan_canonicalize_slices_fn
124 submdspan_canonicalize_slices_detail::submdspan_canonicalize_slices_fn,
125 submdspan_canonicalize_slices
126);
127// parasoft-end-suppress AUTOSAR-M7_3_3-a
128// parasoft-end-suppress CERT_CPP-DCL59-a
129// parasoft-end-suppress CERT_CPP-DCL56-a
130
131} // namespace base
132} // namespace arene
133
134#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_CANONICALIZE_SLICES_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr bool all_of_v
Trait which tests if every value in a parameter pack is true .
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
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
Definition accumulate.hpp:36
typedef nullptr_t
The type of nullptr.
Definition cstddef.hpp:33
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
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
Provides the all_of_v<bool...> trait.