1 /*******************************************************************************
2 * tlx/meta/call_foreach_tuple_with_index.hpp
3 *
4 * Part of tlx - http://panthema.net/tlx
5 *
6 * Copyright (C) 2016-2017 Timo Bingmann <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
11 #ifndef TLX_META_CALL_FOREACH_TUPLE_WITH_INDEX_HEADER
12 #define TLX_META_CALL_FOREACH_TUPLE_WITH_INDEX_HEADER
13
14 #include <tuple>
15
16 #include <tlx/meta/call_foreach_with_index.hpp>
17 #include <tlx/meta/index_sequence.hpp>
18
19 namespace tlx {
20
21 //! \addtogroup tlx_meta
22 //! \{
23
24 /******************************************************************************/
25 // Variadic Template Expander: run a generic templated functor (like a generic
26 // lambda) for each component of a std::tuple.
27 //
28 // Called with func(StaticIndex<> index, Argument arg).
29
30 namespace meta_detail {
31
32 //! helper for call_foreach_tuple_with_index
33 template <typename Functor, typename Tuple, std::size_t... Is>
call_foreach_tuple_with_index_impl(Functor && f,Tuple && t,index_sequence<Is...>)34 void call_foreach_tuple_with_index_impl(
35 Functor&& f, Tuple&& t, index_sequence<Is...>) {
36 return call_foreach_with_index(
37 std::forward<Functor>(f), std::get<Is>(std::forward<Tuple>(t)) ...);
38 }
39
40 } // namespace meta_detail
41
42 //! Call a generic functor (like a generic lambda) to each components of a tuple
43 //! together with its zero-based index.
44 template <typename Functor, typename Tuple>
call_foreach_tuple_with_index(Functor && f,Tuple && t)45 void call_foreach_tuple_with_index(Functor&& f, Tuple&& t) {
46 using Indices = make_index_sequence<
47 std::tuple_size<typename std::decay<Tuple>::type>::value>;
48 meta_detail::call_foreach_tuple_with_index_impl(
49 std::forward<Functor>(f), std::forward<Tuple>(t), Indices());
50 }
51
52 //! \}
53
54 } // namespace tlx
55
56 #endif // !TLX_META_CALL_FOREACH_TUPLE_WITH_INDEX_HEADER
57
58 /******************************************************************************/
59