Let applyTuple accept any number of tuples
[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 #pragma once
29
30 #include <functional>
31 #include <tuple>
32 #include <utility>
33
34 namespace folly {
35
36 //////////////////////////////////////////////////////////////////////
37
38 namespace detail {
39 namespace apply_tuple {
40
41 template <std::size_t...>
42 struct IndexSequence {};
43
44 template <std::size_t N, std::size_t... Is>
45 struct MakeIndexSequence : MakeIndexSequence<N - 1, N - 1, Is...> {};
46
47 template <std::size_t... Is>
48 struct MakeIndexSequence<0, Is...> : IndexSequence<Is...> {};
49
50 inline constexpr std::size_t sum() {
51   return 0;
52 }
53 template <typename... Args>
54 inline constexpr std::size_t sum(std::size_t v1, Args... vs) {
55   return v1 + sum(vs...);
56 }
57
58 template <class... Tuple>
59 using MakeIndexSequenceFromTuple = MakeIndexSequence<sum(
60     std::tuple_size<typename std::decay<Tuple>::type>::value...)>;
61
62 // This is to allow using this with pointers to member functions,
63 // where the first argument in the tuple will be the this pointer.
64 template <class F>
65 inline constexpr F&& makeCallable(F&& f) {
66   return std::forward<F>(f);
67 }
68 template <class M, class C>
69 inline constexpr auto makeCallable(M(C::*d)) -> decltype(std::mem_fn(d)) {
70   return std::mem_fn(d);
71 }
72
73 template <class F, class Tuple, std::size_t... Indexes>
74 inline constexpr auto call(F&& f, Tuple&& t, IndexSequence<Indexes...>)
75     -> decltype(
76         std::forward<F>(f)(std::get<Indexes>(std::forward<Tuple>(t))...)) {
77   return std::forward<F>(f)(std::get<Indexes>(std::forward<Tuple>(t))...);
78 }
79
80 template <class Tuple, std::size_t... Indexes>
81 inline constexpr auto forwardTuple(Tuple&& t, IndexSequence<Indexes...>)
82     -> decltype(
83         std::forward_as_tuple(std::get<Indexes>(std::forward<Tuple>(t))...)) {
84   return std::forward_as_tuple(std::get<Indexes>(std::forward<Tuple>(t))...);
85 }
86
87 } // namespace apply_tuple
88 } // namespace detail
89
90 //////////////////////////////////////////////////////////////////////
91
92 /**
93  * Invoke a callable object with a set of arguments passed as a tuple, or a
94  *     series of tuples
95  *
96  * Example: the following lines are equivalent
97  *     func(1, 2, 3, "foo");
98  *     applyTuple(func, std::make_tuple(1, 2, 3, "foo"));
99  *     applyTuple(func, std::make_tuple(1, 2), std::make_tuple(3, "foo"));
100  */
101
102 template <class F, class... Tuples>
103 inline constexpr auto applyTuple(F&& f, Tuples&&... t)
104     -> decltype(detail::apply_tuple::call(
105         detail::apply_tuple::makeCallable(std::forward<F>(f)),
106         std::tuple_cat(detail::apply_tuple::forwardTuple(
107             std::forward<Tuples>(t),
108             detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
109         detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{})) {
110   return detail::apply_tuple::call(
111       detail::apply_tuple::makeCallable(std::forward<F>(f)),
112       std::tuple_cat(detail::apply_tuple::forwardTuple(
113           std::forward<Tuples>(t),
114           detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
115       detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{});
116 }
117
118 //////////////////////////////////////////////////////////////////////
119 }