Make folly::getCurrentThreadId() return a thread ID on OSX
[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 namespace detail {
117 namespace apply_tuple {
118
119 template <class F>
120 class Uncurry {
121  public:
122   explicit Uncurry(F&& func) : func_(std::move(func)) {}
123   explicit Uncurry(const F& func) : func_(func) {}
124
125   template <class Tuple>
126   auto operator()(Tuple&& tuple) const
127       -> decltype(applyTuple(std::declval<F>(), std::forward<Tuple>(tuple))) {
128     return applyTuple(func_, std::forward<Tuple>(tuple));
129   }
130
131  private:
132   F func_;
133 };
134 } // namespace apply_tuple
135 } // namespace detail
136
137 /**
138  * Wraps a function taking N arguments into a function which accepts a tuple of
139  * N arguments. Note: This function will also accept an std::pair if N == 2.
140  *
141  * For example, given the below code:
142  *
143  *    std::vector<std::tuple<int, int, int>> rows = ...;
144  *    auto test = [](std::tuple<int, int, int>& row) {
145  *      return std::get<0>(row) * std::get<1>(row) * std::get<2>(row) == 24;
146  *    };
147  *    auto found = std::find_if(rows.begin(), rows.end(), test);
148  *
149  *
150  * 'test' could be rewritten as:
151  *
152  *    auto test =
153  *        folly::uncurry([](int a, int b, int c) { return a * b * c == 24; });
154  *
155  */
156 template <class F>
157 auto uncurry(F&& f)
158     -> detail::apply_tuple::Uncurry<typename std::decay<F>::type> {
159   return detail::apply_tuple::Uncurry<typename std::decay<F>::type>(
160       std::forward<F>(f));
161 }
162
163 //////////////////////////////////////////////////////////////////////
164 }