libtasks Documentation  1.6
task.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _TASKS_TASK_H_
9 #define _TASKS_TASK_H_
10 
11 #include <functional>
12 #include <vector>
13 
14 namespace tasks {
15 
16 class worker;
17 
19 class task {
20  public:
21  typedef std::function<void(worker* worker)> finish_func_worker_t;
22  typedef std::function<void()> finish_func_void_t;
23  struct finish_func_t {
26 
28  switch (m_type) {
29  case 0:
30  m_f_worker(worker);
31  break;
32  case 1:
33  m_f_void();
34  break;
35  }
36  }
37 
38  int m_type = 0;
41  };
42 
43  virtual ~task() {}
44 
46  inline bool auto_delete() const { return m_auto_delete; }
47 
49  inline void disable_auto_delete() { m_auto_delete = false; }
50 
52  void finish(worker* worker = nullptr);
53 
57  m_finish_funcs.push_back(finish_func_t(f));
58  }
59 
62  inline void on_finish(finish_func_void_t f) {
63  m_finish_funcs.push_back(finish_func_t(f));
64  }
65 
66  private:
67  // Default behavior is to delete a task when handle_event returns false. Change this by calling
68  // disable_auto_delete().
69  bool m_auto_delete = true;
70 
71  std::vector<finish_func_t> m_finish_funcs;
72 };
73 
74 } // tasks
75 
76 #endif // _TASKS_TASK_H_
std::function< void()> finish_func_void_t
Definition: task.h:22
bool m_auto_delete
Definition: task.h:69
The base class for any task.
Definition: task.h:19
bool auto_delete() const
Returns true if auto deletion is active.
Definition: task.h:46
void finish(worker *worker=nullptr)
Called by a worker when a task has auto_deletion enabled.
Definition: task.cpp:13
finish_func_worker_t m_f_worker
Definition: task.h:39
void on_finish(finish_func_worker_t f)
Definition: task.h:56
finish_func_void_t m_f_void
Definition: task.h:40
void disable_auto_delete()
Call this to deactivate auto deletion.
Definition: task.h:49
void on_finish(finish_func_void_t f)
Definition: task.h:62
finish_func_t(finish_func_worker_t f)
Definition: task.h:24
std::vector< finish_func_t > m_finish_funcs
Definition: task.h:71
finish_func_t(finish_func_void_t f)
Definition: task.h:25
void operator()(worker *worker)
Definition: task.h:27
std::function< void(worker *worker)> finish_func_worker_t
Definition: task.h:21
virtual ~task()
Definition: task.h:43