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
59 TaskPtr ContinueWith(const Action<RTaskPtr<T>>& continuationAction)
60 {
61 auto result = MakeObject<Task>(
62 std::function<void()>([continuationAction, task = MakeSharedPtr(this)] { continuationAction(task); }));
63 ContinueWithCore(result);
64 return result;
65 }
66
71 {
72 return {MakeSharedPtr(this)};
73 }
74
76
79 {
80 }
81
83 void set_Result(const T& result)
84 {
85 m_result = result;
86 }
87
88private:
90 FunctionT MakeFunction(const Func<T>& func)
91 {
92 return [func, this]
93 {
94 set_Result(func());
95 };
96 }
97
99 T m_result;
100};
101
102}} // namespace Threading::Tasks
103
104namespace Runtime { namespace CompilerServices {
105
106template <typename T>
108{
109public:
110 ResultTaskAwaiter(const RTaskPtr<T>& task) : m_task(task)
111 {}
112
113 bool get_IsCompleted() const
114 {
115 return m_task->get_IsCompleted();
116 }
117
118 void OnCompleted(const Action<>& continuation)
119 {
120 m_task->AddContinuation(continuation);
121 }
122
123 T GetResult() const
124 {
125 return m_task->get_Result();
126 }
127
129
130private:
131 RTaskPtr<T> m_task;
132};
133
134template <typename T>
136{
137public:
138 ConfiguredResultTaskAwaitable(const RTaskPtr<T>& task, bool continueOnCapturedContext)
139 : m_task(task), m_continueOnCapturedContext(continueOnCapturedContext)
140 {}
141
143 {
144 auto result = m_task->GetAwaiter();
145 result.continueOnCapturedContext = m_continueOnCapturedContext;
146 return result;
147 }
148
149private:
150 RTaskPtr<T> m_task;
151 bool m_continueOnCapturedContext;
152};
153
154}} // namespace Runtime::CompilerServices
155
156} // 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:142
ConfiguredResultTaskAwaitable(const RTaskPtr< T > &task, bool continueOnCapturedContext)
Definition: result_task.h:138
T GetResult() const
Definition: result_task.h:123
ResultTaskAwaiter(const RTaskPtr< T > &task)
Definition: result_task.h:110
void OnCompleted(const Action<> &continuation)
Definition: result_task.h:118
bool continueOnCapturedContext
Definition: result_task.h:128
bool get_IsCompleted() const
Definition: result_task.h:113
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
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:59
ResultTask(const Func< T > &function)
Constructs a ResultTask with a function that returns a value.
Definition: result_task.h:31
ResultTask()
Internal implementation. Not for user code.
Definition: result_task.h:78
void set_Result(const T &result)
Sets the result value for the task.
Definition: result_task.h:83
Runtime::CompilerServices::ResultTaskAwaiter< T > GetAwaiter() const
Gets an awaiter for this result task for use with Await.
Definition: result_task.h:70
Represents an asynchronous operation that can be awaited and composed with other tasks.
Definition: task.h:27
void Wait()
Waits for the task to complete.
Task()
Internal constructor for creating uninitialized tasks.
void ContinueWithCore(const TaskPtr &continuationTask)
Internal implementation for adding continuations.
std::function< void()> FunctionT
Internal implementation. Not for user code.
Definition: task.h:118
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