Thrill  0.1
call_foreach_with_index.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/meta/call_foreach_with_index.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2016-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_META_CALL_FOREACH_WITH_INDEX_HEADER
12 #define TLX_META_CALL_FOREACH_WITH_INDEX_HEADER
13 
14 #include <utility>
15 
17 
18 namespace tlx {
19 
20 //! \addtogroup tlx_meta
21 //! \{
22 
23 /******************************************************************************/
24 // Variadic Template Expander: run a generic templated functor (like a generic
25 // lambda) for each of the variadic template parameters.
26 //
27 // Called with func(StaticIndex<> index, Argument arg).
28 
29 namespace meta_detail {
30 
31 //! helper for call_foreach_with_index: base case
32 template <size_t Index, typename Functor, typename Arg>
33 void call_foreach_with_index_impl(Functor&& f, Arg&& arg) {
34  std::forward<Functor>(f)(StaticIndex<Index>(), std::forward<Arg>(arg));
35 }
36 
37 //! helper for call_foreach_with_index: general recursive case
38 template <size_t Index, typename Functor, typename Arg, typename... MoreArgs>
39 void call_foreach_with_index_impl(Functor&& f, Arg&& arg, MoreArgs&& ... rest) {
40  std::forward<Functor>(f)(StaticIndex<Index>(), std::forward<Arg>(arg));
41  call_foreach_with_index_impl<Index + 1>(
42  std::forward<Functor>(f), std::forward<MoreArgs>(rest) ...);
43 }
44 
45 } // namespace meta_detail
46 
47 //! Call a generic functor (like a generic lambda) for each variadic template
48 //! argument together with its zero-based index.
49 template <typename Functor, typename... Args>
50 void call_foreach_with_index(Functor&& f, Args&& ... args) {
51  meta_detail::call_foreach_with_index_impl<0>(
52  std::forward<Functor>(f), std::forward<Args>(args) ...);
53 }
54 
55 //! \}
56 
57 } // namespace tlx
58 
59 #endif // !TLX_META_CALL_FOREACH_WITH_INDEX_HEADER
60 
61 /******************************************************************************/
void call_foreach_with_index(Functor &&f, Args &&... args)
void call_foreach_with_index_impl(Functor &&f, Arg &&arg)
helper for call_foreach_with_index: base case