#pragma once #include "coro.h" #include "simple.h" #include "task.h" namespace NRainCheck { namespace NPrivate { void SpawnTaskImpl(TTaskRunnerBase* task); template TIntrusivePtr SpawnTaskWithRunner(TEnv* env, TParam param1, ISubtaskListener* subtaskListener) { static_assert((std::is_base_of::value), "expect (std::is_base_of::value)"); TIntrusivePtr task(new TRunner(env, subtaskListener, new TTask(env, param1))); NPrivate::SpawnTaskImpl(task.Get()); return task; } template void SpawnSubtaskWithRunner(TEnv* env, TSubtaskCompletion* completion) { static_assert((std::is_base_of::value), "expect (std::is_base_of::value)"); TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); completion->SetRunning(current); NPrivate::SpawnTaskImpl(new TRunner(env, completion, new TTask(env))); } template void SpawnSubtaskWithRunner(TEnv* env, TSubtaskCompletion* completion, TParam param) { static_assert((std::is_base_of::value), "expect (std::is_base_of::value)"); TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); completion->SetRunning(current); NPrivate::SpawnTaskImpl(new TRunner(env, completion, new TTask(env, param))); } } // Instantiate and start a task with given parameter. template TIntrusivePtr SpawnTask(TEnv* env, TParam param1, ISubtaskListener* subtaskListener = &TNopSubtaskListener::Instance) { return NPrivate::SpawnTaskWithRunner< TTask, typename TTask::ITask, typename TTask::TTaskRunner, TEnv, TParam>(env, param1, subtaskListener); } // Instantiate and start subtask of given task. template void SpawnSubtask(TEnv* env, TSubtaskCompletion* completion, TParam param) { return NPrivate::SpawnSubtaskWithRunner(env, completion, param); } }