Wayverb
for_each.h
1 #pragma once
2 
3 #include "utilities/tuple_like.h"
4 
5 #include <initializer_list>
6 #include <utility>
7 
8 namespace util {
9 
10 template <typename Func, typename... Ts>
11 void for_each_params(Func&& func, Ts&&... ts) {
12  (void)std::initializer_list<int>{((void)func(std::forward<Ts>(ts)), 0)...};
13 }
14 
15 template <typename Func, typename T, size_t... Ix>
16 void for_each(Func&& func, T&& t, std::index_sequence<Ix...>) {
17  for_each_params(std::forward<Func>(func),
18  tuple_like_getter<Ix>(std::forward<T>(t))...);
19 }
20 
21 template <typename Func, typename T>
22 void for_each(Func&& func, T&& t) {
23  for_each(std::forward<Func>(func),
24  std::forward<T>(t),
25  std::make_index_sequence<
26  tuple_like_size_v<decay_const_ref_t<T>>>{});
27 }
28 
29 template <size_t Ix, typename Func, typename... Ts>
30 constexpr auto apply_at_index(Func&& func, Ts&&... ts) {
31  return func(tuple_like_getter<Ix>(std::forward<Ts>(ts))...);
32 }
33 
34 template <size_t... Ix, typename Func, typename... Ts>
35 void for_each(Func&& func, std::index_sequence<Ix...>, Ts&&... ts) {
36  (void)std::initializer_list<int>{
37  ((void)apply_at_index<Ix>(std::forward<Func>(func),
38  std::forward<Ts>(ts)...),
39  0)...};
40 }
41 
42 template <typename Func, typename T, typename... Ts>
43 void for_each(Func&& func, T&& t, Ts&&... ts) {
44  for_each(
45  std::forward<Func>(func),
46  std::make_index_sequence<tuple_like_size_v<decay_const_ref_t<T>>>{},
47  std::forward<T>(t),
48  std::forward<Ts>(ts)...);
49 }
50 
51 } // namespace util
Definition: allocator.h:6