xtd 1.0.0
Loading...
Searching...
No Matches
task_completion_source.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "task_result.hpp"
7#include "../../new_ptr.hpp"
8#include "../../ptr.hpp"
9
11namespace xtd {
13 namespace threading {
15 namespace tasks {
16 template<class result_t>
17 class task_completion_source : public xtd::object {
18 public:
20
22 task_completion_source() : data_ {xtd::new_ptr<data>([this] {
23 //if (data_->task.token().is_cancellation_requested()) throw task_canceled_exception {};
24 if (data_->exception != null) throw *data_->exception;
25 return data_->result;
26 })} {}
28
30
32 auto task() const noexcept -> const xtd::threading::tasks::task<result_t>& {return data_->task;}
34
36
38 auto set_canceled() -> void {
39 data_->cancellation.cancel();
40 data_->task.start();
41 }
42
43 template<class exception_t>
44 auto set_exception(const exception_t& exception) -> void {
45 data_->exception = xtd::ptr<xtd::exception> {exception.template memberwise_clone<exception_t>().release()};
46 *data_->exception = exception;
47 data_->task.start();
48 }
49
50 auto set_result(result_t&& result) -> void {
51 data_->result = std::forward<result_t>(result);
52 data_->task.start();
53 }
55
56 private:
57 struct data {
58 template<typename func_t>
59 data(func_t func) {task = {func, cancellation.token()};}
61 result_t result;
64 };
65 xtd::ptr<data> data_;
66 };
67 }
68 }
69}
Contains xtd::threading::cancellation_token_source class.
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.hpp:29
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
auto memberwise_clone() const -> xtd::unique_ptr_object< object_t >
Creates a shallow copy of the current object.
Signals to a xtd::threading::cancellation_token that it should be canceled.
Definition cancellation_token_source.hpp:38
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
null_ptr null
Represents a null pointer value.
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
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
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:197
Contains xtd::new_ptr method.
Contains xtd::ptr type.
Contains xtd::threading::tasks::task <result_t> class.