2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Defines a function folly::applyTuple, which takes a function and a
19 * std::tuple of arguments and calls the function with those
24 * int x = folly::applyTuple(std::plus<int>(), std::make_tuple(12, 12));
28 #ifndef FOLLY_APPLYTUPLE_H_
29 #define FOLLY_APPLYTUPLE_H_
33 #include <type_traits>
37 //////////////////////////////////////////////////////////////////////
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);
51 : std::tuple_size<typename std::remove_reference<Tuple>::type>
54 template<class Tuple, class ...Unpacked> struct ExprDoUnpack {
56 value = sizeof...(Unpacked) < DerefSize<Tuple>::value
60 template<class Tuple, class ...Unpacked> struct ExprIsUnpacked {
62 value = sizeof...(Unpacked) == DerefSize<Tuple>::value
66 // CallTuple recursively unpacks tuple arguments so we can forward
67 // them into the function.
70 template<class F, class Tuple, class ...Unpacked>
71 static typename std::enable_if<ExprDoUnpack<Tuple, Unpacked...>::value,
73 >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
74 typedef typename std::tuple_element<
76 typename std::remove_reference<Tuple>::type
78 return CallTuple<Ret>::call(f, std::forward<Tuple>(t),
79 std::forward<Unpacked>(unp)...,
80 std::forward<ElementType>(std::get<sizeof...(Unpacked)>(t))
84 template<class F, class Tuple, class ...Unpacked>
85 static typename std::enable_if<ExprIsUnpacked<Tuple, Unpacked...>::value,
87 >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
88 return makeCallable(f)(std::forward<Unpacked>(unp)...);
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;
102 //////////////////////////////////////////////////////////////////////
104 template<class Callable, class Tuple>
105 typename detail::ReturnValue<
106 typename std::decay<Callable>::type,
107 typename std::remove_reference<Tuple>::type
109 applyTuple(const Callable& c, Tuple&& t) {
110 typedef typename detail::ReturnValue<
111 typename std::decay<Callable>::type,
112 typename std::remove_reference<Tuple>::type
114 return detail::CallTuple<RetT>::call(c, std::forward<Tuple>(t));
117 //////////////////////////////////////////////////////////////////////