Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
to_array.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "False positive: this header intentionally defines both an implementation
2// class and function object API." Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ARRAY_TO_ARRAY_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ARRAY_TO_ARRAY_HPP_
8
9// IWYU pragma: private, include "arene/base/array.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
18#include "arene/base/stdlib_choice/conditional.hpp"
19#include "arene/base/stdlib_choice/cstddef.hpp"
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/integer_sequence.hpp"
22#include "arene/base/stdlib_choice/is_array.hpp"
23#include "arene/base/stdlib_choice/is_constructible.hpp"
24#include "arene/base/stdlib_choice/is_move_constructible.hpp"
25#include "arene/base/stdlib_choice/is_object.hpp"
26#include "arene/base/stdlib_choice/is_void.hpp"
27#include "arene/base/stdlib_choice/move.hpp"
28#include "arene/base/stdlib_choice/remove_cv.hpp"
29#include "arene/base/stdlib_choice/remove_reference.hpp"
32
33// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
34
35namespace arene {
36namespace base {
37namespace to_array_detail {
38
39/// @brief function object implementing the @c to_array and @c to_array_of helpers
40/// @tparam D destination element type, or @c void to request that the element type be deduced from the source array
41/// (with cvref qualifiers removed)
42///
43/// Constructs an @c array from a C-array, deducing the size from the source array and either deducing the element type
44/// (when @c D is @c void) or converting each element to the explicitly requested type @c D.
45template <class D>
46class to_array_fn {
47 static_assert(std::is_void<D>::value || std::is_object<D>::value, "'D' must an object type");
48
49 /// @brief type of the conversion function object applied to each element
50 using convert_fn = std::conditional_t<std::is_void<D>::value, identity, decltype(construct<D>)>;
51
52 /// @brief type of the resulting array's element type, deduced from the source array's element type if @c D is @c void
53 /// @tparam From reference-qualified element type of the source array
54 template <typename From>
55 using to_t = std::conditional_t<std::is_void<D>::value, remove_cvref_t<From>, D>;
56
57 /// @brief Implementation for both the lvalue and rvalue overloads
58 /// @tparam From reference-qualified element type of the source array, used to propagate value category
59 /// @tparam Is parameter pack of indices of the elements to read from @c arr
60 /// @tparam Iterator iterator type providing indexed access to the source array's elements
61 /// @tparam To element type of the resulting @c array
62 /// @param arr iterator-like handle to the source array's elements.
63 /// @return @c array<Result, sizeof...(Is)> whose elements are constructed from
64 /// <c> convert_fn{}(static_cast<From>(arr[Is]))... </c>
65 ///
66 /// Builds the resulting @c array by indexing into @c arr with each element of the index sequence and converting
67 /// through @c convert_fn. The cast to @c From propagates the value category of the source array's elements so that
68 /// rvalue overloads move from their elements while lvalue overloads copy from theirs.
69 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
70 template <typename From, std::size_t... Is, typename Iterator>
71 static constexpr auto impl(std::index_sequence<Is...>, Iterator arr) noexcept(
72 noexcept(array<to_t<From>, sizeof...(Is)>{{convert_fn{}(static_cast<From>(arr[Is]))...}})
73 ) -> array<to_t<From>, sizeof...(Is)> {
74 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "False positive: the pointer points to an array"
75 return array<to_t<From>, sizeof...(Is)>{{convert_fn{}(static_cast<From>(arr[Is]))...}};
76 // parasoft-end-suppress AUTOSAR-M5_0_15-a
77 }
78 // parasoft-end-suppress CERT_C-EXP37-a
79
80 public:
81 // NOLINTBEGIN(hicpp-avoid-c-arrays) Specifically providing overloads for c-array support.
82 /// @brief construct an @c array from an lvalue C-array
83 /// @param arr source values to copy from
84 /// @return @c array of the deduced or requested element type
85 template <
86 typename T,
87 std::size_t Size,
89 std::enable_if_t<!std::is_array<T>::value>,
92 constexpr auto operator()(T (&arr)[Size]) const noexcept( //
93 noexcept(impl<T&>(std::make_index_sequence<Size>{}, arr))
94 ) -> array<to_t<T&>, Size> {
95 return impl<T&>(std::make_index_sequence<Size>{}, arr);
96 }
97 /// @brief construct an @c array from an rvalue C-array
98 /// @param arr source values to move from
99 /// @return @c array of the deduced or requested element type
100 template <
101 typename T,
102 std::size_t Size,
104 std::enable_if_t<!std::is_array<T>::value>,
107 constexpr auto operator()(T (&&arr)[Size]) const noexcept( //
108 noexcept(impl<T&&>(std::make_index_sequence<Size>{}, std::move(arr)))
109 ) -> array<to_t<T&&>, Size> {
110 // If constexpr move_iterator were available, we could use that. Since it is not, we propagate the ref-qualifier.
111 return impl<T&&>(std::make_index_sequence<Size>{}, std::move(arr));
112 }
113 // NOLINTEND(hicpp-avoid-c-arrays)
114};
115
116} // namespace to_array_detail
117
118/// @brief construct an @c array from a C-array
119/// Deduces the resulting element type and copies or moves the source elements as appropriate.
120// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
121// object used in multiple TUs."
122// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
123// object used in multiple TUs."
124ARENE_CPP14_INLINE_VARIABLE(to_array_detail::to_array_fn<void>, to_array);
125// parasoft-end-suppress AUTOSAR-M7_3_3-a
126// parasoft-end-suppress CERT_CPP-DCL59-a
127
128/// @brief construct an @c array with a desired element type from a C-array.
129/// @tparam T element type of the resulting @c array
130///
131/// Like @c to_array, but the element type of the resulting @c array is specified. Each source element is converted to
132/// @c T before being used to initialize the corresponding element of the result.
133template <typename T>
134constexpr auto to_array_of = to_array_detail::to_array_fn<T>{};
135
136// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
137
138} // namespace base
139} // namespace arene
140
141// parasoft-end-suppress AUTOSAR-A2_8_1-a
142
143#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ARRAY_TO_ARRAY_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
constexpr auto to_array_of
construct an array with a desired element type from a C-array.
Definition to_array.hpp:134
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
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
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::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