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#include <system/aggregate_exception.h>
12
13namespace System {
14template<typename T>
15class Nullable;
16namespace Threading { namespace Tasks {
17class TaskScheduler;
18}} // namespace Threading
19namespace Runtime { namespace CompilerServices {
20class TaskAwaiter;
21class ConfiguredTaskAwaitable;
22}}
23
24namespace Threading { namespace Tasks {
25
29class ASPOSECPP_SHARED_CLASS Task : public IDisposable
30{
31 using ThisType = Task;
32
33public:
36 ASPOSECPP_SHARED_API Task(const Action<>& action);
40 ASPOSECPP_SHARED_API Task(const Action<>& action, const CancellationToken& cancellationToken);
44 ASPOSECPP_SHARED_API Task(const Action<SharedPtr<Object>>& action, const SharedPtr<Object>& state);
49 ASPOSECPP_SHARED_API Task(const Action<SharedPtr<Object>>& action, const SharedPtr<Object>& state,
50 const CancellationToken& cancellationToken);
52 ASPOSECPP_SHARED_API ~Task();
53
54 static ASPOSECPP_SHARED_API Nullable<int32_t> get_CurrentId();
57 static ASPOSECPP_SHARED_API const TaskPtr& get_CompletedTask();
60 ASPOSECPP_SHARED_API bool get_IsCompleted() const;
63 bool get_IsCanceled() const {return m_status == TaskStatus::Canceled;}
66 TaskStatus get_Status() const {return m_status;}
69 ASPOSECPP_SHARED_API AggregateException get_Exception() const;
70 int32_t get_Id() const {return m_id;}
73 const SharedPtr<Object>& get_AsyncState() const {return m_state;}
76 bool get_IsFaulted() const {return m_status == TaskStatus::Faulted;}
77
81 ASPOSECPP_SHARED_API TaskPtr ContinueWith(const Action<TaskPtr>& continuationAction);
86 template<typename TResult>
87 RTaskPtr<TResult> ContinueWith(const Func<TaskPtr, TResult>& continuationFunction);
88
95 ASPOSECPP_SHARED_API Runtime::CompilerServices::ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) const;
96
99 ASPOSECPP_SHARED_API void RunSynchronously();
103 ASPOSECPP_SHARED_API void RunSynchronously(const SharedPtr<TaskScheduler>& scheduler);
106 ASPOSECPP_SHARED_API void Start();
110 ASPOSECPP_SHARED_API void Start(const SharedPtr<TaskScheduler>& scheduler);
115 ASPOSECPP_SHARED_API void Wait(const CancellationToken& cancellationToken);
118 ASPOSECPP_SHARED_API void Wait();
119
121 ASPOSECPP_SHARED_API void Dispose() override;
122
124
126 using FunctionT = std::function<void()>;
127
129 ASPOSECPP_SHARED_API Task();
131 void set_Function(const FunctionT& fnc) {m_function = fnc;}
133 ASPOSECPP_SHARED_API void set_Status(TaskStatus status);
135 ASPOSECPP_SHARED_API const SharedPtr<TaskScheduler>& get_Scheduler() const {return m_scheduler;}
137 ASPOSECPP_SHARED_API void set_Scheduler(const SharedPtr<TaskScheduler>& scheduler) {m_scheduler = scheduler;}
138
140 ASPOSECPP_SHARED_API void Activate(const SharedPtr<TaskScheduler>& scheduler = nullptr);
142 ASPOSECPP_SHARED_API void Execute();
144 ASPOSECPP_SHARED_API void AddCompletionAction(const Action<>& continuation);
146 ASPOSECPP_SHARED_API void Complete();
148 ASPOSECPP_SHARED_API void Cancel();
149
150protected:
152 ASPOSECPP_SHARED_API void ContinueWithCore(const TaskPtr& continuationTask);
154 ASPOSECPP_SHARED_API void ExecuteContinuations();
155
157 ASPOSECPP_SHARED_API virtual void Finish();
158
159protected:
160 // Task ID
161 int32_t m_id;
167 std::exception_ptr m_exception;
175 std::list<Action<>> m_completions;
178
180 static std::atomic<int32_t> m_next_id;
181};
182
183}} // namespace Threading::Tasks
184
185namespace Runtime { namespace CompilerServices {
186
187class ASPOSECPP_SHARED_CLASS TaskAwaiter
188{
189public:
190 TaskAwaiter(const TaskPtr& task) : m_task(task) {}
191
192 ASPOSECPP_SHARED_API bool get_IsCompleted() const;
193 ASPOSECPP_SHARED_API void OnCompleted(const Action<>& continuation);
194 ASPOSECPP_SHARED_API void GetResult() const;
195
196 bool continueOnCapturedContext = false;
197
198private:
199 TaskPtr m_task;
200};
201
202class ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable
203{
204public:
205 ASPOSECPP_SHARED_CLASS ConfiguredTaskAwaitable(const TaskPtr& task, bool continueOnCapturedContext);
207
208private:
209 TaskPtr m_task;
210 bool m_continueOnCapturedContext;
211};
212
213}}} // namespace System::Runtime::CompilerServices
214
215#include <system/func.h>
216#include <system/threading/tasks/result_task.h>
217
218namespace System { namespace Threading { namespace Tasks {
219
220template <typename TResult>
222{
223 auto result = MakeObject<Threading::Tasks::ResultTask<TResult>>(
224 Func<TResult>([continuationFunction, task = MakeSharedPtr(this)] { return continuationFunction(task); }));
225 ContinueWithCore(result);
226 return result;
227}
228
229}}} // namespace System::Threading::Tasks
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
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:190
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:30
bool get_IsFaulted() const
Gets whether the task completed due to an unhandled exception.
Definition: task.h:76
static std::atomic< int32_t > m_next_id
ID of next task will be created.
Definition: task.h:180
void set_Function(const FunctionT &fnc)
Sets the internal function to execute.
Definition: task.h:131
bool get_IsCanceled() const
Gets whether the task completed due to cancellation.
Definition: task.h:63
void Cancel()
Marks the task as cancelled and finishes task.
std::exception_ptr m_exception
Captured exception if the task faulted.
Definition: task.h:167
AggregateException get_Exception() const
Gets the ID for task.
void Start()
Starts the task execution using the default scheduler.
int32_t get_Id() const
Definition: task.h:70
void Wait()
Waits for the task to complete.
SharedPtr< Object > m_state
User-defined state object.
Definition: task.h:173
void AddCompletionAction(const Action<> &continuation)
Adds a continuation action to be executed upon completion.
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 set_Scheduler(const SharedPtr< TaskScheduler > &scheduler)
Sets the scheduler associated with this task.
Definition: task.h:137
FunctionT m_function
The function to execute.
Definition: task.h:163
void RunSynchronously()
Runs the task synchronously on the current thread.
void Activate(const SharedPtr< TaskScheduler > &scheduler=nullptr)
Activates the task for execution on a scheduler.
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:66
SharedPtr< TaskScheduler > m_scheduler
Scheduler responsible for executing the task.
Definition: task.h:171
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.
int32_t m_id
Definition: task.h:161
bool get_IsCompleted() const
Gets whether the task has completed.
Task(const Action<> &action)
Constructs a Task with an action to execute.
void ExecuteContinuations()
Executes all registered continuations.
CancellationToken m_cancellation_token
Cancellation token for the task.
Definition: task.h:169
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.
std::list< Action<> > m_completions
List of continuation tasks to execute after completion.
Definition: task.h:175
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:73
std::mutex m_completions_mutex
Mutex protecting continuations operations.
Definition: task.h:177
void Start(const SharedPtr< TaskScheduler > &scheduler)
Starts the task execution using the specified scheduler.
TaskStatus m_status
Current execution status.
Definition: task.h:165
virtual void Finish()
Finalizes task completion and executes continuations.
std::function< void()> FunctionT
Internal implementation. Not for user code.
Definition: task.h:126
const SharedPtr< TaskScheduler > & get_Scheduler() const
Gets the scheduler associated with this task.
Definition: task.h:135
void RunSynchronously(const SharedPtr< TaskScheduler > &scheduler)
Runs the task synchronously using the specified scheduler.
void set_Status(TaskStatus status)
Sets the task status.
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
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650