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
10#include "arene/base/compiler_support/cpp14_inline.hpp"
11#include "arene/base/constraints/constraints.hpp"
12#include "arene/base/contracts/contract.hpp"
13#include "arene/base/mdspan/detail/canonical_slice.hpp"
14#include "arene/base/mdspan/detail/is_valid_submdspan_slice_for.hpp"
15#include "arene/base/mdspan/detail/is_valid_submdspan_slice_type_for.hpp"
16#include "arene/base/mdspan/detail/mandate_each_argtype_is_submdspan_slice.hpp"
17#include "arene/base/mdspan/detail/to_array.hpp"
18#include "arene/base/mdspan/extents.hpp"
19#include "arene/base/stdlib_choice/cstddef.hpp"
20#include "arene/base/stdlib_choice/declval.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/logical_and.hpp"
23#include "arene/base/stdlib_choice/make_tuple.hpp" // IWYU pragma: export
24#include "arene/base/stdlib_choice/move.hpp"
25#include "arene/base/tuple/detail/tuple_transform_reduce.hpp"
26#include "arene/base/type_traits/all_of.hpp"
27#include "arene/base/type_traits/is_invocable.hpp"
28
29// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
30// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
31
32namespace arene {
33namespace base {
34
35namespace submdspan_canonicalize_slices_detail {
36
37/// @brief convert slices to canonical form: a @c std::tuple of slices where
38/// each is @c full_extent_t, a single index, or @c extent_slice
39class submdspan_canonicalize_slices_fn {
40 public:
41 // parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: no commented-out code, this is documentation"
42 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameter 'src' is used"
43 /// @brief convert slices to canonical form
44 /// @tparam IndexType index type
45 /// @tparam Extents static extent values
46 /// @tparam SliceSpecifiers slice types
47 /// @param src @c extents to slice
48 /// @param slices @c submdspan slice specifiers
49 ///
50 /// @return @c std::tuple of canonical slices where the canonical form of a
51 /// value @c s with type @c S will be:
52 /// * @c static_cast<full_extent_t>(std::move(s)) if the slice is convertible
53 /// to @c full_extent_t; otherwise
54 /// * @c std::integral_constant<IndexType, S::value> if the slice is
55 /// integral-constant-like; otherwise
56 /// * @c IndexType(std::move(s)) if the slice is convertible to @c IndexType;
57 /// otherwise
58 /// * a specialization of @c extent_slice if @c S is a specialization of
59 /// @c extent_slice or @c range_slice; otherwise
60 /// * a specialization of @c extent_slice if @c S is index-pair-like
61 ///
62 /// @note Constraints <br>
63 /// * <c> sizeof...(Extents) == sizeof...(SliceSpecifiers) </c>
64 /// @note Mandates <br>
65 /// * each type @c S in @c SliceSpecifiers... is a valid submdspan slice for
66 /// @c IndexType
67 /// * the canonical form of each @c S in @c SliceSpecifiers... is a valid
68 /// submdspan slice type for associated static extent of @c src
69 // parasoft-end-suppress AUTOSAR-A2_7_2-a
70 template <
71 class IndexType,
72 std::size_t... Extents,
73 class... SliceSpecifiers,
74 constraints<std::enable_if_t<sizeof...(Extents) == sizeof...(SliceSpecifiers)>> = nullptr,
75 std::nullptr_t = mdspan_detail::mandate_each_argtype_is_submdspan_slice<IndexType, SliceSpecifiers...>()>
76 constexpr auto operator()(extents<IndexType, Extents...> const& src, SliceSpecifiers... slices) const noexcept( //
77 all_of_v<is_nothrow_invocable_v<mdspan_detail::canonical_slice_fn<IndexType>, SliceSpecifiers&&>...>
78 ) -> decltype(std::make_tuple(std::declval<mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers>>()...)) {
79 using mdspan_detail::is_valid_submdspan_slice_type_for_v;
80 static_assert(
81 all_of_v<is_valid_submdspan_slice_type_for_v<
82 mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers>,
83 Extents>...>,
84 "each type in 'SliceSpecifiers' must be a valid 'submdspan' slice type for the associated extent"
85 );
86
87 auto const canonicalized = std::make_tuple(mdspan_detail::canonical_slice<IndexType>(std::move(slices))...);
88
89 ARENE_PRECONDITION(tuple_detail::tuple_transform_reduce(
90 canonicalized,
91 mdspan_detail::to_array(src),
92 true,
93 std::logical_and<>{},
94 mdspan_detail::is_valid_submdspan_slice_for
95 ));
96
97 return canonicalized;
98 }
99 // parasoft-end-suppress AUTOSAR-A0_1_4-a
100 // parasoft-end-suppress AUTOSAR-A2_7_2-a
101};
102} // namespace submdspan_canonicalize_slices_detail
103
104// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variables are initialized"
105
106// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
107// object used in multiple TUs."
108// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
109// object used in multiple TUs."
110/// @def arene::base::submdspan_canonicalize_slices
111/// @brief transforms @c submdspan slice arguments to canonical form
112///
113/// @see submdspan_canonicalize_slices_detail::submdspan_canonicalize_slices_fn
115 submdspan_canonicalize_slices_detail::submdspan_canonicalize_slices_fn,
117);
118// parasoft-end-suppress AUTOSAR-M7_3_3-a
119// parasoft-end-suppress CERT_CPP-DCL59-a
120// parasoft-end-suppress CERT_CPP-DCL56-a
121
122} // namespace base
123} // namespace arene
124
125#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_CANONICALIZE_SLICES_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(submdspan_canonicalize_slices_detail::submdspan_canonicalize_slices_fn, submdspan_canonicalize_slices)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10