CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
result_task.h
1
2#pragma once
3
4#include <functional>
5#include <system/object.h>
6#include <system/func.h>
7#include <system/threading/tasks/task.h>
8
9namespace System { namespace Runtime { namespace CompilerServices {
10template<typename T>
11class ResultTaskAwaiter;
12template<typename T>
13class ConfiguredResultTaskAwaitable;
14}}
15
16namespace Threading { namespace Tasks {
17
22template<typename T>
23class ResultTask final : public Task
24{
25 using ThisType = ResultTask<T>;
26 using BaseType = Task;
27
28public:
31 ResultTask(const Func<T>& function) : Task(MakeFunction(function))
32 {
33 }
34
41 {
42 Wait();
43 return m_result;
44 }
45
51 {
52 return {MakeSharedPtr(this), continueOnCapturedContext};
53 }
54
56
61 TaskPtr ContinueWith(const Action<RTaskPtr<T>>& continuationAction)
62 {
63 auto result = MakeObject<Task>(
64 std::function<void()>([continuationAction, task = MakeSharedPtr(this)] { continuationAction(task); }));
65 ContinueWithCore(result);
66 return result;
67 }
68
74 template<typename TNewResult>
75 RTaskPtr<TNewResult> ContinueWith(const Func<RTaskPtr<T>, TNewResult>& continuationFunction)
76 {
77 auto result = MakeObject<ResultTask<TNewResult>>(std::function<TNewResult()>(
78 [continuationFunction, task = MakeSharedPtr(this)] { return continuationFunction(task); }));
79 ContinueWithCore(result);
80 return result;
81 }
82
87 {
88 return {MakeSharedPtr(this)};
89 }
90
92
95 {
96 }
97
99 ResultTask(const T& result) : m_result(result)
100 {
102 }
103
105 void set_Result(const T& result)
106 {
107 m_result = result;
108 }
109
111 void Complete(const T& result)
112 {
113 set_Result(result);
115 }
116
117private:
119 FunctionT MakeFunction(const Func<T>& func)
120 {
121 return [func, this]
122 {
123 set_Result(func());
124 };
125 }
126
128 T m_result;
129};
130
131}} // namespace Threading::Tasks
132
133namespace Runtime { namespace CompilerServices {
134
135template <typename T>
137{
138public:
139 ResultTaskAwaiter(const RTaskPtr<T>& task) : m_task(task)
140 {}
141
142 bool get_IsCompleted() const
143 {
144 return m_task->get_IsCompleted();
145 }
146
147 void OnCompleted(const Action<>& continuation)
148 {
149 m_task->AddCompletionAction(continuation);
150 }
151
152 T GetResult() const
153 {
154 return m_task->get_Result();
155 }
156
158
159private:
160 RTaskPtr<T> m_task;
161};
162
163template <typename T>
165{
166public:
167 ConfiguredResultTaskAwaitable(const RTaskPtr<T>& task, bool continueOnCapturedContext)
168 : m_task(task), m_continueOnCapturedContext(continueOnCapturedContext)
169 {}
170
172 {
173 auto result = m_task->GetAwaiter();
174 result.continueOnCapturedContext = m_continueOnCapturedContext;
175 return result;
176 }
177
178private:
179 RTaskPtr<T> m_task;
180 bool m_continueOnCapturedContext;
181};
182
183}} // namespace Runtime::CompilerServices
184
185} // namespace System
Function delegate. This type should be allocated on stack and passed to functions by value or by refe...
Definition: func.h:82
Runtime::CompilerServices::ResultTaskAwaiter< T > GetAwaiter() const
Definition: result_task.h:171
ConfiguredResultTaskAwaitable(const RTaskPtr< T > &task, bool continueOnCapturedContext)
Definition: result_task.h:167
T GetResult() const
Definition: result_task.h:152
ResultTaskAwaiter(const RTaskPtr< T > &task)
Definition: result_task.h:139
void OnCompleted(const Action<> &continuation)
Definition: result_task.h:147
bool continueOnCapturedContext
Definition: result_task.h:157
bool get_IsCompleted() const
Definition: result_task.h:142
A Task specialization that returns a result value upon completion.
Definition: result_task.h:24
Runtime::CompilerServices::ConfiguredResultTaskAwaitable< T > ConfigureAwait(bool continueOnCapturedContext) const
Configures how awaits on this result task should behave regarding context capture.
Definition: result_task.h:50
void Complete(const T &result)
Sets the result value for the task and completes it.
Definition: result_task.h:111
T get_Result()
Gets the result of the asynchronous operation.
Definition: result_task.h:40
TaskPtr ContinueWith(const Action< RTaskPtr< T > > &continuationAction)
Creates a continuation that executes when the result task completes.
Definition: result_task.h:61
ResultTask(const Func< T > &function)
Constructs a ResultTask with a function that returns a value.
Definition: result_task.h:31
ResultTask(const T &result)
Internal constructor for creating result tasks with specified result.
Definition: result_task.h:99
ResultTask()
Internal implementation. Not for user code.
Definition: result_task.h:94
void set_Result(const T &result)
Sets the result value for the task.
Definition: result_task.h:105
Runtime::CompilerServices::ResultTaskAwaiter< T > GetAwaiter() const
Gets an awaiter for this result task for use with Await.
Definition: result_task.h:86
RTaskPtr< TNewResult > ContinueWith(const Func< RTaskPtr< T >, TNewResult > &continuationFunction)
Creates a continuation that executes when the result task completes.
Definition: result_task.h:75
Represents an asynchronous operation that can be awaited and composed with other tasks.
Definition: task.h:30
void Wait()
Waits for the task to complete.
Task()
Internal constructor for creating uninitialized tasks.
void Complete()
Marks the task as completed and finishes task.
TaskPtr ContinueWith(const Action< TaskPtr > &continuationAction)
Creates a continuation that executes when the task completes.
void ContinueWithCore(const TaskPtr &continuationTask)
Internal implementation for adding continuations.
std::function< void()> FunctionT
Internal implementation. Not for user code.
Definition: task.h:126
void set_Status(TaskStatus status)
Sets the task status.
Definition: db_command.h:9
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650