Wayverb
apply.h
1 #pragma once
2 
3 #include "utilities/tuple_like.h"
4 
5 #include <initializer_list>
6 #include <tuple>
7 #include <utility>
8 
9 namespace util {
10 
11 template <typename Func, typename... Ts>
12 constexpr auto apply_params(Func&& func, Ts&&... ts) {
13  return func(std::forward<Ts>(ts)...);
14 }
15 
16 template <typename Func, typename T, size_t... Ix>
17 constexpr auto apply(Func&& func, T&& t, std::index_sequence<Ix...>) {
18  return apply_params(std::forward<Func>(func),
19  tuple_like_getter<Ix>(std::forward<T>(t))...);
20 }
21 
24 template <typename Callback, typename Collection>
25 constexpr auto apply(Callback&& callback, Collection&& c) {
26  return apply(std::forward<Callback>(callback),
27  std::forward<Collection>(c),
28  std::make_index_sequence<
29  tuple_like_size_v<decay_const_ref_t<Collection>>>{});
30 }
31 
33 
34 template <typename Callbacks, typename Collection, size_t... Ix>
35 constexpr auto apply_each(Callbacks&& callbacks,
36  const Collection& c,
37  std::index_sequence<Ix...>) {
38  return std::make_tuple(apply(tuple_like_getter<Ix>(callbacks), c)...);
39 }
40 
46 template <typename Callbacks, typename Collection>
47 constexpr auto apply_each(Callbacks&& callbacks, const Collection& c) {
48  return apply_each(
49  std::forward<Callbacks>(callbacks),
50  c,
51  std::make_index_sequence<
52  tuple_like_size_v<decay_const_ref_t<Callbacks>>>{});
53 }
54 
55 template <typename Callbacks, size_t... Ix>
56 constexpr auto apply_each(Callbacks&& callbacks, std::index_sequence<Ix...>) {
57  return std::make_tuple(tuple_like_getter<Ix>(callbacks)()...);
58 }
59 
60 template <typename Callbacks>
61 constexpr auto apply_each(Callbacks&& callbacks) {
62  return apply_each(
63  std::forward<Callbacks>(callbacks),
64  std::make_index_sequence<
65  tuple_like_size_v<decay_const_ref_t<Callbacks>>>{});
66 }
67 
69 
70 template <typename Callbacks, typename Collection, size_t... Ix>
71 void call_each(Callbacks&& callbacks,
72  const Collection& c,
73  std::index_sequence<Ix...>) {
74  (void)std::initializer_list<int>{
75  ((void)apply(tuple_like_getter<Ix>(callbacks), c), 0)...};
76 }
77 
82 template <typename Callbacks, typename Collection>
83 void call_each(Callbacks&& callbacks, const Collection& c) {
84  call_each(std::forward<Callbacks>(callbacks),
85  c,
86  std::make_index_sequence<
87  tuple_like_size_v<decay_const_ref_t<Callbacks>>>{});
88 }
89 
90 template <typename Callbacks, size_t... Ix>
91 void call_each(Callbacks&& callbacks, std::index_sequence<Ix...>) {
92  (void)std::initializer_list<int>{
93  ((void)tuple_like_getter<Ix>(callbacks)(), 0)...};
94 }
95 
96 template <typename Callbacks>
97 void call_each(Callbacks&& callbacks) {
98  call_each(std::forward<Callbacks>(callbacks),
99  std::make_index_sequence<
100  tuple_like_size_v<decay_const_ref_t<Callbacks>>>{});
101 }
102 
103 } // namespace util
Definition: allocator.h:6