CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
task.h
1
2#pragma once
3
4#include <functional>
5#include <system/idisposable.h>
6#include <system/threading/tasks/task_status.h>
7#include <system/threading/cancellation_token.h>
8#include <system/action.h>
9
10namespace System {
11template<typename T>
12class Nullable;
13namespace Threading { namespace Tasks {
14class TaskScheduler;
15}}
16namespace Runtime { namespace CompilerServices {
17class TaskAwaiter;
18class ConfiguredTaskAwaitable;
19}}
20
21namespace Threading { namespace Tasks {
22
26class ASPOSECPP_SHARED_CLASS Task : public IDisposable
27{
28 using ThisType = Task;
29
30public:
33 ASPOSECPP_SHARED_API Task(const Action<>& action);
37 ASPOSECPP_SHARED_API Task(const Action<>& action, const CancellationToken& cancellationToken);
41 ASPOSECPP_SHARED_API Task(const Action<SharedPtr<Object>>& action, const SharedPtr<Object>& state);
46 ASPOSECPP_SHARED_API Task(const Action<SharedPtr<Object>>& action, const SharedPtr<Object>& state,
47 const CancellationToken& cancellationToken);
49 ASPOSECPP_SHARED_API ~Task();
50
51 static ASPOSECPP_SHARED_API Nullable<int32_t> get_CurrentId();
54 static ASPOSECPP_SHARED_API const TaskPtr& get_CompletedTask();
57 ASPOSECPP_SHARED_API bool get_IsCompleted() const;
60 bool get_IsCanceled() const {return m_status == TaskStatus::Canceled;}
63 TaskStatus get_Status() const {return m_status;}
66 int32_t get_Id() const {return m_id;}
69 const SharedPtr<Object>& get_AsyncState() const {return m_state;}
72 bool get_IsFaulted() const {return m_status == TaskStatus::Faulted;}
73
77 ASPOSECPP_SHARED_API TaskPtr ContinueWith(const Action<TaskPtr>& continuationAction);
78
79
86 ASPOSECPP_SHARED_API Runtime::CompilerServices::ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) const;
87
88
91 ASPOSECPP_SHARED_API void RunSynchronously();
95 ASPOSECPP_SHARED_API void RunSynchronously(const SharedPtr<TaskScheduler>& scheduler);
98 ASPOSECPP_SHARED_API void Start();
102 ASPOSECPP_SHARED_API void Start(const SharedPtr<TaskScheduler>& scheduler);
107 ASPOSECPP_SHARED_API void Wait(const CancellationToken& cancellationToken);
110 ASPOSECPP_SHARED_API void Wait();
111
113 ASPOSECPP_SHARED_API void Dispose() override;
114
116
118 using FunctionT = std::function<void()>;
119
121 ASPOSECPP_SHARED_API Task();
123 void set_Function(const FunctionT& fnc) {m_function = fnc;}
125 ASPOSECPP_SHARED_API void set_Status(TaskStatus status);
127 ASPOSECPP_SHARED_API TaskScheduler* get_Scheduler() const {return m_scheduler;}
128
130 ASPOSECPP_SHARED_API void Activate(TaskScheduler* = nullptr);
132 ASPOSECPP_SHARED_API void Execute();
134 ASPOSECPP_SHARED_API void AddContinuation(const Action<>& continuation);
136 ASPOSECPP_SHARED_API void Complete();
137
138protected:
140 ASPOSECPP_SHARED_API void ContinueWithCore(const TaskPtr& continuationTask);
142 ASPOSECPP_SHARED_API void ExecuteContinuations();
143
145 ASPOSECPP_SHARED_API void Finish();
146
147private:
148 // Task ID
149 int32_t m_id;
151 FunctionT m_function;
153 TaskStatus m_status;
155 std::exception_ptr m_exception;
157 CancellationToken m_cancellation_token;
159 TaskScheduler* m_scheduler;
161 SharedPtr<Object> m_state;
163 std::list<Action<>> m_continuations;
165 static int32_t m_next_id;
166};
167
168}} // namespace Threading::Tasks
169
170namespace Runtime { namespace CompilerServices {
171
172class ASPOSECPP_SHARED_CLASS TaskAwaiter
173{
174public:
175 TaskAwaiter(const TaskPtr& task) : m_task(task) {}
176
177 ASPOSECPP_SHARED_API bool get_IsCompleted() const;
178 ASPOSECPP_SHARED_API void OnCompleted(const Action<>& continuation);
179 ASPOSECPP_SHARED_API void GetResult() const;
180
181 bool continueOnCapturedContext = false;
182
183private:
184 TaskPtr m_task;
185};
186
187class ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable
188{
189public:
190 ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable(const TaskPtr& task, bool continueOnCapturedContext);
192
193private:
194 TaskPtr m_task;
195 bool m_continueOnCapturedContext;
196};
197
198}}
199
200} // namespace System::Runtime::CompilerServices
Defines method that releases resources owned by the current object. Objects of this class should only...
Definition: idisposable.h:30
Forward declaration.
Definition: nullable.h:74
Runtime::CompilerServices::TaskAwaiter GetAwaiter() const
ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable(const TaskPtr &task, bool continueOnCapturedContext)
void OnCompleted(const Action<> &continuation)
TaskAwaiter(const TaskPtr &task)
Definition: task.h:175
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Propagates notification that operations should be canceled. This class provides a mechanism for coope...
Definition: cancellation_token.h:43
Represents an asynchronous operation that can be awaited and composed with other tasks.
Definition: task.h:27
bool get_IsFaulted() const
Gets whether the task completed due to an unhandled exception.
Definition: task.h:72
void AddContinuation(const Action<> &continuation)
Adds a continuation action to be executed upon completion.
void set_Function(const FunctionT &fnc)
Sets the internal function to execute.
Definition: task.h:123
bool get_IsCanceled() const
Gets whether the task completed due to cancellation.
Definition: task.h:60
void Start()
Starts the task execution using the default scheduler.
int32_t get_Id() const
Gets the ID for task.
Definition: task.h:66
void Wait()
Waits for the task to complete.
void Dispose() override
Releases resources associated with the task.
Runtime::CompilerServices::TaskAwaiter GetAwaiter() const
Gets an awaiter for this task for use with Await.
void RunSynchronously()
Runs the task synchronously on the current thread.
Task()
Internal constructor for creating uninitialized tasks.
void Execute()
Executes the task's function.
Runtime::CompilerServices::ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) const
Configures how awaits on this task should behave regarding context capture.
Task(const Action< SharedPtr< Object > > &action, const SharedPtr< Object > &state, const CancellationToken &cancellationToken)
Constructs a Task with stateful action, state, and cancellation token.
void Wait(const CancellationToken &cancellationToken)
Waits for the task to complete with cancellation support.
TaskStatus get_Status() const
Gets the current status of the task.
Definition: task.h:63
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.
bool get_IsCompleted() const
Gets whether the task has completed.
TaskScheduler * get_Scheduler() const
Gets the scheduler associated with this task.
Definition: task.h:127
Task(const Action<> &action)
Constructs a Task with an action to execute.
void ExecuteContinuations()
Executes all registered continuations.
void Activate(TaskScheduler *=nullptr)
Activates the task for execution on a scheduler.
static Nullable< int32_t > get_CurrentId()
static const TaskPtr & get_CompletedTask()
Gets a completed task (singleton)
Task(const Action<> &action, const CancellationToken &cancellationToken)
Constructs a Task with an action and cancellation token.
Task(const Action< SharedPtr< Object > > &action, const SharedPtr< Object > &state)
Constructs a Task with a stateful action and state object.
void ContinueWithCore(const TaskPtr &continuationTask)
Internal implementation for adding continuations.
const SharedPtr< Object > & get_AsyncState() const
Gets the user-defined state object associated with the task.
Definition: task.h:69
void Start(const SharedPtr< TaskScheduler > &scheduler)
Starts the task execution using the specified scheduler.
std::function< void()> FunctionT
Internal implementation. Not for user code.
Definition: task.h:118
void RunSynchronously(const SharedPtr< TaskScheduler > &scheduler)
Runs the task synchronously using the specified scheduler.
void Finish()
Finalizes task completion and executes continuations.
void set_Status(TaskStatus status)
Sets the task status.
Represents an object that handles the low-level work of queuing tasks onto threads.
Definition: task_scheduler.h:11
TaskStatus
Definition: task_status.h:8
Definition: db_command.h:9
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40