CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
task.h
1
2#pragma once
3
4#include <functional>
5#include <atomic>
6#include <mutex>
7#include <system/idisposable.h>
8#include <system/threading/tasks/task_status.h>
9#include <system/threading/cancellation_token.h>
10#include <system/action.h>
11
12namespace System {
13template<typename T>
14class Nullable;
15namespace Threading { namespace Tasks {
16class TaskScheduler;
17}}
18namespace Runtime { namespace CompilerServices {
19class TaskAwaiter;
20class ConfiguredTaskAwaitable;
21}}
22
23namespace Threading { namespace Tasks {
24
28class ASPOSECPP_SHARED_CLASS Task : public IDisposable
29{
30 using ThisType = Task;
31
32public:
35 ASPOSECPP_SHARED_API Task(const Action<>& action);
39 ASPOSECPP_SHARED_API Task(const Action<>& action, const CancellationToken& cancellationToken);
43 ASPOSECPP_SHARED_API Task(const Action<SharedPtr<Object>>& action, const SharedPtr<Object>& state);
48 ASPOSECPP_SHARED_API Task(const Action<SharedPtr<Object>>& action, const SharedPtr<Object>& state,
49 const CancellationToken& cancellationToken);
51 ASPOSECPP_SHARED_API ~Task();
52
53 static ASPOSECPP_SHARED_API Nullable<int32_t> get_CurrentId();
56 static ASPOSECPP_SHARED_API const TaskPtr& get_CompletedTask();
59 ASPOSECPP_SHARED_API bool get_IsCompleted() const;
62 bool get_IsCanceled() const {return m_status == TaskStatus::Canceled;}
65 TaskStatus get_Status() const {return m_status;}
68 int32_t get_Id() const {return m_id;}
71 const SharedPtr<Object>& get_AsyncState() const {return m_state;}
74 bool get_IsFaulted() const {return m_status == TaskStatus::Faulted;}
75
79 ASPOSECPP_SHARED_API TaskPtr ContinueWith(const Action<TaskPtr>& continuationAction);
80
81
88 ASPOSECPP_SHARED_API Runtime::CompilerServices::ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) const;
89
90
93 ASPOSECPP_SHARED_API void RunSynchronously();
97 ASPOSECPP_SHARED_API void RunSynchronously(const SharedPtr<TaskScheduler>& scheduler);
100 ASPOSECPP_SHARED_API void Start();
104 ASPOSECPP_SHARED_API void Start(const SharedPtr<TaskScheduler>& scheduler);
109 ASPOSECPP_SHARED_API void Wait(const CancellationToken& cancellationToken) const;
112 ASPOSECPP_SHARED_API void Wait() const;
113
115 ASPOSECPP_SHARED_API void Dispose() override;
116
118
120 using FunctionT = std::function<void()>;
121
123 ASPOSECPP_SHARED_API Task();
125 void set_Function(const FunctionT& fnc) {m_function = fnc;}
127 ASPOSECPP_SHARED_API void set_Status(TaskStatus status);
129 ASPOSECPP_SHARED_API TaskScheduler* get_Scheduler() const {return m_scheduler;}
131 ASPOSECPP_SHARED_API void set_Scheduler(TaskScheduler* scheduler) {m_scheduler = scheduler;}
132
134 ASPOSECPP_SHARED_API void Activate(TaskScheduler* = nullptr);
136 ASPOSECPP_SHARED_API void Execute();
138 ASPOSECPP_SHARED_API void AddContinuation(const Action<>& continuation);
140 ASPOSECPP_SHARED_API void Complete();
141
142protected:
144 ASPOSECPP_SHARED_API void ContinueWithCore(const TaskPtr& continuationTask);
146 ASPOSECPP_SHARED_API void ExecuteContinuations();
147
149 ASPOSECPP_SHARED_API void Finish();
150
151private:
152 // Task ID
153 int32_t m_id;
155 FunctionT m_function;
157 TaskStatus m_status;
159 std::exception_ptr m_exception;
161 CancellationToken m_cancellation_token;
163 TaskScheduler* m_scheduler;
165 SharedPtr<Object> m_state;
167 std::list<Action<>> m_continuations;
169 std::mutex m_continuations_mutex;
170
172 static std::atomic<int32_t> m_next_id;
173};
174
175}} // namespace Threading::Tasks
176
177namespace Runtime { namespace CompilerServices {
178
179class ASPOSECPP_SHARED_CLASS TaskAwaiter
180{
181public:
182 TaskAwaiter(const TaskPtr& task) : m_task(task) {}
183
184 ASPOSECPP_SHARED_API bool get_IsCompleted() const;
185 ASPOSECPP_SHARED_API void OnCompleted(const Action<>& continuation);
186 ASPOSECPP_SHARED_API void GetResult() const;
187
188 bool continueOnCapturedContext = false;
189
190private:
191 TaskPtr m_task;
192};
193
194class ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable
195{
196public:
197 ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable(const TaskPtr& task, bool continueOnCapturedContext);
199
200private:
201 TaskPtr m_task;
202 bool m_continueOnCapturedContext;
203};
204
205}}
206
207} // 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:75
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:182
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:29
bool get_IsFaulted() const
Gets whether the task completed due to an unhandled exception.
Definition: task.h:74
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:125
bool get_IsCanceled() const
Gets whether the task completed due to cancellation.
Definition: task.h:62
void Start()
Starts the task execution using the default scheduler.
int32_t get_Id() const
Gets the ID for task.
Definition: task.h:68
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.
TaskStatus get_Status() const
Gets the current status of the task.
Definition: task.h:65
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:129
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 Wait() const
Waits for the task to complete.
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:71
void Start(const SharedPtr< TaskScheduler > &scheduler)
Starts the task execution using the specified scheduler.
void set_Scheduler(TaskScheduler *scheduler)
Sets the scheduler associated with this task.
Definition: task.h:131
std::function< void()> FunctionT
Internal implementation. Not for user code.
Definition: task.h:120
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.
void Wait(const CancellationToken &cancellationToken) const
Waits for the task to complete with cancellation support.
Represents an object that handles the low-level work of queuing tasks onto threads.
Definition: task_scheduler.h:12
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