2 * Copyright 2016 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));
36 //////////////////////////////////////////////////////////////////////
39 namespace apply_tuple {
41 template <std::size_t...>
42 struct IndexSequence {};
44 template <std::size_t N, std::size_t... Is>
45 struct MakeIndexSequence : MakeIndexSequence<N - 1, N - 1, Is...> {};
47 template <std::size_t... Is>
48 struct MakeIndexSequence<0, Is...> : IndexSequence<Is...> {};
50 inline constexpr std::size_t sum() {
53 template <typename... Args>
54 inline constexpr std::size_t sum(std::size_t v1, Args... vs) {
55 return v1 + sum(vs...);
58 template <class... Tuple>
59 using MakeIndexSequenceFromTuple = MakeIndexSequence<sum(
60 std::tuple_size<typename std::decay<Tuple>::type>::value...)>;
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.
65 inline constexpr F&& makeCallable(F&& f) {
66 return std::forward<F>(f);
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);
73 template <class F, class Tuple, std::size_t... Indexes>
74 inline constexpr auto call(F&& f, Tuple&& t, IndexSequence<Indexes...>)
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))...);
80 template <class Tuple, std::size_t... Indexes>
81 inline constexpr auto forwardTuple(Tuple&& t, IndexSequence<Indexes...>)
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))...);
87 } // namespace apply_tuple
90 //////////////////////////////////////////////////////////////////////
93 * Invoke a callable object with a set of arguments passed as a tuple, or a
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"));
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...>{});
118 //////////////////////////////////////////////////////////////////////