folly copyright 2015 -> copyright 2016
[folly.git] / folly / ApplyTuple.h
1 /*
2  * Copyright 2016 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 template<class Tuple, class ...Unpacked> struct ExprDoUnpack {
55   enum {
56     value = sizeof...(Unpacked) < DerefSize<Tuple>::value
57   };
58 };
59
60 template<class Tuple, class ...Unpacked> struct ExprIsUnpacked {
61   enum {
62     value = sizeof...(Unpacked) == DerefSize<Tuple>::value
63   };
64 };
65
66 // CallTuple recursively unpacks tuple arguments so we can forward
67 // them into the function.
68 template<class Ret>
69 struct CallTuple {
70   template<class F, class Tuple, class ...Unpacked>
71   static typename std::enable_if<ExprDoUnpack<Tuple, Unpacked...>::value,
72     Ret
73   >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
74     typedef typename std::tuple_element<
75       sizeof...(Unpacked),
76       typename std::remove_reference<Tuple>::type
77     >::type ElementType;
78     return CallTuple<Ret>::call(f, std::forward<Tuple>(t),
79       std::forward<Unpacked>(unp)...,
80       std::forward<ElementType>(std::get<sizeof...(Unpacked)>(t))
81     );
82   }
83
84   template <class F, class Tuple, class... Unpacked>
85   static typename std::enable_if<ExprIsUnpacked<Tuple, Unpacked...>::value,
86                                  Ret>::type
87   call(const F& f, Tuple&& /* t */, Unpacked&&... unp) {
88     return makeCallable(f)(std::forward<Unpacked>(unp)...);
89   }
90 };
91
92 // The point of this meta function is to extract the contents of the
93 // tuple as a parameter pack so we can pass it into std::result_of<>.
94 template<class F, class Args> struct ReturnValue;
95 template<class F, class ...Args>
96 struct ReturnValue<F,std::tuple<Args...>> {
97   typedef typename std::result_of<F (Args...)>::type type;
98 };
99
100 }
101
102 //////////////////////////////////////////////////////////////////////
103
104 template<class Callable, class Tuple>
105 typename detail::ReturnValue<
106   typename std::decay<Callable>::type,
107   typename std::decay<Tuple>::type
108 >::type
109 applyTuple(const Callable& c, Tuple&& t) {
110   typedef typename detail::ReturnValue<
111     typename std::decay<Callable>::type,
112     typename std::decay<Tuple>::type
113   >::type RetT;
114   return detail::CallTuple<RetT>::call(c, std::forward<Tuple>(t));
115 }
116
117 //////////////////////////////////////////////////////////////////////
118
119 }
120
121 #endif