7
7
8
8
namespace lzcoders
9
9
{
10
+ namespace detail {
10
11
template <typename T> struct promise ;
12
+ }
11
13
12
14
struct coro_execution_context
13
15
{
@@ -30,9 +32,9 @@ concept AsyncAwaitable = requires(T t)
30
32
* TASKS *
31
33
******************************************************************************************/
32
34
template <typename T>
33
- struct task : std::coroutine_handle<promise<T>>
35
+ struct task : std::coroutine_handle<detail:: promise<T>>
34
36
{
35
- using promise_type = promise<T>;
37
+ using promise_type = detail:: promise<T>;
36
38
37
39
T operator ()(const std::source_location& sl = std::source_location::current())
38
40
{
@@ -56,112 +58,8 @@ struct task : std::coroutine_handle<promise<T>>
56
58
}
57
59
};
58
60
59
- template <typename T> struct awaitable ;
60
-
61
- template <typename T>
62
- struct promise
63
- {
64
- task<T> get_return_object () { d (); return {task<T>::from_promise (*this )}; }
65
- std::suspend_always initial_suspend () noexcept { d (); return {}; }
66
- std::suspend_always final_suspend () noexcept { d (); return {}; }
67
- void unhandled_exception () { d (); throw ; }
68
- void return_value (T v)
69
- {
70
- d ();
71
- result_.set_value (v);
72
- if (continuation_)
73
- continuation_->resume ();
74
- }
75
-
76
- template <typename U>
77
- auto await_transform (task<U> t)
78
- {
79
- d ();
80
- t.promise ().context_ = context_;
81
- return awaitable<U>{t};
82
- }
83
-
84
- template <AsyncAwaitable U>
85
- auto await_transform (U t)
86
- {
87
- d (" Updating awaitable action" );
88
- t.action_ = [this ](std::coroutine_handle<> h){
89
- d (" Async awaiter complete" );
90
- context_->resume (h);
91
- };
92
-
93
- return t;
94
- }
95
-
96
- T get_result ()
97
- {
98
- return result_.get_future ().get ();
99
- }
100
-
101
- std::promise<T> result_;
102
- std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
103
- std::optional<std::coroutine_handle<>> continuation_;
104
- };
105
-
106
- template <>
107
- struct promise <void >
108
- {
109
- task<void > get_return_object () { d (); return {task<void >::from_promise (*this )}; }
110
- std::suspend_always initial_suspend () noexcept { d (); return {}; }
111
- std::suspend_always final_suspend () noexcept { d (); return {}; }
112
- void unhandled_exception () { d (); throw ; }
113
- void return_void () { d (); ended_.set_value (true ); }
114
-
115
- template <typename U>
116
- auto await_transform (task<U> t)
117
- {
118
- d ();
119
- return awaitable<U>{t};
120
- }
121
-
122
- template <AsyncAwaitable U>
123
- auto await_transform (U t)
124
- {
125
- t.action_ = [this ](std::coroutine_handle<> h){
126
- d (" Async awaiter complete" );
127
- context_->resume (h);
128
- };
129
-
130
- return t;
131
- }
132
-
133
- void get_result ()
134
- {
135
- ended_.get_future ().get ();
136
- }
137
- std::promise<bool > ended_;
138
- std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
139
- };
140
-
141
- template <typename T>
142
- struct awaitable
143
- {
144
- bool await_ready ()
145
- {
146
- d ();
147
- return false ;
148
- }
149
-
150
- void await_suspend (std::coroutine_handle<> h)
151
- {
152
- d ();
153
- t_.promise ().continuation_ = h;
154
- t_.resume ();
155
- }
156
-
157
- T await_resume ()
158
- {
159
- d (" Returns the inner task result" );
160
- return t_.get_result ();
161
- }
162
-
163
- task<T> t_;
164
- };
165
61
}
166
62
63
+ #include < lzcoders/detail/coro_detail.hpp>
64
+
167
65
#endif
0 commit comments