FuncTask

Files CCore/inc/task/FuncTask.h CCore/src/task/FuncTask.cpp

RunFuncTask runs a task, which calls the given functor.


template <class FuncInit,class ... TT>
void RunFuncTask(const FuncInit &func_init,TT ... tt);

template <class FuncInit,class ... TT>
void RunFuncTask(const FuncInit &func_init,Function<void (void)> exit_function,TT ... tt);

The first argument is a Functor Init object. The functor itself is created by the spawned task and then it is called.

The second argument may be an exit function. It is called as a part of the task exit processing. This function is always called, even if the task run failed.

If the task run is failed, then an exception is thrown.

Remaining arguments are used to create a Task object.

To run a task the following class, derived from the class Task, is used.


template <class FuncInit>
class FuncTask : public Task
 {
   FuncInit func_init;
   Function<void (void)> exit_function;
   
  private: 
  
   virtual void entry()
    {
     FunctorTypeOf<FuncInit> func(func_init);
    
     func();
    }
   
   virtual void exit()
    {
     auto func=exit_function;
     
     delete this;
     
     func();
    }
   
  public: 
  
   template <class ... TT>
   explicit FuncTask(const FuncInit &func_init_,TT ... tt)
    : Task(tt...),
      func_init(func_init_) 
    {
    }
   
   template <class ... TT>
   FuncTask(const FuncInit &func_init_,Function<void (void)> exit_function_,TT ... tt) 
    : Task(tt...),
      func_init(func_init_),
      exit_function(exit_function_) 
    {
    }
   
   virtual ~FuncTask() {}
 };