Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
for_each.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_FOR_EACH_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_FOR_EACH_HPP_
7
8// IWYU pragma: private, include "arene/base/tuple.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/constraints/constraints.hpp"
13#include "arene/base/functional/invoke.hpp"
14#include "arene/base/monostate/monostate.hpp"
15#include "arene/base/stdlib_choice/cstddef.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/forward.hpp"
18#include "arene/base/stdlib_choice/integer_sequence.hpp"
19#include "arene/base/stdlib_choice/integral_constant.hpp"
20#include "arene/base/stdlib_choice/remove_reference.hpp"
21#include "arene/base/stdlib_choice/tuple_size.hpp"
22#include "arene/base/tuple/detail/get.hpp"
23#include "arene/base/type_manipulation/consume_values.hpp"
24#include "arene/base/type_traits/is_invocable.hpp"
25// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
26
27// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
28
29namespace arene {
30namespace base {
31
32namespace tuple_for_each_detail {
33/// @brief Invokes the specified callable with the specified arguments and discard the result, returning @c monostate to
34/// be consumable
35/// @tparam F The type of the callable to invoke
36/// @tparam ArgTypes The types of the arguments
37/// @param func The callable to invoke
38/// @param args The arguments with which to invoke the callable
39/// @return A @c monostate object
40/// @note Does not participate in overload resolution if the specified @c F cannot be invoked with the specified @c
41/// ArgTypes
42template <typename F, typename... ArgTypes, constraints<std::enable_if_t<is_invocable_v<F, ArgTypes...>>> = nullptr>
43constexpr auto invoke_for_consume(F&& func, ArgTypes&&... args) noexcept(is_nothrow_invocable_v<F, ArgTypes...>)
44 -> monostate {
45 static_cast<void>(arene::base::invoke(std::forward<F>(func), std::forward<ArgTypes>(args)...));
46 return {};
47}
48
49// parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameters 'tuple' and 'operation' are used"
50
51/// @brief The underlying implementation for for_each. Not intended to be
52/// called by user code.
53/// @tparam Tuple type implementing the tuple protocol.
54/// @tparam Op The type of operation to perform on each tuple element.
55/// @tparam I The parameter pack of indices.
56/// @param tuple The tuple-ish to iterate over.
57/// @param operation The operation to perform on each element.
58template <typename Tuple, typename Op, std::size_t... I>
59constexpr void for_each(Tuple&& tuple, Op&& operation, std::index_sequence<I...>) {
60 consume_values({invoke_for_consume(std::forward<Op>(operation), tuple_detail::get<I>(std::forward<Tuple>(tuple)))...}
61 );
62}
63
64// parasoft-end-suppress AUTOSAR-A0_1_4-a
65
66/// @brief The underlying implementation for for_each_index. Not intended to
67/// be called by user code.
68/// @tparam Tuple type implementing the tuple protocol.
69/// @tparam Op The type of operation to perform on each tuple element, which
70/// accepts the index as the first parameter of type std::size_t.
71/// @tparam I The parameter pack of indices.
72/// @param tuple The tuple-ish to iterate over.
73/// @param operation The operation to perform on each element, which should accept
74/// a std::size_t as the first argument for the element's index.
75template <typename Tuple, typename Op, std::size_t... I>
76constexpr void for_each_index(Tuple&& tuple, Op&& operation, std::index_sequence<I...>) {
77 consume_values({invoke_for_consume(
78 std::forward<Op>(operation),
79 std::integral_constant<std::size_t, I>{},
80 tuple_detail::get<I>(std::forward<Tuple>(tuple))
81 )...});
82}
83} // namespace tuple_for_each_detail
84
85/// @brief Perform an operation on each element of a "tuple-ish" type, which
86/// includes std::tuple, std::pair, std::array, or arene::base::array.
87/// @tparam Tuple type implementing the tuple protocol.
88/// @tparam Op The type of operation to perform on each tuple element.
89/// @param tuple The tuple-ish to iterate over.
90/// @param operation The operation to perform on each element.
91///
92/// @note A type implements the tuple protocol if it
93/// * provides a specialization of @c std::tuple_size
94/// * provides a specialization of @c std::tuple_element
95/// * provides a function to get an element by index @c I, which is specified
96/// as:
97/// ** <c> std::forward<Tuple>(tuple).get<I>() </c> if valid
98/// ** otherwise, <c> get<I>(std::forward<Tuple>(tuple)) </c> if valid,
99/// where @c get is looked up by ADL only
100/// Note that the tuple-like concept defines types that satisfy the tuple
101/// protocol but are restricted to a list of types defined in the @c std
102/// namespace (@c std::array, @c std::pair, @c std::tuple in C++14). The term
103/// tuple-ish is used to define types that satisfy the tuple protocol without
104/// the restriction that the type is define in the @c std namespace -- which
105/// includes user-defined types.
106///
107template <typename Tuple, typename Op>
108constexpr void for_each(Tuple&& tuple, Op&& operation) { // CODEQLFP(DCL51-CPP)
109 tuple_for_each_detail::for_each(
110 std::forward<Tuple>(tuple),
111 std::forward<Op>(operation),
112 std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}
113 );
114}
115
116/// @brief Perform an operation on each element of a "tuple-ish" type, which
117/// includes std::tuple, std::pair, std::array, or arene::base::array.
118/// @tparam Tuple type implementing the tuple protocol.
119/// @tparam Op The type of operation to perform on each tuple element, which
120/// accepts the index as the first parameter of type @c std::size_t or
121/// @c std::integral_constant<std::size_t, N>.
122/// @param tuple The tuple-ish to iterate over.
123/// @param operation The operation to perform on each element, which should accept
124/// a @c std::size_t or @c std::integral_constant<std::size_t, N> as the first
125/// argument for the element's index.
126///
127/// @note A type implements the tuple protocol if it
128/// * provides a specialization of @c std::tuple_size
129/// * provides a specialization of @c std::tuple_element
130/// * provides a function to get an element by index @c I, which is specified
131/// as:
132/// ** <c> std::forward<Tuple>(tuple).get<I>() </c> if valid
133/// ** otherwise, <c> get<I>(std::forward<Tuple>(tuple)) </c> if valid,
134/// where @c get is looked up by ADL only
135/// Note that the tuple-like concept defines types that satisfy the tuple
136/// protocol but are restricted to a list of types defined in the @c std
137/// namespace (@c std::array, @c std::pair, @c std::tuple in C++14). The term
138/// tuple-ish is used to define types that satisfy the tuple protocol without
139/// the restriction that the type is defined in the @c std namespace -- which
140/// includes user-defined types.
141///
142template <typename Tuple, typename Op>
143constexpr void for_each_index(Tuple&& tuple, Op&& operation) {
144 tuple_for_each_detail::for_each_index(
145 std::forward<Tuple>(tuple),
146 std::forward<Op>(operation),
147 std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}
148 );
149}
150
151} // namespace base
152} // namespace arene
153
154#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_FOR_EACH_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr void for_each_index(Tuple &&tuple, Op &&operation)
Perform an operation on each element of a "tuple-ish" type, which includes std::tuple,...
Definition for_each.hpp:143
constexpr void for_each(Tuple &&tuple, Op &&operation)
Perform an operation on each element of a "tuple-ish" type, which includes std::tuple,...
Definition for_each.hpp:108
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10