Skip to content

Commit 124dbec

Browse files
committed
adapted producing result types
1 parent 71d6518 commit 124dbec

File tree

16 files changed

+161
-218
lines changed

16 files changed

+161
-218
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)
3434

3535
FetchContent_Declare(
3636
execution
37-
SOURCE_DIR /Users/kuehl/src/execution
38-
# GIT_REPOSITORY https://github.com/bemanproject/execution
39-
# GIT_TAG 578c05
37+
# SOURCE_DIR <path-to>/execution
38+
GIT_REPOSITORY https://github.com/bemanproject/execution
39+
GIT_TAG 578c05
4040
)
4141
FetchContent_MakeAvailable(execution)
4242
FetchContent_Declare(

examples/CMakeLists.txt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,32 @@
22

33
set(ALL_EXAMPLES co_await-task)
44
set(ALL_EXAMPLES
5-
co_await-task
6-
task_scheduler
7-
environment
5+
affinity
6+
alloc
7+
c++now-affinity
8+
c++now-allocator
89
c++now-basic
9-
c++now-result-types
10-
c++now-errors
1110
c++now-cancel
11+
c++now-errors
12+
c++now-query
13+
c++now-result-types
1214
c++now-return
13-
c++now-with_error
14-
c++now-allocator
1515
c++now-stop_token
16-
c++now-query
17-
c++now-affinity
18-
affinity
19-
escaped-exception
16+
c++now-with_error
2017
co_await-result
21-
friendly
22-
loop
23-
stop
24-
alloc
18+
co_await-task
2519
container
26-
hello
27-
query
28-
async-lock
20+
environment
2921
error
22+
escaped-exception
23+
friendly
24+
hello
3025
into_optional
26+
loop
27+
query
3128
result_example
29+
stop
30+
task_scheduler
3231
)
3332

3433
message("Examples to be built: ${ALL_EXAMPLES}")

include/beman/lazy/lazy.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
// ----------------------------------------------------------------------------
1010

1111
namespace beman::lazy {
12-
template <typename T = void, typename Context = ::beman::task::default_environment>
12+
template <typename T = void, typename Context = ::beman::task::detail::default_environment>
1313
using lazy = ::beman::task::detail::task<T, Context>;
1414
}
1515

1616
namespace beman::execution {
17-
template <typename T = void, typename Context = ::beman::task::default_environment>
17+
template <typename T = void, typename Context = ::beman::task::detail::default_environment>
1818
using lazy [[deprecated("beman::execution::lazy has been renamed to beman::execution::task")]] =
1919
::beman::execution::task<T, Context>;
2020
}

include/beman/task/detail/awaiter.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
#include <beman/task/detail/state_base.hpp>
99
#include <coroutine>
1010
#include <utility>
11-
#include <iostream> //-dk:TODO
1211

1312
// ----------------------------------------------------------------------------
1413

1514
namespace beman::task::detail {
16-
template <typename Env, typename Promise>
17-
class awaiter : public ::beman::task::detail::state_base<Env> {
15+
template <typename Value, typename Env, typename Promise>
16+
class awaiter : public ::beman::task::detail::state_base<Value, Env> {
1817
public:
19-
using stop_token_type = typename ::beman::task::detail::state_base<Env>::stop_token_type;
20-
using scheduler_type = typename ::beman::task::detail::state_base<Env>::scheduler_type;
18+
using stop_token_type = typename ::beman::task::detail::state_base<Value, Env>::stop_token_type;
19+
using scheduler_type = typename ::beman::task::detail::state_base<Value, Env>::scheduler_type;
2120

2221
explicit awaiter(::beman::task::detail::handle<Promise> h) : handle(::std::move(h)) {}
2322
constexpr auto await_ready() const noexcept -> bool { return false; }
@@ -30,7 +29,10 @@ class awaiter : public ::beman::task::detail::state_base<Env> {
3029
assert(this->parent);
3130
return this->handle.start(this);
3231
}
33-
auto await_resume() { ::beman::task::detail::logger l("awaiter::await_resume()"); }
32+
auto await_resume() {
33+
::beman::task::detail::logger l("awaiter::await_resume()");
34+
return this->result_resume();
35+
}
3436
auto parent_handle() -> ::std::coroutine_handle<> { return ::std::move(this->parent); }
3537

3638
private:
@@ -44,7 +46,7 @@ class awaiter : public ::beman::task::detail::state_base<Env> {
4446
return ::std::exchange(*this->scheduler, other);
4547
}
4648
auto do_get_stop_token() -> stop_token_type override { return {}; }
47-
auto do_get_context() -> Env& override { return this->env; }
49+
auto do_get_environment() -> Env& override { return this->env; }
4850

4951
Env env;
5052
::std::optional<scheduler_type> scheduler;

include/beman/task/detail/handle.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class handle {
3434
auto release() -> ::std::coroutine_handle<P> {
3535
return ::std::coroutine_handle<P>::from_promise(*this->h.release());
3636
}
37-
template <::beman::execution::receiver Receiver>
38-
auto complete(Receiver&& receiver) -> void {
39-
this->h->complete(::std::forward<Receiver>(receiver));
40-
}
4137
};
4238

4339
} // namespace beman::task::detail

include/beman/task/detail/promise_base.hpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#ifndef INCLUDED_INCLUDE_BEMAN_TASK_DETAIL_PROMISE_BASE
55
#define INCLUDED_INCLUDE_BEMAN_TASK_DETAIL_PROMISE_BASE
66

7-
#include <beman/task/detail/result_type.hpp>
7+
#include <beman/task/detail/state_base.hpp>
88
#include <beman/task/detail/logger.hpp>
99
#include <beman/execution/execution.hpp>
1010
#include <cstddef>
@@ -20,13 +20,8 @@ namespace beman::task::detail {
2020
* \headerfile beman/task/task.hpp <beman/task/task.hpp>
2121
* \internal
2222
*/
23-
template <::beman::task::detail::stoppable Stop, typename Value, typename ErrorCompletions>
24-
class promise_base;
25-
26-
template <::beman::task::detail::stoppable Stop, typename Value, typename... Error>
27-
requires(not ::std::same_as<Value, void>)
28-
class promise_base<Stop, Value, ::beman::execution::completion_signatures<::beman::execution::set_error_t(Error)...>>
29-
: public ::beman::task::detail::result_type<Stop, Value, Error...> {
23+
template <::beman::task::detail::stoppable Stop, typename Value, typename Environment>
24+
class promise_base {
3025
public:
3126
/*
3227
* \brief Set the value result.
@@ -35,21 +30,34 @@ class promise_base<Stop, Value, ::beman::execution::completion_signatures<::bema
3530
template <typename T>
3631
void return_value(T&& value) {
3732
::beman::task::detail::logger l("promise_base::return_value(T&&)");
38-
this->set_value(::std::forward<T>(value));
33+
this->get_state()->set_value(::std::forward<T>(value));
3934
}
35+
36+
protected:
37+
auto set_state(::beman::task::detail::state_base<Value, Environment>* s) noexcept -> void { this->state_ = s; }
38+
auto get_state() const noexcept -> ::beman::task::detail::state_base<Value, Environment>* { return this->state_; }
39+
40+
private:
41+
::beman::task::detail::state_base<Value, Environment>* state_{};
4042
};
4143

42-
template <typename ::beman::task::detail::stoppable Stop, typename... Error>
43-
class promise_base<Stop, void, ::beman::execution::completion_signatures<::beman::execution::set_error_t(Error)...>>
44-
: public ::beman::task::detail::result_type<Stop, void_type, Error...> {
44+
template <typename ::beman::task::detail::stoppable Stop, typename Environment>
45+
class promise_base<Stop, void, Environment> {
4546
public:
4647
/*
4748
* \brief Set the value result although without any value.
4849
*/
4950
void return_void() {
5051
::beman::task::detail::logger l("promise_base::return_void()");
51-
this->set_value(void_type{});
52+
this->get_state()->set_value(void_type{});
5253
}
54+
55+
protected:
56+
auto set_state(::beman::task::detail::state_base<void, Environment>* s) noexcept -> void { this->state_ = s; }
57+
auto get_state() const noexcept -> ::beman::task::detail::state_base<void, Environment>* { return this->state_; }
58+
59+
private:
60+
::beman::task::detail::state_base<void, Environment>* state_{};
5361
};
5462
} // namespace beman::task::detail
5563

include/beman/task/detail/promise_env.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ struct promise_env {
2727
template <typename Q, typename... A>
2828
requires requires(const Promise* p, Q q, A&&... a) {
2929
::beman::execution::forwarding_query(q);
30-
q(p->get_context(), std::forward<A>(a)...);
30+
q(p->get_environment(), std::forward<A>(a)...);
3131
}
3232
auto query(Q q, A&&... a) const noexcept {
33-
return q(promise->get_context(), std::forward<A>(a)...);
33+
return q(promise->get_environment(), std::forward<A>(a)...);
3434
}
3535
};
3636
} // namespace beman::task::detail

include/beman/task/detail/promise_type.hpp

Lines changed: 18 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -32,118 +32,10 @@
3232

3333
namespace beman::task::detail {
3434

35-
#if 0
36-
template <typename T>
37-
struct has_exception_ptr;
38-
template <typename... T>
39-
struct has_exception_ptr<::beman::execution::completion_signatures<T...>> {
40-
static constexpr bool value{
41-
::beman::execution::detail::meta::contains<::beman::execution::set_error_t(::std::exception_ptr), T...>};
42-
};
43-
44-
template <typename T>
45-
inline constexpr bool has_exception_ptr_v{::beman::task::detail::has_exception_ptr<T>::value};
46-
47-
template <typename Coroutine, typename T, typename C>
48-
struct promise_type : ::beman::task::detail::promise_base<::beman::task::detail::stoppable::yes,
49-
::std::remove_cvref_t<T>,
50-
::beman::task::detail::error_types_of_t<C>>,
51-
::beman::task::detail::allocator_support<::beman::task::detail::allocator_of_t<C>> {
52-
53-
void start([[maybe_unused]] auto&& e, ::beman::task::detail::state_base<C>* s) {
54-
this->state = s;
55-
if constexpr (std::same_as<::beman::task::detail::inline_scheduler, scheduler_type>) {
56-
this->scheduler.emplace();
57-
} else {
58-
this->scheduler.emplace(::beman::execution::get_scheduler(e));
59-
}
60-
this->initial->run();
61-
}
62-
63-
struct initial_base {
64-
virtual ~initial_base() = default;
65-
virtual void run() = 0;
66-
};
67-
struct initial_sender {
68-
using sender_concept = ::beman::execution::sender_t;
69-
using completion_signatures = ::beman::execution::completion_signatures<::beman::execution::set_value_t()>;
70-
71-
template <::beman::execution::receiver Receiver>
72-
struct state : initial_base {
73-
using operation_state_concept = ::beman::execution::operation_state_t;
74-
promise_type* promise;
75-
Receiver receiver;
76-
template <typename R>
77-
state(promise_type* p, R&& r) : promise(p), receiver(::std::forward<R>(r)) {}
78-
void start() & noexcept { this->promise->initial = this; }
79-
void run() override { ::beman::execution::set_value(::std::move(receiver)); }
80-
};
81-
82-
promise_type* promise{};
83-
template <::beman::execution::receiver Receiver>
84-
auto connect(Receiver&& receiver) {
85-
return state<::std::remove_cvref_t<Receiver>>(this->promise, ::std::forward<Receiver>(receiver));
86-
}
87-
};
88-
89-
auto initial_suspend() noexcept {
90-
return this->internal_await_transform(initial_sender{this},
91-
optional_ref_scheduler<scheduler_type>{&this->scheduler});
92-
}
93-
void unhandled_exception() {
94-
if constexpr (::beman::task::detail::has_exception_ptr_v<::beman::task::detail::error_types_of_t<C>>) {
95-
this->set_error(std::current_exception());
96-
} else {
97-
std::terminate();
98-
}
99-
}
100-
auto get_return_object() noexcept { return Coroutine(::beman::task::detail::handle<promise_type>(this)); }
101-
102-
template <typename E>
103-
auto await_transform(::beman::task::detail::with_error<E> with) noexcept {
104-
// This overload is only used if error completions use `co_await with_error(e)`.
105-
return std::move(with);
106-
}
107-
template <typename Env, typename P>
108-
auto await_transform(::beman::task::detail::awaiter<Env, P>&& a) noexcept {
109-
return ::std::move(a);
110-
}
111-
template <::beman::execution::sender Sender, typename Scheduler>
112-
auto internal_await_transform(Sender&& sender, Scheduler&& sched) noexcept {
113-
if constexpr (std::same_as<::beman::task::detail::inline_scheduler, scheduler_type>)
114-
return ::beman::execution::as_awaitable(std::forward<Sender>(sender), *this);
115-
else
116-
return ::beman::execution::as_awaitable(
117-
::beman::task::affine_on(::std::forward<Sender>(sender), ::std::forward<Scheduler>(sched)), *this);
118-
}
119-
template <::beman::execution::sender Sender>
120-
auto await_transform(Sender&& sender) noexcept {
121-
return this->internal_await_transform(::std::forward<Sender>(sender), *this->scheduler);
122-
}
123-
auto await_transform(::beman::task::detail::change_coroutine_scheduler<scheduler_type> c) {
124-
return ::std::move(c);
125-
}
126-
127-
[[no_unique_address]] allocator_type allocator;
128-
std::optional<scheduler_type> scheduler{};
129-
::beman::task::detail::state_base<C>* state{};
130-
initial_base* initial{};
131-
132-
std::coroutine_handle<> unhandled_stopped() {
133-
this->state->complete();
134-
return std::noop_coroutine();
135-
}
136-
137-
138-
auto get_env() const noexcept -> env_t { return env_t{this}; }
139-
};
140-
#else
141-
142-
template <typename Coroutine, typename T, typename Environment>
35+
template <typename Coroutine, typename Value, typename Environment>
14336
class promise_type
144-
: public ::beman::task::detail::promise_base<::beman::task::detail::stoppable::yes,
145-
::std::remove_cvref_t<T>,
146-
::beman::task::detail::error_types_of_t<Environment>>,
37+
: public ::beman::task::detail::
38+
promise_base<::beman::task::detail::stoppable::yes, ::std::remove_cvref_t<Value>, Environment>,
14739
public ::beman::task::detail::allocator_support<::beman::task::detail::allocator_of_t<Environment>> {
14840
public:
14941
using allocator_type = ::beman::task::detail::allocator_of_t<Environment>;
@@ -165,7 +57,7 @@ class promise_type
16557

16658
auto unhandled_exception() noexcept { /*-dk:TODO*/ }
16759
std::coroutine_handle<> unhandled_stopped() {
168-
this->state->complete();
60+
this->get_state()->complete();
16961
return std::noop_coroutine();
17062
}
17163

@@ -192,31 +84,33 @@ class promise_type
19284

19385
template <typename E>
19486
auto yield_value(with_error<E> with) noexcept -> ::beman::task::detail::final_awaiter {
195-
this->set_error(::std::move(with.error));
87+
this->get_state()->set_error(::std::move(with.error));
19688
return {};
19789
}
19890

19991
auto get_env() const noexcept -> ::beman::task::detail::promise_env<promise_type> { return {this}; }
20092

201-
auto start(::beman::task::detail::state_base<Environment>* state) -> ::std::coroutine_handle<> {
93+
auto start(::beman::task::detail::state_base<Value, Environment>* state) -> ::std::coroutine_handle<> {
20294
::beman::task::detail::logger l("promise_type::start");
203-
this->state = state;
95+
this->set_state(state);
20496
return ::std::coroutine_handle<promise_type>::from_promise(*this);
20597
}
206-
auto notify_complete() -> ::std::coroutine_handle<> { return this->state->complete(); }
207-
scheduler_type change_scheduler(scheduler_type other) { return this->state->set_scheduler(::std::move(other)); }
98+
auto notify_complete() -> ::std::coroutine_handle<> { return this->get_state()->complete(); }
99+
scheduler_type change_scheduler(scheduler_type other) {
100+
return this->get_state()->set_scheduler(::std::move(other));
101+
}
208102

209-
auto get_scheduler() const noexcept -> scheduler_type { return this->state->get_scheduler(); }
103+
auto get_scheduler() const noexcept -> scheduler_type { return this->get_state()->get_scheduler(); }
210104
auto get_allocator() const noexcept -> allocator_type { return this->allocator; }
211-
auto get_stop_token() const noexcept -> stop_token_type { return this->state->get_stop_token(); }
212-
auto get_context() const noexcept -> const Environment& { return this->state->get_context(); }
105+
auto get_stop_token() const noexcept -> stop_token_type { return this->get_state()->get_stop_token(); }
106+
auto get_environment() const noexcept -> const Environment& { return this->get_state()->get_environment(); }
213107

214108
private:
215-
allocator_type allocator{};
216-
::std::optional<scheduler_type> scheduler{};
217-
::beman::task::detail::state_base<Environment>* state{};
109+
using env_t = ::beman::task::detail::promise_env<promise_type>;
110+
111+
allocator_type allocator{};
112+
::std::optional<scheduler_type> scheduler{};
218113
};
219-
#endif
220114
} // namespace beman::task::detail
221115

222116
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)