Copyright 2012 -> 2013
[folly.git] / folly / ApplyTuple.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Defines a function folly::applyTuple, which takes a function and a
19  * std::tuple of arguments and calls the function with those
20  * arguments.
21  *
22  * Example:
23  *
24  *    int x = folly::applyTuple(std::plus<int>(), std::make_tuple(12, 12));
25  *    ASSERT(x == 24);
26  */
27
28 #ifndef FOLLY_APPLYTUPLE_H_
29 #define FOLLY_APPLYTUPLE_H_
30
31 #include <tuple>
32 #include <functional>
33 #include <type_traits>
34
35 namespace folly {
36
37 //////////////////////////////////////////////////////////////////////
38
39 namespace detail {
40
41 // This is to allow using this with pointers to member functions,
42 // where the first argument in the tuple will be the this pointer.
43 template<class F> F& makeCallable(F& f) { return f; }
44 template<class R, class C, class ...A>
45 auto makeCallable(R (C::*d)(A...)) -> decltype(std::mem_fn(d)) {
46   return std::mem_fn(d);
47 }
48
49 template<class Tuple>
50 struct DerefSize
51   : std::tuple_size<typename std::remove_reference<Tuple>::type>
52 {};
53
54 // CallTuple recursively unpacks tuple arguments so we can forward
55 // them into the function.
56 template<class Ret>
57 struct CallTuple {
58   template<class F, class Tuple, class ...Unpacked>
59   static typename std::enable_if<
60     (sizeof...(Unpacked) < DerefSize<Tuple>::value),
61     Ret
62   >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
63     typedef typename std::tuple_element<
64       sizeof...(Unpacked),
65       typename std::remove_reference<Tuple>::type
66     >::type ElementType;
67     return CallTuple<Ret>::call(f, std::forward<Tuple>(t),
68       std::forward<Unpacked>(unp)...,
69       std::forward<ElementType>(std::get<sizeof...(Unpacked)>(t))
70     );
71   }
72
73   template<class F, class Tuple, class ...Unpacked>
74   static typename std::enable_if<
75     (sizeof...(Unpacked) == DerefSize<Tuple>::value),
76     Ret
77   >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
78     return makeCallable(f)(std::forward<Unpacked>(unp)...);
79   }
80 };
81
82 // The point of this meta function is to extract the contents of the
83 // tuple as a parameter pack so we can pass it into std::result_of<>.
84 template<class F, class Args> struct ReturnValue {};
85 template<class F, class ...Args>
86 struct ReturnValue<F,std::tuple<Args...>> {
87   typedef typename std::result_of<F (Args...)>::type type;
88 };
89
90 }
91
92 //////////////////////////////////////////////////////////////////////
93
94 template<class Callable, class Tuple>
95 typename detail::ReturnValue<
96   typename std::decay<Callable>::type,
97   typename std::remove_reference<Tuple>::type
98 >::type
99 applyTuple(const Callable& c, Tuple&& t) {
100   typedef typename detail::ReturnValue<
101     typename std::decay<Callable>::type,
102     typename std::remove_reference<Tuple>::type
103   >::type RetT;
104   return detail::CallTuple<RetT>::call(c, std::forward<Tuple>(t));
105 }
106
107 //////////////////////////////////////////////////////////////////////
108
109 }
110
111 #endif