spawn.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include "coro.h"
  3. #include "simple.h"
  4. #include "task.h"
  5. namespace NRainCheck {
  6. namespace NPrivate {
  7. void SpawnTaskImpl(TTaskRunnerBase* task);
  8. template <typename TTask, typename ITask, typename TRunner, typename TEnv, typename TParam>
  9. TIntrusivePtr<TRunner> SpawnTaskWithRunner(TEnv* env, TParam param1, ISubtaskListener* subtaskListener) {
  10. static_assert((std::is_base_of<ITask, TTask>::value), "expect (std::is_base_of<ITask, TTask>::value)");
  11. TIntrusivePtr<TRunner> task(new TRunner(env, subtaskListener, new TTask(env, param1)));
  12. NPrivate::SpawnTaskImpl(task.Get());
  13. return task;
  14. }
  15. template <typename TTask, typename ITask, typename TRunner, typename TEnv>
  16. void SpawnSubtaskWithRunner(TEnv* env, TSubtaskCompletion* completion) {
  17. static_assert((std::is_base_of<ITask, TTask>::value), "expect (std::is_base_of<ITask, TTask>::value)");
  18. TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
  19. completion->SetRunning(current);
  20. NPrivate::SpawnTaskImpl(new TRunner(env, completion, new TTask(env)));
  21. }
  22. template <typename TTask, typename ITask, typename TRunner, typename TEnv, typename TParam>
  23. void SpawnSubtaskWithRunner(TEnv* env, TSubtaskCompletion* completion, TParam param) {
  24. static_assert((std::is_base_of<ITask, TTask>::value), "expect (std::is_base_of<ITask, TTask>::value)");
  25. TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
  26. completion->SetRunning(current);
  27. NPrivate::SpawnTaskImpl(new TRunner(env, completion, new TTask(env, param)));
  28. }
  29. }
  30. // Instantiate and start a task with given parameter.
  31. template <typename TTask, typename TEnv, typename TParam>
  32. TIntrusivePtr<typename TTask::TTaskRunner> SpawnTask(TEnv* env, TParam param1, ISubtaskListener* subtaskListener = &TNopSubtaskListener::Instance) {
  33. return NPrivate::SpawnTaskWithRunner<
  34. TTask, typename TTask::ITask, typename TTask::TTaskRunner, TEnv, TParam>(env, param1, subtaskListener);
  35. }
  36. // Instantiate and start subtask of given task.
  37. template <typename TTask, typename TEnv, typename TParam>
  38. void SpawnSubtask(TEnv* env, TSubtaskCompletion* completion, TParam param) {
  39. return NPrivate::SpawnSubtaskWithRunner<TTask, typename TTask::ITask, typename TTask::TTaskRunner>(env, completion, param);
  40. }
  41. }