GCC Code Coverage Report


Directory: include/kumi/
File: include/kumi/algorithm/for_each.hpp
Date: 2026-10-14 16:33:56
Exec Total Coverage
Lines: 18 18 100.0%
Functions: 224 224 100.0%
Branches: 9 9 100.0%

Line Branch Exec Source
1 //======================================================================================================================
2 /*
3 KUMI - Compact Tuple Tools
4 Copyright : KUMI Project Contributors
5 SPDX-License-Identifier: BSL-1.0
6 */
7 //======================================================================================================================
8 #pragma once
9
10 namespace kumi
11 {
12 namespace _
13 {
14 template<kumi::_::case_ Case, typename F, typename T, typename... Ts>
15 374 KUMI_HIDDEN_ABI constexpr void for_each_switch(auto N, F f, T&& t, Ts&&... ts)
16 {
17 if constexpr (Case == kumi::_::case_::normal)
18 {
19 if constexpr (kumi::concepts::record_type<T>)
20 {
21 constexpr auto field = kumi::identifier_of<kumi::element_t<N, T>>();
22 38 kumi::invoke(f, get<field>(KUMI_FWD(t)), get<field>(KUMI_FWD(ts))...);
23 }
24 200 else kumi::invoke(f, get<N>(KUMI_FWD(t)), get<N>(KUMI_FWD(ts))...);
25 }
26 else if constexpr (Case == kumi::_::case_::indexed)
27 {
28
1/1
✓ Branch 2 taken 1 times.
132 kumi::invoke(f, N, get<N>(KUMI_FWD(t)), get<N>(KUMI_FWD(ts))...);
29 }
30 else if constexpr (Case == kumi::_::case_::field)
31 {
32 constexpr auto field = kumi::identifier_of<kumi::element_t<N, T>>();
33
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
40 kumi::invoke(f, kumi::_::make_str(field), get<field>(KUMI_FWD(t)), get<field>(KUMI_FWD(ts))...);
34 }
35 374 }
36
37 template<kumi::_::case_ Case, typename F, std::size_t... I, typename T, typename... Ts>
38 59 KUMI_HIDDEN_ABI constexpr void for_each_(kumi::_::adl_tag_t, F&& f, std::index_sequence<I...>, T&& t, Ts&&... ts)
39 {
40 if constexpr (sizeof...(I) == 0) return;
41 59 else ((for_each_switch<Case>(kumi::index<I>, KUMI_FWD(f), KUMI_FWD(t), KUMI_FWD(ts)...)), ...);
42 59 }
43 }
44
45 struct for_each_t
46 {
47 template<typename Function, kumi::concepts::product_type T, kumi::concepts::product_type... Ts>
48 KUMI_ABI constexpr void operator()(Function f, T&& t, Ts&&... ts) const
49 requires(kumi::concepts::compatible_product_types<T, Ts...>) && (kumi::_::supports_call<Function&, T, Ts...>)
50 {
51 2 if constexpr (kumi::concepts::empty_product_type<T>) return;
52 else
53
2/2
✓ Branch 1 taken 17 times.
✓ Branch 4 taken 9 times.
26 for_each_<kumi::_::case_::normal>(kumi::_::adl_tag, f, kumi::function::indexes_for<T>{}, KUMI_FWD(t),
54 KUMI_FWD(ts)...);
55 26 }
56 };
57
58 struct for_each_index_t
59 {
60 template<typename Function, kumi::concepts::product_type T, kumi::concepts::product_type... Ts>
61 KUMI_ABI constexpr void operator()(Function f, T&& t, Ts&&... ts) const
62 requires(!kumi::concepts::record_type<T> && (!kumi::concepts::record_type<Ts> && ...))
63 {
64 1 if constexpr (kumi::concepts::empty_product_type<T>) return;
65 else
66
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 1 times.
6 for_each_<kumi::_::case_::indexed>(kumi::_::adl_tag, f, kumi::function::indexes_for<T>{}, KUMI_FWD(t),
67 KUMI_FWD(ts)...);
68 6 }
69 };
70
71 struct for_each_field_t
72 {
73 template<typename Function, kumi::concepts::record_type R, kumi::concepts::record_type... Rs>
74 KUMI_ABI constexpr void operator()(Function f, R&& r, Rs&&... rs) const
75 requires(kumi::concepts::compatible_product_types<R, Rs...>)
76 {
77 1 if constexpr (kumi::concepts::empty_product_type<R>) return;
78 else
79
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 1 times.
5 for_each_<kumi::_::case_::field>(kumi::_::adl_tag, f, kumi::function::indexes_for<R>{}, KUMI_FWD(r),
80 KUMI_FWD(rs)...);
81 5 }
82 };
83
84 //====================================================================================================================
85 /**
86 @ingroup kumi_transforms
87
88 @var for_each
89 @brief Callable object applying the Callable object f on each element of a product type.
90
91 `f` is applied on the values if the given product_type is a record type.
92
93 @note This function does not take part in overload resolution if `f` can't be applied to the
94 elements of `t` and/or `ts`, or if the product types are not compatible.
95
96 @see kumi::concepts::compatible_product_types
97
98 @qualifier inline
99 @qualifier constexpr
100
101 @groupheader{Header file}
102 @code
103 #include <kumi/algorithm/for_each.hpp>
104 @endcode
105
106 @groupheader{Call Signature}
107
108 @code
109 template<typename Function, product_type T, product_type... Ts>
110 constexpr decltype(auto) for_each(Function && f, T && t, Ts &&... ts);
111 @endcode
112
113 @subgroupheader{Parameters}
114
115 - `f`: Callable object to be invoked
116 - `t`: Product Type whose elements are used as arguments to f
117 - `ts`: Other product types whose elements are used as arguments to f
118
119 @see kumi::for_each_index
120 @see kumi::for_each_field
121
122 @groupheader{Examples}
123
124 @tab_begin
125
126 @tab{Tuple}
127 @godbolt{doc/tuple/algo/for_each.cpp}
128
129 @tab{Record}
130 @godbolt{doc/record/algo/for_each.cpp}
131
132 @tab_end
133 **/
134 //====================================================================================================================
135 KUMI_VARIABLE_ABI constexpr for_each_t for_each{};
136
137 //====================================================================================================================
138 /**
139 @ingroup kumi_tuple_transforms
140
141 @var for_each_index
142 @brief Callable object applying the Callable object f on each element of a product type and its index.
143
144 @note This function does not take part in overload resolution if `f` can't be applied to the
145 elements of `t` and/or `ts` and an integral constant. This function cannot be applied
146 on record types.
147
148 @qualifier inline
149 @qualifier constexpr
150
151 @groupheader{Header file}
152 @code
153 #include <kumi/algorithm/for_each.hpp>
154 @endcode
155
156 @groupheader{Call Signature}
157
158 @code
159 template<typename Function, product_type T, product_type... Ts>
160 constexpr decltype(auto) for_each_index(Function && f, T && t, Ts &&... ts);
161 @endcode
162
163 @subgroupheader{Parameters}
164
165 - `f`: Callable object to be invoked
166 - `t`: Product Type whose elements are used as arguments to f
167 - `ts`: Other product types whose elements are used as arguments to f
168
169 @see kumi::for_each
170 @see kumi::for_each_field
171
172 @groupheader{Examples}
173
174 @godbolt{doc/tuple/algo/for_each_index.cpp}
175 **/
176 //====================================================================================================================
177 KUMI_VARIABLE_ABI constexpr for_each_index_t for_each_index{};
178
179 //====================================================================================================================
180 /**
181 @ingroup kumi_record_transforms
182
183 @var for_each_field
184 @brief Callable object applying the Callable object f on each element of a product type and its field.
185
186 @note This function does not take part in overload resolution if `f` can't be applied to the
187 elements of `t` and/or `ts` and a field or if the product types are not compatible.
188 @see kumi::concepts::compatible_product_types
189
190
191 This function can only be applied to record types.
192 The function needs to be defined to handle types modeling kumi::concepts::field.
193
194 @qualifier inline
195 @qualifier constexpr
196
197 @groupheader{Header file}
198 @code
199 #include <kumi/algorithm/for_each.hpp>
200 @endcode
201
202 @groupheader{Call Signature}
203
204 @code
205 template<typename Function, product_type T, product_type... Ts>
206 constexpr decltype(auto) for_each_field(Function && f, T && t, Ts &&... ts);
207 @endcode
208
209 @subgroupheader{Parameters}
210
211 - `f`: Callable object to be invoked
212 - `t`: Product Type whose elements are used as arguments to f
213 - `ts`: Other product types whose elements are used as arguments to f
214
215 @see kumi::for_each
216 @see kumi::for_each_index
217
218 @groupheader{Examples}
219
220 @godbolt{doc/tuple/algo/for_each_field.cpp}
221 **/
222 //====================================================================================================================
223 KUMI_VARIABLE_ABI constexpr for_each_field_t for_each_field{};
224 }
225