2 * Copyright 2017 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));
34 #include <folly/Utility.h>
38 //////////////////////////////////////////////////////////////////////
41 namespace apply_tuple {
43 inline constexpr std::size_t sum() {
46 template <typename... Args>
47 inline constexpr std::size_t sum(std::size_t v1, Args... vs) {
48 return v1 + sum(vs...);
51 template <typename... Tuples>
53 static constexpr auto value = sum(std::tuple_size<Tuples>::value...);
56 template <typename... Tuples>
57 using MakeIndexSequenceFromTuple = folly::make_index_sequence<
58 TupleSizeSum<typename std::decay<Tuples>::type...>::value>;
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.
63 inline constexpr F&& makeCallable(F&& f) {
64 return std::forward<F>(f);
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);
71 template <class F, class Tuple, std::size_t... Indexes>
72 inline constexpr auto call(F&& f, Tuple&& t, folly::index_sequence<Indexes...>)
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))...);
78 template <class Tuple, std::size_t... Indexes>
79 inline constexpr auto forwardTuple(Tuple&& t, folly::index_sequence<Indexes...>)
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))...);
85 } // namespace apply_tuple
88 //////////////////////////////////////////////////////////////////////
91 * Invoke a callable object with a set of arguments passed as a tuple, or a
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"));
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...>{});
116 //////////////////////////////////////////////////////////////////////