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 
16 #include <tlx/meta/static_index.hpp>
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>
33 class CallForRangeImpl
34 {
35 public:
call(Functor && f)36     static void call(Functor&& f) {
37         std::forward<Functor>(f)(StaticIndex<Index>());
38         CallForRangeImpl<Index + 1, Size - 1, Functor>::call(
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:
call(Functor &&)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>
call_for_range(Functor && f)55 void call_for_range(Functor&& f) {
56     meta_detail::CallForRangeImpl<0, Size, Functor>::call(
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>
call_for_range(Functor && f)62 void call_for_range(Functor&& f) {
63     meta_detail::CallForRangeImpl<Begin, End - Begin, Functor>::call(
64         std::forward<Functor>(f));
65 }
66 
67 //! \}
68 
69 } // namespace tlx
70 
71 #endif // !TLX_META_CALL_FOR_RANGE_HEADER
72 
73 /******************************************************************************/
74