19 template<
class result_t>
20 class task :
public xtd::threading::tasks::basic_task<result_t> {
25 std::coroutine_handle<promise_type> self;
27 auto final_suspend()
noexcept {
28 struct final_awaiter {
31 bool await_ready()
noexcept {
return false;}
32 void await_resume()
noexcept {}
33 void await_suspend(std::coroutine_handle<promise_type> handle)
noexcept {
34 promise.task->start();
35 promise.self.destroy();
38 return final_awaiter {*
this};
42 self = std::coroutine_handle<promise_type>::from_promise(*
this);
45 std::suspend_never initial_suspend() {
return {};}
46 void return_value(
const result_t& result) {task->template basic_task<result_t>::data_->result = result;}
47 void unhandled_exception() {exception = task->template basic_task<result_t>::data_->exception;}
53 bool await_ready()
const noexcept {
return task.is_completed();}
54 void await_suspend(std::coroutine_handle<> handle) {task.continue_with([handle] {handle.resume();});}
55 result_t await_resume() {
56 if (task.is_faulted()) task.rethrow_exception();
63 task(task&&) =
default;
64 task(
const task&) =
default;
65 auto operator=(task&&) -> task& =
default;
66 auto operator=(
const task&) -> task& =
default;
68 ~task() {
if (handle_.is_unique() && *handle_) handle_->destroy();}
76 task(
const xtd::func<result_t>&
func,
const xtd::threading::cancellation_token& cancellation_token) : basic_task<result_t> {
func, cancellation_token} {}
82 task(
const std::function<result_t()>&
func) : basic_task<result_t> {
func} {}
83 task(
const std::function<result_t()>&
func,
const xtd::threading::cancellation_token& cancellation_token) : basic_task<result_t> {
func, cancellation_token} {}
84 task(
const std::function<result_t(
const xtd::any_object&)>&
func,
const xtd::any_object& state) : basic_task<result_t> {
func, state} {}
85 task(
const std::function<result_t(
const xtd::any_object&)>&
func,
const xtd::any_object& state,
const xtd::threading::cancellation_token& cancellation_token) : basic_task<result_t> {
func, state, cancellation_token} {}
92 [[nodiscard]]
auto result() const noexcept -> const result_t& {
93 static_cast<basic_task<result_t>&
>(
const_cast<task&
>(*this)).wait();
94 return basic_task<result_t>::data_->result;
101 [[nodiscard]]
static auto from_result(result_t result) -> task {
103 t.data_->result = result;
105 t.basic_task<result_t>::data_->end_event.set();
113 auto operator co_await()
noexcept {
return awaiter {*
this};}
Contains xtd::threading::tasks::basic_task <> class.
Represents an exception whose state is captured at a certain point in code.
Definition exception_dispatch_info.hpp:32
Represents an asynchronous operation.
Definition task_result.hpp:20
xtd::delegate< result_t(arguments_t... arguments)> func
Represents a delegate that has variables parameters and returns a value of the type specified by the ...
Definition func.hpp:27
@ ran_to_completion
The task completed execution successfully.
Definition task_status.hpp:35
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ t
The T key.
Definition console_key.hpp:126
The xtd::threading::tasks namespace provides types that simplify the work of writing concurrent and a...
Definition itask.hpp:14
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Definition task_result.hpp:50
Definition task_result.hpp:22
Contains xtd::threading::tasks::task_factory <result_t> class.