mirror of
https://git.lynn.is/Gwen/argparser.git
synced 2024-04-29 11:13:45 +02:00
31 lines
1 KiB
C++
31 lines
1 KiB
C++
#ifndef ARGPARSER_TUPLE_ITERATION_H
|
|
#define ARGPARSER_TUPLE_ITERATION_H
|
|
|
|
#include <tuple>
|
|
|
|
// adapted from https://blog.tartanllama.xyz/exploding-tuples-fold-expressions/
|
|
|
|
|
|
namespace argparser::internal::tuple_foreach_impl {
|
|
template<std::size_t... Idx>
|
|
auto make_index_dispatcher(std::index_sequence<Idx...>) {
|
|
return [](auto &&f) { (f(std::integral_constant<std::size_t, Idx>{}), ...); };
|
|
}
|
|
template<std::size_t N>
|
|
auto make_index_dispatcher() {
|
|
return make_index_dispatcher(std::make_index_sequence<N>{});
|
|
}
|
|
}// namespace argparser::internal::tuple_foreach_impl
|
|
|
|
namespace argparser::internal {
|
|
template<typename Tuple, typename Func>
|
|
void tuple_foreach(Tuple &&t, Func &&f) {
|
|
constexpr auto n = std::tuple_size<std::decay_t<Tuple>>::value;
|
|
auto dispatcher = internal::tuple_foreach_impl::make_index_dispatcher<n>();
|
|
dispatcher([&f, &t](auto idx) { f(std::get<idx>(std::forward<Tuple>(t)), idx); });
|
|
}
|
|
|
|
}// namespace argparser::internal
|
|
|
|
#endif//ARGPARSER_TUPLE_ITERATION_H
|