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