xtd 0.2.0
Loading...
Searching...
No Matches
task_result.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "basic_task.hpp"
6
8namespace xtd {
10 namespace threading {
12 namespace tasks {
19 template<class result_t>
20 class task : public xtd::threading::tasks::basic_task<result_t> {
21 public:
22 struct promise_type {
25 std::coroutine_handle<promise_type> self;
26
27 auto final_suspend() noexcept {
28 struct final_awaiter {
29 promise_type& promise;
30
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();
36 }
37 };
38 return final_awaiter {*this};
39 }
40 xtd::threading::tasks::task<result_t> get_return_object() {
42 self = std::coroutine_handle<promise_type>::from_promise(*this);
43 return *task;
44 }
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;}
48 };
49
50 struct awaiter {
52
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();
57 return task.result();
58 }
59 };
60
62 task() = default;
63 task(task&&) = default;
64 task(const task&) = default;
65 auto operator=(task&&) -> task& = default;
66 auto operator=(const task&) -> task& = default;
67 task(const std::coroutine_handle<typename task<result_t>::promise_type>& handle) { {*handle_ = handle;}}
68 ~task() {if (handle_.is_unique() && *handle_) handle_->destroy();}
69
71
73
75 task(const xtd::func<result_t>& func) : basic_task<result_t> {func} {}
76 task(const xtd::func<result_t>& func, const xtd::threading::cancellation_token& cancellation_token) : basic_task<result_t> {func, cancellation_token} {}
77 task(const xtd::func<result_t, const xtd::any_object&>& func, const xtd::any_object& state) : basic_task<result_t> {func, state} {}
78 task(const xtd::func<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} {}
80
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} {}
87
88
90
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;
95 }
97
99
101 [[nodiscard]] static auto from_result(result_t result) -> task {
102 auto t = task {};
103 t.data_->result = result;
104 t.basic_task<result_t>::data_->status = xtd::threading::tasks::task_status::ran_to_completion;
105 t.basic_task<result_t>::data_->end_event.set();
106 return t;
107 }
109
111
113 auto operator co_await() noexcept {return awaiter {*this};}
115
116 private:
118 };
119 }
120 }
121}
122
123#include "task_factory.hpp"
124
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.