proxygen
TraitsTest.cpp
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 
17 #include <folly/Portability.h>
18 
19 #if FOLLY_HAS_COROUTINES
20 
22 #include <experimental/coroutine>
23 #include <type_traits>
24 
25 using namespace folly::coro;
26 
27 template <typename T>
28 struct SomeAwaiter1 {
29  bool await_ready();
30  void await_suspend(std::experimental::coroutine_handle<>);
31  T await_resume();
32 };
33 
34 template <typename T>
35 struct SomeAwaiter2 {
36  bool await_ready();
37  bool await_suspend(std::experimental::coroutine_handle<>);
38  T await_resume();
39 };
40 
41 template <typename T>
42 struct SomeAwaiter3 {
43  bool await_ready();
44  std::experimental::coroutine_handle<> await_suspend(
45  std::experimental::coroutine_handle<>);
46  T await_resume();
47 };
48 
49 struct MissingAwaitReady {
50  void await_suspend(std::experimental::coroutine_handle<>);
51  int await_resume();
52 };
53 
54 struct WrongAwaitReadyReturnType {
55  void* await_ready();
56  void await_suspend(std::experimental::coroutine_handle<>);
57  int await_resume();
58 };
59 
60 struct MissingAwaitSuspend {
61  bool await_ready();
62  int await_resume();
63 };
64 
65 struct WrongAwaitSuspendArgType {
66  bool await_ready();
67  void await_suspend(float);
68  int await_resume();
69 };
70 
71 struct MissingAwaitResume {
72  bool await_ready();
73  void await_suspend(std::experimental::coroutine_handle<void>);
74 };
75 
76 struct MemberOperatorCoAwait {
77  SomeAwaiter1<void> operator co_await() &;
78  SomeAwaiter2<int> operator co_await() &&;
79  SomeAwaiter3<float> operator co_await() const&;
80 };
81 
82 struct FreeOperatorCoAwait {};
83 SomeAwaiter1<void> operator co_await(FreeOperatorCoAwait);
84 
85 struct MoveOnlyFreeOperatorCoAwait {};
86 SomeAwaiter1<int> operator co_await(MoveOnlyFreeOperatorCoAwait&&);
87 
88 struct MemberOperatorCoAwaitWithInvalidAwaiter {
89  int operator co_await();
90 };
91 
92 static_assert(is_awaiter_v<SomeAwaiter1<void>>, "");
93 static_assert(is_awaiter_v<SomeAwaiter2<int>>, "");
94 static_assert(is_awaiter_v<SomeAwaiter3<float>>, "");
95 static_assert(!is_awaiter_v<void>, "");
96 static_assert(!is_awaiter_v<int>, "");
97 static_assert(!is_awaiter_v<MissingAwaitReady>, "");
98 static_assert(!is_awaiter_v<WrongAwaitReadyReturnType>, "");
99 static_assert(!is_awaiter_v<MissingAwaitSuspend>, "");
100 static_assert(!is_awaiter_v<WrongAwaitSuspendArgType>, "");
101 static_assert(!is_awaiter_v<MissingAwaitResume>, "");
102 static_assert(!is_awaiter_v<MemberOperatorCoAwait>, "");
103 
104 static_assert(is_awaitable_v<SomeAwaiter1<void>>, "");
105 static_assert(is_awaitable_v<SomeAwaiter2<int>>, "");
106 static_assert(is_awaitable_v<SomeAwaiter3<void*>>, "");
107 static_assert(is_awaitable_v<MemberOperatorCoAwait>, "");
108 static_assert(is_awaitable_v<MemberOperatorCoAwait&>, "");
109 static_assert(is_awaitable_v<MemberOperatorCoAwait&&>, "");
110 static_assert(is_awaitable_v<const MemberOperatorCoAwait&>, "");
111 static_assert(is_awaitable_v<FreeOperatorCoAwait>, "");
112 static_assert(is_awaitable_v<FreeOperatorCoAwait&&>, "");
113 static_assert(is_awaitable_v<const FreeOperatorCoAwait&>, "");
114 static_assert(is_awaitable_v<MoveOnlyFreeOperatorCoAwait&&>, "");
115 static_assert(!is_awaitable_v<MoveOnlyFreeOperatorCoAwait&>, "");
116 static_assert(!is_awaitable_v<const MoveOnlyFreeOperatorCoAwait&>, "");
117 static_assert(!is_awaitable_v<void>, "");
118 static_assert(!is_awaitable_v<MemberOperatorCoAwaitWithInvalidAwaiter>, "");
119 
120 static_assert(
121  std::is_same<awaiter_type_t<SomeAwaiter1<void>>, SomeAwaiter1<void>&>::
122  value,
123  "");
124 static_assert(
125  std::is_same<awaiter_type_t<MemberOperatorCoAwait>, SomeAwaiter2<int>>::
126  value,
127  "");
128 static_assert(
129  std::is_same<awaiter_type_t<MemberOperatorCoAwait&>, SomeAwaiter1<void>>::
130  value,
131  "");
132 static_assert(
133  std::is_same<awaiter_type_t<FreeOperatorCoAwait>, SomeAwaiter1<void>>::
134  value,
135  "");
136 
137 static_assert(
138  std::is_same<await_result_t<SomeAwaiter1<void>>, void>::value,
139  "");
140 static_assert(
141  std::is_same<await_result_t<MemberOperatorCoAwait>, int>::value,
142  "");
143 static_assert(
144  std::is_same<await_result_t<MemberOperatorCoAwait&>, void>::value,
145  "");
146 static_assert(
148  "");
149 static_assert(
151  "");
152 
153 #endif
typename awaiter_type< Awaitable >::type awaiter_type_t
Definition: Traits.h:183
constexpr bool is_awaiter_v
Definition: Traits.h:79
folly::std T
typename await_result< Awaitable >::type await_result_t
Definition: Traits.h:194
constexpr bool is_awaitable_v
Definition: Traits.h:120
const
Definition: upload.py:398
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)