41ac5337dec4df1366b72ebc4f88de2967011816
[folly.git] / folly / ApplyTuple.h
1 /*
2  * Copyright 2017 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 <typename... Tuples>
59 struct TupleSizeSum {
60   static constexpr auto value = sum(std::tuple_size<Tuples>::value...);
61 };
62
63 template <typename... Tuples>
64 using MakeIndexSequenceFromTuple = MakeIndexSequence<
65     TupleSizeSum<typename std::decay<Tuples>::type...>::value>;
66
67 // This is to allow using this with pointers to member functions,
68 // where the first argument in the tuple will be the this pointer.
69 template <class F>
70 inline constexpr F&& makeCallable(F&& f) {
71   return std::forward<F>(f);
72 }
73 template <class M, class C>
74 inline constexpr auto makeCallable(M(C::*d)) -> decltype(std::mem_fn(d)) {
75   return std::mem_fn(d);
76 }
77
78 template <class F, class Tuple, std::size_t... Indexes>
79 inline constexpr auto call(F&& f, Tuple&& t, IndexSequence<Indexes...>)
80     -> decltype(
81         std::forward<F>(f)(std::get<Indexes>(std::forward<Tuple>(t))...)) {
82   return std::forward<F>(f)(std::get<Indexes>(std::forward<Tuple>(t))...);
83 }
84
85 template <class Tuple, std::size_t... Indexes>
86 inline constexpr auto forwardTuple(Tuple&& t, IndexSequence<Indexes...>)
87     -> decltype(
88         std::forward_as_tuple(std::get<Indexes>(std::forward<Tuple>(t))...)) {
89   return std::forward_as_tuple(std::get<Indexes>(std::forward<Tuple>(t))...);
90 }
91
92 } // namespace apply_tuple
93 } // namespace detail
94
95 //////////////////////////////////////////////////////////////////////
96
97 /**
98  * Invoke a callable object with a set of arguments passed as a tuple, or a
99  *     series of tuples
100  *
101  * Example: the following lines are equivalent
102  *     func(1, 2, 3, "foo");
103  *     applyTuple(func, std::make_tuple(1, 2, 3, "foo"));
104  *     applyTuple(func, std::make_tuple(1, 2), std::make_tuple(3, "foo"));
105  */
106
107 template <class F, class... Tuples>
108 inline constexpr auto applyTuple(F&& f, Tuples&&... t)
109     -> decltype(detail::apply_tuple::call(
110         detail::apply_tuple::makeCallable(std::forward<F>(f)),
111         std::tuple_cat(detail::apply_tuple::forwardTuple(
112             std::forward<Tuples>(t),
113             detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
114         detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{})) {
115   return detail::apply_tuple::call(
116       detail::apply_tuple::makeCallable(std::forward<F>(f)),
117       std::tuple_cat(detail::apply_tuple::forwardTuple(
118           std::forward<Tuples>(t),
119           detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
120       detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{});
121 }
122
123 //////////////////////////////////////////////////////////////////////
124 }