proxygen
Traits.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-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 #pragma once
17 
18 #include <folly/Traits.h>
19 
20 #include <experimental/coroutine>
21 #include <type_traits>
22 
23 namespace folly {
24 namespace coro {
25 
26 namespace detail {
27 
28 template <typename T>
30 
31 template <typename T>
32 struct _is_coroutine_handle<std::experimental::coroutine_handle<T>>
33  : std::true_type {};
34 
35 template <typename T>
37  std::is_void<T>,
38  std::is_same<bool, T>,
39  _is_coroutine_handle<T>> {};
40 } // namespace detail
41 
60 template <typename T, typename = void>
62 
63 template <typename T>
64 struct is_awaiter<
65  T,
66  folly::void_t<
67  decltype(std::declval<T&>().await_ready()),
68  decltype(std::declval<T&>().await_suspend(
69  std::declval<std::experimental::coroutine_handle<void>>())),
70  decltype(std::declval<T&>().await_resume())>>
72  std::is_same<bool, decltype(std::declval<T&>().await_ready())>,
73  detail::_is_valid_await_suspend_return_type<decltype(
74  std::declval<T&>().await_suspend(
75  std::declval<
76  std::experimental::coroutine_handle<void>>()))>> {};
77 
78 template <typename T>
80 
81 namespace detail {
82 
83 template <typename Awaitable, typename = void>
85 
86 template <typename Awaitable>
88  Awaitable,
89  folly::void_t<decltype(std::declval<Awaitable>().operator co_await())>>
90  : is_awaiter<decltype(std::declval<Awaitable>().operator co_await())> {};
91 
92 template <typename Awaitable, typename = void>
94 
95 template <typename Awaitable>
97  Awaitable,
98  folly::void_t<decltype(operator co_await(std::declval<Awaitable>()))>>
99  : is_awaiter<decltype(operator co_await(std::declval<Awaitable>()))> {};
100 
101 } // namespace detail
102 
113 template <typename T>
115  detail::_has_member_operator_co_await<T>,
116  detail::_has_free_operator_co_await<T>,
117  is_awaiter<T>> {};
118 
119 template <typename T>
121 
129 template <
130  typename Awaitable,
131  std::enable_if_t<
136  value,
137  int> = 0>
138 Awaitable& get_awaiter(Awaitable&& awaitable) {
139  return awaitable;
140 }
141 
142 template <
143  typename Awaitable,
144  std::enable_if_t<
146  int> = 0>
147 decltype(auto) get_awaiter(Awaitable&& awaitable) {
148  return static_cast<Awaitable&&>(awaitable).operator co_await();
149 }
150 
151 template <
152  typename Awaitable,
153  std::enable_if_t<
157  value,
158  int> = 0>
159 decltype(auto) get_awaiter(Awaitable&& awaitable) {
160  return operator co_await(static_cast<Awaitable&&>(awaitable));
161 }
162 
169 template <typename Awaitable, typename = void>
170 struct awaiter_type {};
171 
172 template <typename Awaitable>
173 struct awaiter_type<Awaitable, std::enable_if_t<is_awaitable_v<Awaitable>>> {
174  using type = decltype(get_awaiter(std::declval<Awaitable>()));
175 };
176 
182 template <typename Awaitable>
184 
185 template <typename Awaitable, typename = void>
186 struct await_result {};
187 
188 template <typename Awaitable>
189 struct await_result<Awaitable, std::enable_if_t<is_awaitable_v<Awaitable>>> {
190  using type = decltype(get_awaiter(std::declval<Awaitable>()).await_resume());
191 };
192 
193 template <typename Awaitable>
195 
196 } // namespace coro
197 } // namespace folly
typename awaiter_type< Awaitable >::type awaiter_type_t
Definition: Traits.h:183
constexpr bool is_awaiter_v
Definition: Traits.h:79
STL namespace.
decltype(get_awaiter(std::declval< Awaitable >()).await_resume()) type
Definition: Traits.h:190
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
typename await_result< Awaitable >::type await_result_t
Definition: Traits.h:194
bool_constant< true > true_type
Definition: gtest-port.h:2210
type_t< void, Ts... > void_t
Definition: Traits.h:302
constexpr bool is_awaitable_v
Definition: Traits.h:120
Awaitable & get_awaiter(Awaitable &&awaitable)
Definition: Traits.h:138
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
bool_constant< false > false_type
Definition: gtest-port.h:2209