ForEach.hpp 2.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#include <initializer_list>

#include <amdis/common/Index.hpp>
#include <amdis/common/Range.hpp>

namespace AMDiS
{
  namespace Tools
  {
    namespace Impl_
    {
      template <class T>
      void ignored_evaluation(std::initializer_list<T>&&) { /* do nothing */ }
    }

18
19
20
21
22
23
24
25
26
27
28
    template <class Functor, class... Args>
    constexpr void for_variadic(Functor&& f, Args&&... args)
    {
      using std::get;
#if AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      (f(FWD(args)),...);
#else
      Impl_::ignored_evaluation<int>({0, (f(FWD(args)), 0)...});
#endif
    }

29
30
31
32
33
34
35
36
37
38
39
40
41
    template <std::size_t... I, class Tuple, class Functor>
    constexpr void for_each(std::index_sequence<I...>, Tuple&& tuple, Functor&& f)
    {
      using std::get;
#if AMDIS_HAS_EXPANSION_STATEMENTS
      for... (auto&& t : tuple) { f(FWD(t)); }
#elif AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      (f(get<I>(tuple)),...);
#else
      Impl_::ignored_evaluation<int>({0, (f(get<I>(tuple)), 0)...});
#endif
    }

42
43
44
    template <class Tuple, class Functor>
    constexpr void for_each(Tuple&& tuple, Functor&& f)
    {
45
46
47
48
49
50
51
52
53
54
55
56
      Tools::for_each(std::make_index_sequence<Size_v<std::remove_reference_t<Tuple>>>{}, FWD(tuple), FWD(f));
    }


    template <std::size_t I0 = 0, std::size_t... I, class Functor>
    constexpr void for_range(std::index_sequence<I...>, Functor&& f)
    {
#if AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      (f(index_t<I0+I>{}),...);
#else
      Impl_::ignored_evaluation<int>({0, (f(index_t<I0+I>{}), 0)...});
#endif
57
58
59
60
61
    }

    template <std::size_t I0, std::size_t I1, class Functor>
    constexpr void for_range(index_t<I0> i0, index_t<I1> i1, Functor&& f)
    {
62
      Tools::for_range<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
63
64
65
66
67
    }

    template <std::size_t N, class Functor>
    constexpr void for_range(index_t<N>, Functor&& f)
    {
68
      Tools::for_range(std::make_index_sequence<N>{}, FWD(f));
69
70
71
72
73
    }

    template <std::size_t I0, std::size_t I1, class Functor>
    constexpr void for_range(Functor&& f)
    {
74
      Tools::for_range<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
75
76
77
78
    }

  } // end namespace Tools
} // end namespace AMDiS