xtd 0.2.0
Loading...
Searching...
No Matches
wtask.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "task_result.hpp"
6#include "../../func.hpp"
7
9namespace xtd {
11 namespace threading {
13 namespace tasks {
14 template<class result_t = void>
15 class wtask : public xtd::threading::tasks::task<result_t> {
16 public:
17 struct promise_type {
20 std::coroutine_handle<promise_type> self;
21
22 auto final_suspend() noexcept {
23 struct final_awaiter {
24 promise_type& promise;
25
26 bool await_ready() noexcept {return false;}
27 void await_resume() noexcept {}
28 void await_suspend(std::coroutine_handle<promise_type> handle) noexcept {
29 promise.task->start();
30 promise.self.destroy();
31 }
32 };
33 return final_awaiter {*this};
34 }
35 xtd::threading::tasks::wtask<result_t> get_return_object() {
37 self = std::coroutine_handle<promise_type>::from_promise(*this);
38 return *task;
39 }
40 std::suspend_never initial_suspend() {return {};}
41 void return_void() {}
42 void unhandled_exception() {/*exception = task->basic_task<result_t>::data_->exception;*/}
43 };
44
45 struct awaiter {
47
48 bool await_ready() const noexcept {return task.is_completed();}
49 void await_suspend(std::coroutine_handle<> handle) {task.continue_with([handle] {handle.resume();});}
50 void await_resume() {if (task.is_faulted()) task.rethrow_exception();}
51 };
52
54 wtask() = default;
55 wtask(wtask&&) = default;
56 wtask(const wtask&) = default;
57 auto operator=(wtask&&) -> wtask& = default;
58 auto operator=(const wtask&) -> wtask& = default;
60
62
64 wtask(const xtd::func<result_t>& func) : task<result_t>(func) {}
66 wtask(const xtd::func<result_t, const xtd::any_object&>& func, const xtd::any_object& state) : task<result_t>(func, state) {}
68
70 wtask(const std::function<result_t()>& func) : task<result_t>(func) {}
71 wtask(const std::function<result_t()>& func, const xtd::threading::cancellation_token& cancellation_token) : task<result_t>(func, cancellation_token) {}
72 wtask(const std::function<result_t(const xtd::any_object&)>& func, const xtd::any_object& state) : task<result_t>(func, state) {}
73
74 ~wtask() {task<result_t>::wait();}
76
78
80 auto operator co_await() noexcept {return awaiter {*this};}
82 };
83 }
84 }
85}
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
Represents an exception whose state is captured at a certain point in code.
Definition exception_dispatch_info.hpp:32
Propagates notification that operations should be canceled.
Definition cancellation_token.hpp:39
Represents an asynchronous operation.
Definition task_result.hpp:20
Definition wtask.hpp:15
Contains xtd::func delegate.
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
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
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
Contains xtd::threading::tasks::task <result_t> class.