6#if !defined(__XTD_CORE_INTERNAL__)
7#error "Do not include this file: Internal use only"
11#include "../threading/manual_reset_event.h"
12#include "../delegate.h"
15template<
typename result_t =
void>
16inline result_t __xtd_delegate_any_cast(std::any value) {
17 return value.has_value() ? std::any_cast<result_t>(value) : result_t {};
21inline void __xtd_delegate_any_cast<void>(std::any value) {
25inline std::any __xtd_delegate_invoker(std::function<
void()> invoke) {
30template<
typename... arguments_t>
31inline std::any __xtd_delegate_invoker(std::function<
void(arguments_t...)> invoke, arguments_t... arguments) {
36template<
typename result_t>
37inline std::any __xtd_delegate_invoker(std::function<result_t()> invoke) {
38 return std::make_any<result_t>(invoke());
41template<
typename result_t,
typename... arguments_t>
42inline std::any __xtd_delegate_invoker(std::function<result_t(arguments_t...)> invoke, arguments_t... arguments) {
43 return std::make_any<result_t>(invoke(arguments...));
46template<
typename result_t>
47struct xtd::delegate<result_t()>::async_result_invoke::data {
51 bool is_completed =
false;
55template<
typename result_t>
56xtd::delegate<result_t()>::async_result_invoke::async_result_invoke(
xtd::async_callback async_callback, std::any async_state) : data_(
xtd::
new_sptr<data>()) {
58 data_->async_state = async_state;
61template<
typename result_t>
62std::any xtd::delegate<result_t()>::async_result_invoke::async_state() const noexcept {
63 return data_->async_state;
66template<
typename result_t>
68 return data_->async_event;
71template<
typename result_t>
72bool xtd::delegate<result_t()>::async_result_invoke::completed_synchronously() const noexcept {
76template<
typename result_t>
77bool xtd::delegate<result_t()>::async_result_invoke::is_completed() const noexcept {
78 return data_->is_completed;
81template<
typename result_t>
86template<
typename result_t>
88 return begin_invoke(async_callback, *
this);
91template<
typename result_t>
93 auto async = xtd::new_sptr<async_result_invoke>(async_callback, async_state);
94 threading::thread_pool::queue_user_work_item([&, async = async] {
95 async->data_->result = __xtd_delegate_invoker(function_t {std::bind(&xtd::delegate<result_t()>::invoke,
this)});
96 async->data_->is_completed =
true;
97 async->data_->async_event.set();
98 async->data_->async_callback(async);
103template<
typename result_t>
104result_t xtd::delegate<result_t()>::end_invoke(async_result async) {
107 return __xtd_delegate_any_cast<result_t>(
async_result->data_->result);
110template<
typename result_t,
typename... arguments_t>
111struct xtd::delegate<result_t(arguments_t...)>::async_result_invoke::data {
114 std::any async_state;
115 bool is_completed =
false;
119template<
typename result_t,
typename... arguments_t>
120xtd::delegate<result_t(arguments_t...)>::async_result_invoke::async_result_invoke(
xtd::async_callback async_callback, std::any async_state) : data_(
xtd::
new_sptr<data>()) {
122 data_->async_state = async_state;
125template<
typename result_t,
typename... arguments_t>
126std::any xtd::delegate<result_t(arguments_t...)>::async_result_invoke::async_state() const noexcept {
127 return data_->async_state;
130template<
typename result_t,
typename... arguments_t>
132 return data_->async_event;
135template<
typename result_t,
typename... arguments_t>
136bool xtd::delegate<result_t(arguments_t...)>::async_result_invoke::completed_synchronously() const noexcept {
140template<
typename result_t,
typename... arguments_t>
141bool xtd::delegate<result_t(arguments_t...)>::async_result_invoke::is_completed() const noexcept {
142 return data_->is_completed;
145template<
typename result_t,
typename... arguments_t>
146xtd::async_result xtd::delegate<result_t(arguments_t...)>::begin_invoke(arguments_t... arguments) {
150template<
typename result_t,
typename... arguments_t>
152 return begin_invoke(async_callback, *
this, arguments...);
155template<
typename result_t,
typename... arguments_t>
157 auto async = xtd::new_sptr<async_result_invoke>(async_callback, async_state);
158 threading::thread_pool::queue_user_work_item([&, async = async, ...arguments = arguments] {
159 async->data_->result = __xtd_delegate_invoker(function_t {std::bind(&xtd::delegate<result_t(arguments_t...)>::invoke,
this, arguments...)}, arguments...);
160 async->data_->is_completed =
true;
161 async->data_->async_event.set();
162 async->data_->async_callback(async);
167template<
typename result_t,
typename... arguments_t>
168result_t xtd::delegate<result_t(arguments_t...)>::end_invoke(async_result async) {
171 return __xtd_delegate_any_cast<result_t>(
async_result->data_->result);
Represents a thread synchronization event that, when signaled, must be reset manually....
Definition manual_reset_event.h:35
Encapsulates operating system specific objects that wait for exclusive access to shared resources.
Definition wait_handle.h:52
xtd::sptr< xtd::iasync_result > async_result
Represents the status of an asynchronous operation.
Definition async_result.h:19
delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.h:36
sptr< type_t > new_sptr(args_t &&... args)
xtd::news_sptr operator creates a xtd::sptr object.
Definition new_sptr.h:24
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10