ForEach.hpp 1.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once

#include <initializer_list>

#include <amdis/common/Apply.hpp>
#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 */ }
    }

    template <class Tuple, class Functor>
    constexpr void for_each(Tuple&& tuple, Functor&& f)
    {
  #if AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      Tools::apply([f=std::move(f)](auto&&... t) { (f(FWD(t)),...); }, tuple);
  #else
      Tools::apply([f=std::move(f)](auto&&... t) {
        Impl_::ignored_evaluation<int>({0, (f(FWD(t)), 0)...});
      }, tuple);
  #endif
    }

    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)
    {
      Tools::for_each(range_t<I0,I1>{}, FWD(f));
    }

    template <std::size_t N, class Functor>
    constexpr void for_range(index_t<N>, Functor&& f)
    {
      Tools::for_each(range_t<0,N>{}, FWD(f));
    }

    template <std::size_t I0, std::size_t I1, class Functor>
    constexpr void for_range(Functor&& f)
    {
      Tools::for_each(range_t<I0,I1>{}, FWD(f));
    }

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