tlx
vmap_for_range.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/meta/vmap_for_range.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_VMAP_FOR_RANGE_HEADER
12 #define TLX_META_VMAP_FOR_RANGE_HEADER
13 
14 #include <tuple>
15 #include <utility>
16 
18 
19 namespace tlx {
20 
21 //! \addtogroup tlx_meta
22 //! \{
23 
24 /******************************************************************************/
25 // Variadic Template Enumerate Mapper: run a generic templated functor (like a
26 // generic lambda) for each value from [Begin,End), and collect the return
27 // values in a generic std::tuple.
28 //
29 // Called with func(StaticIndex<> index).
30 
31 namespace meta_detail {
32 
33 //! helper for vmap_for_range: general recursive case
34 template <size_t Index, size_t Size, typename Functor>
36 {
37 public:
38  static auto call(Functor&& f) {
39  // call this index before recursion
40  auto x = std::forward<Functor>(f)(StaticIndex<Index>());
41  return std::tuple_cat(
42  std::make_tuple(std::move(x)),
44  std::forward<Functor>(f)));
45  }
46 };
47 
48 //! helper for vmap_for_range: base case
49 template <size_t Index, typename Functor>
50 class VMapForRangeImpl<Index, 0, Functor>
51 {
52 public:
53  static auto call(Functor&& /* f */) {
54  return std::tuple<>();
55  }
56 };
57 
58 } // namespace meta_detail
59 
60 //! Vmap a generic functor (like a generic lambda) for the integers [0,Size).
61 template <size_t Size, typename Functor>
62 auto vmap_for_range(Functor&& f) {
64  std::forward<Functor>(f));
65 }
66 
67 //! Vmap a generic functor (like a generic lambda) for the integers [Begin,End).
68 template <size_t Begin, size_t End, typename Functor>
69 auto vmap_for_range(Functor&& f) {
71  std::forward<Functor>(f));
72 }
73 
74 //! \}
75 
76 } // namespace tlx
77 
78 #endif // !TLX_META_VMAP_FOR_RANGE_HEADER
79 
80 /******************************************************************************/
Helper for call_foreach_with_index() to save the index as a compile-time index.
helper for vmap_for_range: general recursive case
auto vmap_for_range(Functor &&f)
Vmap a generic functor (like a generic lambda) for the integers [0,Size).
static auto call(Functor &&f)