Apply.hpp 1.62 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
51
52
53
54
55
56
57
58
59
#pragma once

#include <tuple>
#include <type_traits>
#include <utility>

#include <amdis/common/Mpl.hpp>

namespace AMDiS
{
  namespace Tools
  {
    namespace Impl_
    {
      template <class F, class Tuple, std::size_t... I>
      constexpr decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>)
      {
        return f(std::get<I>(std::forward<Tuple>(t))...);
      }

      template <class F, std::size_t I0, std::size_t... I>
      constexpr decltype(auto) apply_indices_impl(F&& f, index_t<I0>, std::index_sequence<I...>)
      {
        return f(index_t<I0+I>{}...);
      }
    }  // namespace Impl_

    template <class F, class Tuple>
    constexpr decltype(auto) apply(F&& f, Tuple&& t)
    {
        return Impl_::apply_impl(
            std::forward<F>(f), std::forward<Tuple>(t),
            std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{});
    }

    template <class F, class... Args>
    constexpr decltype(auto) apply_variadic(F&& f, Args&&... args)
    {
        return apply(std::forward<F>(f), std::forward_as_tuple(args...));
    }

    template <class F, std::size_t N>
    constexpr decltype(auto) apply_indices(F&& f, index_t<N>)
    {
      return Impl_::apply_indices_impl(
          std::forward<F>(f),
          index_t<0>{},
          std::make_index_sequence<N>{});
    }

    template <class F, std::size_t I0, std::size_t I1>
    constexpr decltype(auto) apply_indices(F&& f, index_t<I0>, index_t<I1>)
    {
      return Impl_::apply_indices_impl(
          std::forward<F>(f), index_t<I0>{},
          std::make_index_sequence<I1-I0>{});
    }
  }
} // end namespace AMDiS