proxygen
ApplyTuple.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present 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 #pragma once
18 
19 #include <functional>
20 #include <tuple>
21 #include <utility>
22 
23 #include <folly/Traits.h>
24 #include <folly/Utility.h>
26 
27 namespace folly {
28 
30 
34 template <typename Tuple>
37 
38 namespace detail {
39 namespace apply_tuple {
40 namespace adl {
41 using std::get;
42 
43 struct ApplyInvoke {
44  template <typename T>
46 
47  template <typename F, typename T, std::size_t... I>
48  static constexpr auto invoke_(F&& f, T&& t, index_sequence<I...>) noexcept(
49  is_nothrow_invocable<F&&, decltype(get<I>(std::declval<T>()))...>::value)
50  -> invoke_result_t<F&&, decltype(get<I>(std::declval<T>()))...> {
51  return invoke(static_cast<F&&>(f), get<I>(static_cast<T&&>(t))...);
52  }
53 };
54 
55 template <
56  typename Tuple,
57  std::size_t... Indices,
58  typename ReturnTuple =
59  std::tuple<decltype(get<Indices>(std::declval<Tuple>()))...>>
60 auto forward_tuple(Tuple&& tuple, index_sequence<Indices...>) -> ReturnTuple {
61  return ReturnTuple{get<Indices>(std::forward<Tuple>(tuple))...};
62 }
63 } // namespace adl
64 } // namespace apply_tuple
65 } // namespace detail
66 
68  public:
69  template <typename F, typename T>
70  constexpr auto operator()(F&& f, T&& t) const noexcept(
71  noexcept(invoke_(static_cast<F&&>(f), static_cast<T&&>(t), seq<T>{})))
72  -> decltype(invoke_(static_cast<F&&>(f), static_cast<T&&>(t), seq<T>{})) {
73  return invoke_(static_cast<F&&>(f), static_cast<T&&>(t), seq<T>{});
74  }
75 };
76 
78 
79 #if __cpp_lib_apply >= 201603
80 
81 /* using override */ using std::apply;
82 
83 #else // __cpp_lib_apply >= 201603
84 
85 // mimic: std::apply, C++17
86 template <typename F, typename Tuple>
87 constexpr decltype(auto) apply(F&& func, Tuple&& tuple) {
88  return ApplyInvoke{}(static_cast<F&&>(func), static_cast<Tuple&&>(tuple));
89 }
90 
91 #endif // __cpp_lib_apply >= 201603
92 
109 template <typename Tuple>
110 auto forward_tuple(Tuple&& tuple) noexcept
111  -> decltype(detail::apply_tuple::adl::forward_tuple(
112  std::declval<Tuple>(),
113  std::declval<
114  index_sequence_for_tuple<std::remove_reference_t<Tuple>>>())) {
116  std::forward<Tuple>(tuple),
117  index_sequence_for_tuple<std::remove_reference_t<Tuple>>{});
118 }
119 
123 template <typename F, typename Tuple>
125 template <typename F, typename Tuple>
127 template <typename F, typename Tuple>
129 template <typename R, typename F, typename Tuple>
131 template <typename F, typename Tuple>
133 template <typename R, typename F, typename Tuple>
136 
137 namespace detail {
138 namespace apply_tuple {
139 
140 template <class F>
141 class Uncurry {
142  public:
143  explicit Uncurry(F&& func) : func_(std::move(func)) {}
144  explicit Uncurry(const F& func) : func_(func) {}
145 
146  template <class Tuple>
147  auto operator()(Tuple&& tuple) const
148  -> decltype(apply(std::declval<F>(), std::forward<Tuple>(tuple))) {
149  return apply(func_, std::forward<Tuple>(tuple));
150  }
151 
152  private:
153  F func_;
154 };
155 } // namespace apply_tuple
156 } // namespace detail
157 
177 template <class F>
178 auto uncurry(F&& f)
181  std::forward<F>(f));
182 }
183 
184 #if __cpp_lib_make_from_tuple || (_MSC_VER >= 1910 && _MSVC_LANG > 201402)
185 
186 /* using override */ using std::make_from_tuple;
187 
188 #else
189 
190 namespace detail {
191 namespace apply_tuple {
192 template <class T>
193 struct Construct {
194  template <class... Args>
195  constexpr T operator()(Args&&... args) const {
196  return T(std::forward<Args>(args)...);
197  }
198 };
199 } // namespace apply_tuple
200 } // namespace detail
201 
202 // mimic: std::make_from_tuple, C++17
203 template <class T, class Tuple>
204 constexpr T make_from_tuple(Tuple&& t) {
205  return apply(detail::apply_tuple::Construct<T>(), std::forward<Tuple>(t));
206 }
207 
208 #endif
209 
211 } // namespace folly
make_index_sequence< std::tuple_size< Tuple >::value > index_sequence_for_tuple
Definition: ApplyTuple.h:36
static constexpr auto invoke_(F &&f, T &&t, index_sequence< I... >) noexcept(is_nothrow_invocable< F &&, decltype(get< I >(std::declval< T >()))... >::value) -> invoke_result_t< F &&, decltype(get< I >(std::declval< T >()))... >
Definition: ApplyTuple.h:48
index_sequence_for_tuple< std::remove_reference_t< T >> seq
Definition: ApplyTuple.h:45
auto f
auto forward_tuple(Tuple &&tuple, index_sequence< Indices... >) -> ReturnTuple
Definition: ApplyTuple.h:60
typename invoke_result< F, Args... >::type invoke_result_t
Definition: Invoke.h:142
auto uncurry(F &&f) -> detail::apply_tuple::Uncurry< typename std::decay< F >::type >
Definition: ApplyTuple.h:178
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
folly::std T
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
constexpr auto invoke(F &&f, Args &&...args) noexcept(noexcept(static_cast< F && >(f)(static_cast< Args && >(args)...))) -> decltype(static_cast< F && >(f)(static_cast< Args && >(args)...))
Definition: Invoke.h:49
make_integer_sequence< std::size_t, Size > make_index_sequence
Definition: Utility.h:209
invoke_result_t< ApplyInvoke, F, Tuple > apply_result_t
Definition: ApplyTuple.h:126
constexpr T operator()(Args &&...args) const
Definition: ApplyTuple.h:195
auto operator()(Tuple &&tuple) const -> decltype(apply(std::declval< F >(), std::forward< Tuple >(tuple)))
Definition: ApplyTuple.h:147
constexpr auto operator()(F &&f, T &&t) const noexcept(noexcept(invoke_(static_cast< F && >(f), static_cast< T && >(t), seq< T >{}))) -> decltype(invoke_(static_cast< F && >(f), static_cast< T && >(t), seq< T >
Definition: ApplyTuple.h:70
const
Definition: upload.py:398
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
constexpr T make_from_tuple(Tuple &&t)
Definition: ApplyTuple.h:204
decltype(auto) constexpr apply(F &&func, Tuple &&tuple)
Definition: ApplyTuple.h:87