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 <typename... Tuples>
60 static constexpr auto value = sum(std::tuple_size<Tuples>::value...);
63 template <typename... Tuples>
64 using MakeIndexSequenceFromTuple = MakeIndexSequence<
65 TupleSizeSum<typename std::decay<Tuples>::type...>::value>;
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.
70 inline constexpr F&& makeCallable(F&& f) {
71 return std::forward<F>(f);
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);
78 template <class F, class Tuple, std::size_t... Indexes>
79 inline constexpr auto call(F&& f, Tuple&& t, IndexSequence<Indexes...>)
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))...);
85 template <class Tuple, std::size_t... Indexes>
86 inline constexpr auto forwardTuple(Tuple&& t, IndexSequence<Indexes...>)
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))...);
92 } // namespace apply_tuple
95 //////////////////////////////////////////////////////////////////////
98 * Invoke a callable object with a set of arguments passed as a tuple, or a
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"));
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...>{});
123 //////////////////////////////////////////////////////////////////////