CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
task_utils.h
1
2#pragma once
3
4#include <functional>
5#include <system/object.h>
6#include <system/collections/ienumerable.h>
7#include <system/threading/tasks/task.h>
8#include <system/threading/tasks/result_task.h>
9#include <system/threading/tasks/async.h>
10#include <system/array.h>
11
12#undef Yield
13
14namespace System {
15
16namespace Runtime { namespace CompilerServices {
17
18class ASPOSECPP_SHARED_CLASS YieldAwaitable
19{
20public:
21 class ASPOSECPP_SHARED_CLASS YieldAwaiter
22 {
23 public:
24 bool get_IsCompleted() const {return false;}
25 ASPOSECPP_SHARED_API void OnCompleted(const Action<>& continuation);
26 void GetResult() const {}
27
28 static constexpr bool continueOnCapturedContext = false;
29 };
30
32 {
33 return YieldAwaiter();
34 }
35};
36
37}} // namespace Runtime::CompilerServices
38
39namespace Threading {
40
41class CancellationToken;
42
43namespace Tasks {
44
49ASPOSECPP_SHARED_API TaskPtr Delay(int32_t millisecondsDelay);
50ASPOSECPP_SHARED_API TaskPtr Delay(int32_t millisecondsDelay, const CancellationToken& cancellationToken);
51
52//
53// Summary:
54// Creates a System.Threading.Tasks.Task that has completed with a specified exception.
55//
56// Parameters:
57// exception:
58// The exception with which to complete the task.
59//
60// Returns:
61// The faulted task.
62ASPOSECPP_SHARED_API TaskPtr FromException(const Exception& exception);
63
64//
65// Summary:
66// Creates a System.Threading.Tasks.Task`1 that's completed with a specified exception.
67//
68// Parameters:
69// exception:
70// The exception with which to complete the task.
71//
72// Type parameters:
73// TResult:
74// The type of the result returned by the task.
75//
76// Returns:
77// The faulted task.
78template<typename TResult>
80{
81 return MakeAsync<TResult>([=](System::Details::ResultAsyncContext<TResult>& __context) { exception.Throw(); });
82}
83
84//
85// Summary:
86// Creates a System.Threading.Tasks.Task`1 that's completed successfully with the
87// specified result.
88//
89// Parameters:
90// result:
91// The result to store into the completed task.
92//
93// Type parameters:
94// TResult:
95// The type of the result returned by the task.
96//
97// Returns:
98// The successfully completed task.
99template <typename TResult>
101{
102 return MakeObject<ResultTask<TResult>>(result);
103}
104
105ASPOSECPP_SHARED_API TaskPtr Run(const Action<>& action);
106ASPOSECPP_SHARED_API TaskPtr Run(const Action<>& action, const CancellationToken& cancellationToken);
107ASPOSECPP_SHARED_API TaskPtr Run(const Func<TaskPtr>& function);
108
109template <typename TResult>
111{
112 auto result = MakeObject<ResultTask<TResult>>(function);
113 result->Start();
114 return result;
115}
116
117ASPOSECPP_SHARED_API TaskPtr WhenAll(const ArrayPtr<TaskPtr>& tasks);
119
120//
121// Summary:
122// Creates a task that will complete when all of the System.Threading.Tasks.Task`1
123// objects in an enumerable collection have completed.
124//
125// Parameters:
126// tasks:
127// The tasks to wait on for completion.
128//
129// Type parameters:
130// TResult:
131// The type of the completed task.
132//
133// Returns:
134// A task that represents the completion of all of the supplied tasks.
135//
136// Exceptions:
137// T:System.ArgumentNullException:
138// The tasks argument was null.
139//
140// T:System.ArgumentException:
141// The tasks collection contained a null task.
142template <typename T>
144{
145 return WhenAll(tasks->LINQ_ToArray());
146}
147
148//
149// Summary:
150// Creates a task that will complete when all of the System.Threading.Tasks.Task`1
151// objects in an array have completed.
152//
153// Parameters:
154// tasks:
155// The tasks to wait on for completion.
156//
157// Type parameters:
158// TResult:
159// The type of the completed task.
160//
161// Returns:
162// A task that represents the completion of all of the supplied tasks.
163//
164// Exceptions:
165// T:System.ArgumentNullException:
166// The tasks argument was null.
167//
168// T:System.ArgumentException:
169// The tasks array contained a null task.
170template <typename T>
172{
173 //return WhenAll(tasks->LINQ_Cast<TaskPtr>())->ContinueWith<T>
174
175 return MakeAsync<ArrayPtr<T>>([=](System::Details::ResultAsyncContext<ArrayPtr<T>>& __context)
176 {
177 for (auto& task : tasks)
178 {
179 if (!task->get_IsCompleted())
180 {
181 return;
182 }
183 }
184
185 auto size = tasks->get_Length();
186 auto result = MakeArray<T>(size);
187 for (auto i = 0; i < size; ++ i)
188 {
189 result[i] = tasks[i]->get_Result();
190 }
191 __context.Return(result);
192 });
193}
194
196
197}}} // namespace System::Threading::Tasks
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:25
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:113
Function delegate. This type should be allocated on stack and passed to functions by value or by refe...
Definition: func.h:82
bool get_IsCompleted() const
Definition: task_utils.h:24
YieldAwaiter GetAwaiter() const
Definition: task_utils.h:31
Propagates notification that operations should be canceled. This class provides a mechanism for coope...
Definition: cancellation_token.h:43
void Start()
Starts the task execution using the default scheduler.
TaskPtr Delay(int32_t millisecondsDelay)
Creates a task that completes after a time delay.
TaskPtr WhenAll(const ArrayPtr< TaskPtr > &tasks)
TaskPtr Run(const Action<> &action)
TaskPtr FromException(const Exception &exception)
RTaskPtr< TResult > FromResult(TResult result)
Definition: task_utils.h:100
Runtime::CompilerServices::YieldAwaitable Yield()
Definition: db_command.h:9
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40