Apply.hpp 2.13 KB
Newer Older
1
2
3
4
5
6
#pragma once

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

7
8
#include <amdis/common/Index.hpp>
#include <amdis/common/StaticSize.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
9
#include <amdis/common/TypeTraits.hpp>
10
11
12

namespace AMDiS
{
13
  namespace Ranges
14
15
16
  {
    namespace Impl_
    {
17
      template <class Functor, class Tuple, std::size_t... I>
18
      constexpr decltype(auto) applyImpl(Functor&& f, Tuple&& t, std::index_sequence<I...>)
19
      {
20
21
        using std::get;
        return f(get<I>(FWD(t))...);
22
23
      }

24
      template <class Functor, std::size_t I0, std::size_t... I>
25
      constexpr decltype(auto) applyIndicesImpl(Functor&& f, index_t<I0>, std::index_sequence<I...>)
26
27
28
29
30
31
32
33
      {
        return f(index_t<I0+I>{}...);
      }
    }  // namespace Impl_

    template <class F, class Tuple>
    constexpr decltype(auto) apply(F&& f, Tuple&& t)
    {
34
      return Impl_::applyImpl(FWD(f), FWD(t),
35
          std::make_index_sequence<static_size_v<Tuple>>{});
36
37
    }

38
    template <class Functor, class... Args>
39
    constexpr decltype(auto) applyVariadic(Functor&& f, Args&&... args)
40
    {
41
42
      return Impl_::applyImpl(FWD(f), std::forward_as_tuple(args...),
          std::make_index_sequence<sizeof...(Args)>{});
43
44
    }

45
    template <std::size_t N, class Functor>
46
    constexpr decltype(auto) applyIndices(Functor&& f)
47
    {
48
      return Impl_::applyIndicesImpl(FWD(f), index_t<0>{},
49
50
51
          std::make_index_sequence<N>{});
    }

52
53
54
55
56
57
58
    template <std::size_t I0, std::size_t I1, class Functor>
    constexpr decltype(auto) applyIndices(Functor&& f)
    {
      return Impl_::applyIndicesImpl(FWD(f), index_t<I0>{},
          std::make_index_sequence<I1-I0>{});
    }

59
    template <class Functor, std::size_t N>
60
    constexpr decltype(auto) applyIndices(Functor&& f, index_t<N>)
61
    {
62
      return Impl_::applyIndicesImpl(FWD(f), index_t<0>{},
63
64
65
          std::make_index_sequence<N>{});
    }

66
    template <class Functor, std::size_t I0, std::size_t I1>
67
    constexpr decltype(auto) applyIndices(Functor&& f, index_t<I0>, index_t<I1>)
68
    {
69
      return Impl_::applyIndicesImpl(FWD(f), index_t<I0>{},
70
71
          std::make_index_sequence<I1-I0>{});
    }
72
73

  } // end namespace Ranges
74
} // end namespace AMDiS