1 #if !defined( INCLUDED_FUNCTIONAL_H )
2 #define INCLUDED_FUNCTIONAL_H
10 struct rank : rank<N - 1> {
25 using func_member = wrapper<typename F::func>;
28 static wrapper<func_member<F>> test(rank<2>) { return {}; }
32 using type = typename func_lambda<decltype(&F::operator())>::type;
35 template<class R, class... Ts>
36 struct func_lambda<R(*)(Ts...)> {
37 using type = R(Ts...);
40 template<class Object, class R, class... Ts>
41 struct func_lambda<R(Object::*)(Ts...) const> {
42 using type = R(Ts...);
45 template<class Object, class R, class... Ts>
46 struct func_lambda<R(Object::*)(Ts...)> {
47 using type = R(Ts...);
50 template<class F, class = func_lambda<F>>
51 static wrapper<func_lambda<F>> test(rank<1>) { return {}; }
57 template<class R, class... Ts>
59 using result_type = R;
62 using get = typename std::tuple_element<N, std::tuple<Ts...>>::type;
66 template<class Caller>
67 using get_func = typename decltype(detail::get_func::test<Caller>(detail::rank<2>{}))::type::type;
69 template<class Caller>
70 using get_result_type = typename detail::Fn<get_func<Caller>>::result_type;
72 template<class Caller, int N>
73 using get_argument = typename detail::Fn<get_func<Caller>>::template get<N>;
76 template<class Object, class F>
77 struct MemberFunction;
79 template<class Object, class R, class... Ts>
80 struct MemberFunction<Object, R(Ts...)> {
81 using type = R(Object::*)(Ts...);
82 using type_const = R(Object::*)(Ts...) const;
86 template<class Object, class F>
87 using MemberFunction = typename detail::MemberFunction<Object, F>::type;
89 template<class Object, class F>
90 using ConstMemberFunction = typename detail::MemberFunction<Object, F>::type_const;
92 template<class Object, class F>
95 template<class Object, class R, class... Ts>
96 class MemberN<Object, R(Ts...)> {
98 template<R(Object::*f)(Ts...)>
101 using func = R(Object &, Ts...);
103 static R call(Object &object, Ts... args) {
104 return (object.*f)(args...);
109 template<class Object, class F, MemberFunction<Object, F> func>
110 using Member = typename MemberN<Object, F>::template instance<func>;
112 template<class Object, class F>
115 template<class Object, class R, class... Ts>
116 class ConstMemberN<Object, R(Ts...)> {
118 template<R(Object::*f)(Ts...) const>
121 using func = R(const Object &, Ts...);
123 static R call(const Object &object, Ts... args) {
124 return (object.*f)(args...);
129 template<class Object, class F, ConstMemberFunction<Object, F> func>
130 using ConstMember = typename ConstMemberN<Object, F>::template instance<func>;
135 template<class R, class... Ts>
136 class FunctionN<R(Ts...)> {
138 template<R(*f)(Ts...)>
141 using func = R(Ts...);
143 static R call(Ts... args) {
149 template<class F, F *func>
150 using Function = typename FunctionN<F>::template instance<func>;
152 template<class Caller, class F>
153 class CallerShiftFirst;
155 template<class Caller, class R, class FirstArgument, class... Ts>
156 class CallerShiftFirst<Caller, R(FirstArgument, Ts...)> {
158 using func = R(FirstArgument, Ts...);
160 static R call(FirstArgument, Ts... args) {
161 return Caller::call(args...);
165 template<class Functor, class F>
166 class FunctorNInvoke;
173 template<int N, int... S>
174 struct gens : gens<N - 1, N - 1, S...> {
178 struct gens<0, S...> {
179 using type = seq<S...>;
183 using seq_new = typename gens<N>::type;
186 template<class Functor, class R, class... Ts>
187 class FunctorNInvoke<Functor, R(Ts...)> {
188 std::tuple<Ts...> args;
194 struct caller<detail::seq<I...>> {
195 static inline R call(FunctorNInvoke<Functor, R(Ts...)> *self, Functor functor) {
197 return functor(std::get<I>(self->args)...);
202 FunctorNInvoke(Ts... args) : args(args...) {
205 inline R operator()(Functor functor) {
206 return caller<detail::seq_new<sizeof...(Ts)>>::call(this, functor);
210 template<class Functor>
211 using FunctorInvoke = FunctorNInvoke<Functor, get_func<Functor>>;