Cpp-Taskflow  2.2.0
task.hpp
1 #pragma once
2 
3 #include "graph.hpp"
4 
5 namespace tf {
6 
18 class Task {
19 
20  friend class FlowBuilder;
21  friend class Taskflow;
22  friend class TaskView;
23 
24  public:
25 
29  Task() = default;
30 
34  Task(const Task& other);
35 
39  Task& operator = (const Task&);
40 
45 
49  const std::string& name() const;
50 
54  size_t num_successors() const;
55 
59  size_t num_dependents() const;
60 
68  Task& name(const std::string& name);
69 
79  template <typename C>
80  Task& work(C&& callable);
81 
91  template <typename... Ts>
92  Task& precede(Ts&&... tasks);
93 
101  Task& precede(std::vector<Task>& tasks);
102 
111 
121  template <typename... Ts>
122  Task& gather(Ts&&... tasks);
123 
131  Task& gather(std::vector<Task>& tasks);
132 
141 
147  Task& reset();
148 
152  bool empty() const;
153 
154  private:
155 
156  Task(Node&);
157  Task(Node*);
158 
159  Node* _node {nullptr};
160 
161  template <typename S>
162  void _gather(S&);
163 
164  template <typename S>
165  void _precede(S&);
166 };
167 
168 // Constructor
169 inline Task::Task(Node& node) : _node {&node} {
170 }
171 
172 // Constructor
173 inline Task::Task(Node* node) : _node {node} {
174 }
175 
176 // Constructor
177 inline Task::Task(const Task& rhs) : _node {rhs._node} {
178 }
179 
180 // Function: precede
181 template <typename... Ts>
182 Task& Task::precede(Ts&&... tgts) {
183  (_node->precede(*(tgts._node)), ...);
184  return *this;
185 }
186 
187 // Function: precede
189  _precede(tgts);
190  return *this;
191 }
192 
193 // Function: precede
195  _precede(tgts);
196  return *this;
197 }
198 
199 // Function: gather
200 template <typename... Bs>
201 Task& Task::gather(Bs&&... tgts) {
202  (tgts._node->precede(*_node), ...);
203  return *this;
204 }
205 
206 // Procedure: _gather
207 template <typename S>
208 void Task::_gather(S& tgts) {
209  for(auto& from : tgts) {
210  from._node->precede(*_node);
211  }
212 }
213 
214 // Procedure: _precede
215 template <typename S>
216 void Task::_precede(S& tgts) {
217  for(auto& to : tgts) {
218  _node->precede(*(to._node));
219  }
220 }
221 
222 // Function: gather
224  _gather(tgts);
225  return *this;
226 }
227 
228 // Function: gather
230  _gather(tgts);
231  return *this;
232 }
233 
234 // Operator =
235 inline Task& Task::operator = (const Task& rhs) {
236  _node = rhs._node;
237  return *this;
238 }
239 
240 // Operator =
242  _node = ptr;
243  return *this;
244 }
245 
246 // Function: work
247 template <typename C>
248 inline Task& Task::work(C&& c) {
249 
250  if(_node->_module) {
251  TF_THROW(Error::TASKFLOW, "can't assign work to a module task");
252  }
253 
254  _node->_work = std::forward<C>(c);
255 
256  return *this;
257 }
258 
259 // Function: name
260 inline Task& Task::name(const std::string& name) {
261  _node->_name = name;
262  return *this;
263 }
264 
265 // Procedure: reset
266 inline Task& Task::reset() {
267  _node = nullptr;
268  return *this;
269 }
270 
271 // Function: name
272 inline const std::string& Task::name() const {
273  return _node->_name;
274 }
275 
276 // Function: num_dependents
277 inline size_t Task::num_dependents() const {
278  return _node->num_dependents();
279 }
280 
281 // Function: num_successors
282 inline size_t Task::num_successors() const {
283  return _node->num_successors();
284 }
285 
286 // Function: empty
287 inline bool Task::empty() const {
288  return _node == nullptr;
289 }
290 
291 // ----------------------------------------------------------------------------
292 
300 class TaskView {
301 
302  friend class Executor;
303 
304  public:
305 
309  TaskView() = default;
310 
314  TaskView(const Task& task);
315 
319  TaskView(const TaskView& other);
320 
324  TaskView& operator = (const TaskView& other);
325 
329  TaskView& operator = (const Task& other);
330 
335 
339  const std::string& name() const;
340 
344  size_t num_successors() const;
345 
349  size_t num_dependents() const;
350 
354  void reset();
355 
359  bool empty() const;
360 
361  private:
362 
363  TaskView(Node&);
364  TaskView(Node*);
365 
366  Node* _node {nullptr};
367 };
368 
369 // Constructor
370 inline TaskView::TaskView(Node& node) : _node {&node} {
371 }
372 
373 // Constructor
374 inline TaskView::TaskView(Node* node) : _node {node} {
375 }
376 
377 // Constructor
378 inline TaskView::TaskView(const TaskView& rhs) : _node {rhs._node} {
379 }
380 
381 // Constructor
382 inline TaskView::TaskView(const Task& task) : _node {task._node} {
383 }
384 
385 // Operator =
387  _node = rhs._node;
388  return *this;
389 }
390 
391 // Operator =
392 inline TaskView& TaskView::operator = (const Task& rhs) {
393  _node = rhs._node;
394  return *this;
395 }
396 
397 // Operator =
399  _node = ptr;
400  return *this;
401 }
402 
403 // Function: name
404 inline const std::string& TaskView::name() const {
405  return _node->_name;
406 }
407 
408 // Function: num_dependents
409 inline size_t TaskView::num_dependents() const {
410  return _node->num_dependents();
411 }
412 
413 // Function: num_successors
414 inline size_t TaskView::num_successors() const {
415  return _node->num_successors();
416 }
417 
418 // Function: reset
419 inline void TaskView::reset() {
420  _node = nullptr;
421 }
422 
423 // Function: empty
424 inline bool TaskView::empty() const {
425  return _node == nullptr;
426 }
427 
428 } // end of namespace tf. ---------------------------------------------------
429 
size_t num_dependents() const
queries the number of predecessors of the task
Definition: task.hpp:277
Task & reset()
resets the task handle to point to nothing
Definition: task.hpp:266
TaskView()=default
constructs an empty task view
Task & gather(Ts &&... tasks)
adds precedence links from other tasks to this
Definition: task.hpp:201
size_t num_successors() const
queries the number of successors of the task
Definition: task.hpp:414
Definition: taskflow.hpp:5
bool empty() const
queries if the task view is empty
Definition: task.hpp:424
Task & operator=(const Task &)
replaces the contents with a copy of the other task
Definition: task.hpp:235
the class to create a task dependency graph
Definition: core/taskflow.hpp:15
const std::string & name() const
queries the name of the task
Definition: task.hpp:272
A constant wrapper class to a task node, mainly used in the tf::ExecutorObserver interface.
Definition: task.hpp:300
bool empty() const
queries if the task handle points to a task node
Definition: task.hpp:287
void reset()
resets to an empty view
Definition: task.hpp:419
Building blocks of a task dependency graph.
Definition: flow_builder.hpp:13
size_t num_successors() const
queries the number of successors of the task
Definition: task.hpp:282
Handle to modify and access a task.
Definition: task.hpp:18
Task & precede(Ts &&... tasks)
adds precedence links from this to other tasks
Definition: task.hpp:182
Task()=default
constructs an empty task
The executor class to run a taskflow graph.
Definition: executor.hpp:73
TaskView & operator=(const TaskView &other)
replaces the contents with a copy of the other task
Definition: task.hpp:386
size_t num_dependents() const
queries the number of predecessors of the task
Definition: task.hpp:409
Task & work(C &&callable)
assigns a new callable object to the task
Definition: task.hpp:248
const std::string & name() const
queries the name of the task
Definition: task.hpp:404