aboutsummaryrefslogtreecommitdiff
path: root/src/function/detail/apply.h
blob: 3df80efa9d39b0ed4c811118c79abddeeaa4f434 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef TYPEASVALUE_SRC_FUNCTION_DETAIL_APPLY_H_
#define TYPEASVALUE_SRC_FUNCTION_DETAIL_APPLY_H_

#include <type_traits>

#include "list/list.h"
#include "list/operation/nth.h"
#include "list/operation/higher/query.h"

namespace tav {
namespace detail {

struct placeholder_tag { };

template <typename Type>
using is_placeholder = Boolean<
	std::is_base_of<placeholder_tag, Type>::value
>;

template <int Index>
struct placeholder : placeholder_tag { };

template <
	typename Partials,
	typename Argument
>
struct resolve_placeholder {
	typedef Argument type;
};

template <
	typename Partials,
	int      Index
>
struct resolve_placeholder<Partials, placeholder<Index>> {
	typedef tav::Nth<Size<Index>, Partials> type;
};

template <typename... Arguments>
using count_placeholders = Count<is_placeholder, List<Arguments...>>;

template <
	template<typename...> class Function,
	typename...                 Arguments
>
struct apply_none {
	using function = Function<Arguments...>;
};

template <
	template<typename...> class Function,
	typename...                 Arguments
>
struct apply_variadic {
	template <typename... Partials>
	using function = Function<
		Eval<resolve_placeholder<
			List<Partials...>,
			Arguments
		>>...
	>;
};

template <
	template<typename...> class Function,
	typename...                 Arguments
>
struct apply_single : apply_variadic<Function, Arguments...> {
	template <typename Partial0>
	using function = typename apply_variadic<
		Function,
		Arguments...
	>::template function<Partial0>;
};

template <
	template<typename...> class Function,
	typename...                 Arguments
>
struct apply_pair : apply_variadic<Function, Arguments...> {
	template <typename Partial0, typename Partial1>
	using function = typename apply_variadic<
		Function,
		Arguments...
	>::template function<Partial0, Partial1>;
};

}
}

#endif  // TYPEASVALUE_SRC_FUNCTION_DETAIL_APPLY_H_