Skip to content

Commit b1b8052

Browse files
committed
refactoring files
1 parent 06690c7 commit b1b8052

File tree

2 files changed

+123
-108
lines changed

2 files changed

+123
-108
lines changed

include/lzcoders/coro.hpp

+6-108
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace lzcoders
99
{
10+
namespace detail {
1011
template<typename T> struct promise;
12+
}
1113

1214
struct coro_execution_context
1315
{
@@ -30,9 +32,9 @@ concept AsyncAwaitable = requires(T t)
3032
* TASKS *
3133
******************************************************************************************/
3234
template<typename T>
33-
struct task : std::coroutine_handle<promise<T>>
35+
struct task : std::coroutine_handle<detail::promise<T>>
3436
{
35-
using promise_type = promise<T>;
37+
using promise_type = detail::promise<T>;
3638

3739
T operator()(const std::source_location& sl = std::source_location::current())
3840
{
@@ -56,112 +58,8 @@ struct task : std::coroutine_handle<promise<T>>
5658
}
5759
};
5860

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-
};
16561
}
16662

63+
#include <lzcoders/detail/coro_detail.hpp>
64+
16765
#endif
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#ifndef LZCODERS_DETAIL_CORO_DETAIL_HPP
2+
#define LZCODERS_DETAIL_CORO_DETAIL_HPP
3+
4+
#ifndef LZCODERS_CORO_HPP
5+
#error This file is not intended to be used directly. Please, include lzcoders/coro.hpp.
6+
#endif
7+
8+
namespace lzcoders::detail
9+
{
10+
template<typename T> struct awaitable;
11+
12+
template<typename T>
13+
struct promise
14+
{
15+
task<T> get_return_object() { d(); return {task<T>::from_promise(*this)}; }
16+
std::suspend_always initial_suspend() noexcept { d(); return {}; }
17+
std::suspend_always final_suspend() noexcept { d(); return {}; }
18+
void unhandled_exception() { d(); throw; }
19+
void return_value(T v)
20+
{
21+
d();
22+
result_.set_value(v);
23+
if (continuation_)
24+
continuation_->resume();
25+
}
26+
27+
template<typename U>
28+
auto await_transform(task<U> t)
29+
{
30+
d();
31+
t.promise().context_ = context_;
32+
return awaitable<U>{t};
33+
}
34+
35+
template<AsyncAwaitable U>
36+
auto await_transform(U t)
37+
{
38+
d("Updating awaitable action");
39+
t.action_ = [this](std::coroutine_handle<> h){
40+
d("Async awaiter complete");
41+
context_->resume(h);
42+
};
43+
44+
return t;
45+
}
46+
47+
T get_result()
48+
{
49+
return result_.get_future().get();
50+
}
51+
52+
std::promise<T> result_;
53+
std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
54+
std::optional<std::coroutine_handle<>> continuation_;
55+
};
56+
57+
template<>
58+
struct promise<void>
59+
{
60+
task<void> get_return_object() { d(); return {task<void>::from_promise(*this)}; }
61+
std::suspend_always initial_suspend() noexcept { d(); return {}; }
62+
std::suspend_always final_suspend() noexcept { d(); return {}; }
63+
void unhandled_exception() { d(); throw; }
64+
void return_void() { d(); ended_.set_value(true); }
65+
66+
template<typename U>
67+
auto await_transform(task<U> t)
68+
{
69+
d();
70+
return awaitable<U>{t};
71+
}
72+
73+
template<AsyncAwaitable U>
74+
auto await_transform(U t)
75+
{
76+
t.action_ = [this](std::coroutine_handle<> h){
77+
d("Async awaiter complete");
78+
context_->resume(h);
79+
};
80+
81+
return t;
82+
}
83+
84+
void get_result()
85+
{
86+
ended_.get_future().get();
87+
}
88+
std::promise<bool> ended_;
89+
std::shared_ptr<coro_execution_context> context_ = std::make_shared<default_execution_context>();
90+
};
91+
92+
template<typename T>
93+
struct awaitable
94+
{
95+
bool await_ready()
96+
{
97+
d();
98+
return false;
99+
}
100+
101+
void await_suspend(std::coroutine_handle<> h)
102+
{
103+
d();
104+
t_.promise().continuation_ = h;
105+
t_.resume();
106+
}
107+
108+
T await_resume()
109+
{
110+
d("Returns the inner task result");
111+
return t_.get_result();
112+
}
113+
114+
task<T> t_;
115+
};
116+
} // namespace lzcoders::detail
117+
#endif

0 commit comments

Comments
 (0)