Helios++
Helios software for LiDAR simulations
TaskDropper.h
1 #pragma once
2 
3 #include <util/HeliosException.h>
4 #include <util/threadpool/ThreadPool.h>
5 
6 #include <vector>
7 #include <memory>
8 
9 using std::shared_ptr;
10 
21 template <typename TaskType, typename ThreadPoolType, typename ... TaskArgs>
22 class TaskDropper {
23 protected:
24  // *** ATTRIBUTES *** //
25  // ******************** //
31  std::vector<shared_ptr<TaskType>> tasks;
39  size_t maxTasks;
40 
41 public:
42  // *** CONSTRUCTION / DESTRUCTION *** //
43  // ************************************ //
49  TaskDropper(size_t const maxTasks=32) : maxTasks(maxTasks) {}
50  virtual ~TaskDropper() = default;
51 
52 
53  // *** TASK DROPPER METHODS *** //
54  // ***************************** //
60  virtual inline bool add(shared_ptr<TaskType> task){
61  tasks.push_back(task);
62  if(tasks.size() == maxTasks){
63  drop();
64  return true;
65  }
66  return false;
67  }
75  virtual inline bool add(shared_ptr<TaskType> task, TaskArgs ... args){
76  tasks.push_back(task);
77  if(tasks.size() == maxTasks){
78  drop(args...);
79  return true;
80  }
81  return false;
82  }
93  virtual inline bool add(ThreadPoolType &pool, shared_ptr<TaskType> task){
94  tasks.push_back(task);
95  if(tasks.size() == maxTasks){
96  drop(pool);
97  return true;
98  }
99  return false;
100  }
101 
111  virtual inline char tryAdd(ThreadPoolType &pool, shared_ptr<TaskType> task){
112  tasks.push_back(task);
113  if(tasks.size() == maxTasks){
114  if(tryDrop(pool)) return 1;
115  return 2;
116  }
117  return 0;
118  }
119 
127  virtual inline void drop(){
128  for(shared_ptr<TaskType> task : tasks) doTask(*task);
129  tasks.clear();
130  }
139  virtual inline void drop(TaskArgs ... args){
140  for(shared_ptr<TaskType> task : tasks) doTask(*task, args...);
141  tasks.clear();
142  }
156  virtual inline void drop(ThreadPoolType &pool){
157  throw HeliosException(
158  "TaskDropper::drop(ThreadPoolType &) cannot be used.\n"
159  "It must be overridden by any derived class providing parallel "
160  "execution through given thread pool."
161  );
162  };
163 
171  virtual inline bool tryDrop(ThreadPoolType &pool){
172  throw HeliosException(
173  "TaskDropper::tryDrop(ThreadPoolType &) cannot be used.\n"
174  "It must be overridden by any derived class providing parallel "
175  "non-blocking execution through given thread pool."
176  );
177  }
178 
184  inline TaskDropper emptyClone() const {
185  return TaskDropper(maxTasks);
186  }
187 
188 protected:
189  // *** INTERNAL TASK DROPPER METHODS *** //
190  // *************************************** //
199  virtual inline void doTask(TaskType &task)
200  {task();}
207  virtual inline void doTask(TaskType &task, TaskArgs ... args)
208  {task(args...);}
209 
210 public:
211  // *** FUNCTOR OPERATORS *** //
212  // *************************** //
218  virtual void operator()(){drop();}
226  virtual void operator()(TaskArgs ... args){drop(args...);}
227 
228  // *** GETTERs and SETTERs *** //
229  // ***************************** //
235  virtual inline size_t getMaxTasks() const
236  {return maxTasks;}
242  virtual inline void setMaxTasks(size_t const maxTasks)
243  {this->maxTasks = maxTasks;}
248  virtual inline shared_ptr<TaskType> popTask(){
249  shared_ptr<TaskType> task = tasks.back();
250  tasks.pop_back();
251  return task;
252  }
253 
254 };
Base class for Helios exceptions.
Definition: HeliosException.h:12
Class which handles tasks dropping. It is, executing and then removing each task when dropping.
Definition: TaskDropper.h:22
virtual bool tryDrop(ThreadPoolType &pool)
Like drop(ThreadPoolType &) but supporting non blocking calls.
Definition: TaskDropper.h:171
virtual void drop(TaskArgs ... args)
Drop all tasks with arguments, one after another.
Definition: TaskDropper.h:139
std::vector< shared_ptr< TaskType > > tasks
Tasks to be dropped as a whole.
Definition: TaskDropper.h:31
virtual void doTask(TaskType &task)
Execute a task with no arguments.
Definition: TaskDropper.h:199
virtual void doTask(TaskType &task, TaskArgs ... args)
Execute a task with corresponding arguments.
Definition: TaskDropper.h:207
virtual bool add(shared_ptr< TaskType > task, TaskArgs ... args)
Like TaskDropper::add but for tasks with arguments.
Definition: TaskDropper.h:75
virtual char tryAdd(ThreadPoolType &pool, shared_ptr< TaskType > task)
Like TaskDropper::add(ThreadPoolType &, shared_ptr<TaskType>) but supporting non-blocking calls.
Definition: TaskDropper.h:111
virtual void drop(ThreadPoolType &pool)
A callback for TaskDropper::drop but using a thread pool to be executed in parallel.
Definition: TaskDropper.h:156
virtual bool add(ThreadPoolType &pool, shared_ptr< TaskType > task)
Like TaskDropper::add but running the drop method in parallel through a callback from given thread po...
Definition: TaskDropper.h:93
virtual size_t getMaxTasks() const
Get current maximum tasks limit.
Definition: TaskDropper.h:235
virtual void drop()
Drop all tasks, one after another.
Definition: TaskDropper.h:127
virtual void operator()(TaskArgs ... args)
Functor operator with arguments calling drop method with arguments which can be used by thread pools.
Definition: TaskDropper.h:226
virtual bool add(shared_ptr< TaskType > task)
Add a task to the TaskDropper.
Definition: TaskDropper.h:60
virtual void setMaxTasks(size_t const maxTasks)
Set new maximum tasks limit.
Definition: TaskDropper.h:242
virtual shared_ptr< TaskType > popTask()
Pop a task from vector of tasks.
Definition: TaskDropper.h:248
TaskDropper(size_t const maxTasks=32)
Default constructor for TaskDropper.
Definition: TaskDropper.h:49
size_t maxTasks
Specify the maximum number of tasks before forcing a drop.
Definition: TaskDropper.h:39
virtual void operator()()
Void functor operator calling drop method to be compatible with thread pools.
Definition: TaskDropper.h:218
TaskDropper emptyClone() const
Do an empty clone of this task dropper. It is, an exact clone but with an empty tasks vector.
Definition: TaskDropper.h:184