xtd 0.2.0
Loading...
Searching...
No Matches
__delegate.h
Go to the documentation of this file.
1
4#pragma once
6#if !defined(__XTD_CORE_INTERNAL__)
7#error "Do not include this file: Internal use only"
8#endif
10
11#include "../threading/manual_reset_event.h"
12#include "../delegate.h"
13
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 {};
18}
19
20template<>
21inline void __xtd_delegate_any_cast<void>(std::any value) {
22 return;
23}
24
25inline std::any __xtd_delegate_invoker(std::function<void()> invoke) {
26 invoke();
27 return std::any {};
28}
29
30template<typename... arguments_t>
31inline std::any __xtd_delegate_invoker(std::function<void(arguments_t...)> invoke, arguments_t... arguments) {
32 invoke(arguments...);
33 return std::any {};
34}
35
36template<typename result_t>
37inline std::any __xtd_delegate_invoker(std::function<result_t()> invoke) {
38 return std::make_any<result_t>(invoke());
39}
40
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...));
44}
45
46template<typename result_t>
47struct xtd::delegate<result_t()>::async_result_invoke::data {
50 std::any async_state;
51 bool is_completed = false;
52 std::any result;
53};
54
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>()) {
57 data_->async_callback = async_callback;
58 data_->async_state = async_state;
59}
60
61template<typename result_t>
62std::any xtd::delegate<result_t()>::async_result_invoke::async_state() const noexcept {
63 return data_->async_state;
64}
65
66template<typename result_t>
67xtd::threading::wait_handle& xtd::delegate<result_t()>::async_result_invoke::async_wait_handle() noexcept {
68 return data_->async_event;
69}
70
71template<typename result_t>
72bool xtd::delegate<result_t()>::async_result_invoke::completed_synchronously() const noexcept {
73 return false;
74}
75
76template<typename result_t>
77bool xtd::delegate<result_t()>::async_result_invoke::is_completed() const noexcept {
78 return data_->is_completed;
79}
80
81template<typename result_t>
82inline xtd::async_result xtd::delegate<result_t()>::begin_invoke() {
83 return begin_invoke(xtd::async_callback {}, *this);
84}
85
86template<typename result_t>
87inline xtd::async_result xtd::delegate<result_t()>::begin_invoke(xtd::async_callback async_callback) {
88 return begin_invoke(async_callback, *this);
89}
90
91template<typename result_t>
92xtd::async_result xtd::delegate<result_t()>::begin_invoke(xtd::async_callback async_callback, std::any async_state) {
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);
99 });
100 return async;
101}
102
103template<typename result_t>
104result_t xtd::delegate<result_t()>::end_invoke(async_result async) {
105 auto async_result = as<async_result_invoke>(async);
106 async_result->data_->async_event.wait_one();
107 return __xtd_delegate_any_cast<result_t>(async_result->data_->result);
108}
109
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;
116 std::any result;
117};
118
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>()) {
121 data_->async_callback = async_callback;
122 data_->async_state = async_state;
123}
124
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;
128}
129
130template<typename result_t, typename... arguments_t>
131xtd::threading::wait_handle& xtd::delegate<result_t(arguments_t...)>::async_result_invoke::async_wait_handle() noexcept {
132 return data_->async_event;
133}
134
135template<typename result_t, typename... arguments_t>
136bool xtd::delegate<result_t(arguments_t...)>::async_result_invoke::completed_synchronously() const noexcept {
137 return false;
138}
139
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;
143}
144
145template<typename result_t, typename... arguments_t>
146xtd::async_result xtd::delegate<result_t(arguments_t...)>::begin_invoke(arguments_t... arguments) {
147 return begin_invoke(xtd::async_callback {}, *this, arguments...);
148}
149
150template<typename result_t, typename... arguments_t>
151xtd::async_result xtd::delegate<result_t(arguments_t...)>::begin_invoke(xtd::async_callback async_callback, arguments_t... arguments) {
152 return begin_invoke(async_callback, *this, arguments...);
153}
154
155template<typename result_t, typename... arguments_t>
156xtd::async_result xtd::delegate<result_t(arguments_t...)>::begin_invoke(xtd::async_callback async_callback, std::any async_state, arguments_t... arguments) {
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);
163 });
164 return async;
165}
166
167template<typename result_t, typename... arguments_t>
168result_t xtd::delegate<result_t(arguments_t...)>::end_invoke(async_result async) {
169 auto async_result = as<async_result_invoke>(async);
170 if (!async_result->data_->is_completed) async_result->data_->async_event.wait_one();
171 return __xtd_delegate_any_cast<result_t>(async_result->data_->result);
172}
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