tlx
call_for_range.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/meta/call_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_CALL_FOR_RANGE_HEADER
12 #define TLX_META_CALL_FOR_RANGE_HEADER
13 
14 #include <utility>
15 
17 
18 namespace tlx {
19 
20 //! \addtogroup tlx_meta
21 //! \{
22 
23 /******************************************************************************/
24 // Variadic Template Enumerator: run a generic templated functor (like a generic
25 // lambda) for the integers 0 .. Size-1 or more general [Begin,End).
26 //
27 // Called with func(StaticIndex<> index).
28 
29 namespace meta_detail {
30 
31 //! helper for call_for_range: general recursive case
32 template <size_t Index, size_t Size, typename Functor>
34 {
35 public:
36  static void call(Functor&& f) {
37  std::forward<Functor>(f)(StaticIndex<Index>());
39  std::forward<Functor>(f));
40  }
41 };
42 
43 //! helper for call_for_range: base case
44 template <size_t Index, typename Functor>
45 class CallForRangeImpl<Index, 0, Functor>
46 {
47 public:
48  static void call(Functor&& /* f */) { }
49 };
50 
51 } // namespace meta_detail
52 
53 //! Call a generic functor (like a generic lambda) for the integers [0,Size).
54 template <size_t Size, typename Functor>
55 void call_for_range(Functor&& f) {
57  std::forward<Functor>(f));
58 }
59 
60 //! Call a generic functor (like a generic lambda) for the integers [Begin,End).
61 template <size_t Begin, size_t End, typename Functor>
62 void call_for_range(Functor&& f) {
64  std::forward<Functor>(f));
65 }
66 
67 //! \}
68 
69 } // namespace tlx
70 
71 #endif // !TLX_META_CALL_FOR_RANGE_HEADER
72 
73 /******************************************************************************/
helper for call_for_range: general recursive case
void call_for_range(Functor &&f)
Call a generic functor (like a generic lambda) for the integers [0,Size).
Helper for call_foreach_with_index() to save the index as a compile-time index.
static void call(Functor &&f)