Cpp-Taskflow  2.0.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 
22  template <template<typename...> typename E>
23  friend class BasicTaskflow;
24 
25  public:
26 
30  Task() = default;
31 
35  Task(const Task& other);
36 
42  Task(Task&& other);
43 
47  Task& operator = (const Task&);
48 
52  const std::string& name() const;
53 
57  size_t num_successors() const;
58 
62  size_t num_dependents() const;
63 
71  Task& name(const std::string& name);
72 
82  template <typename C>
83  Task& work(C&& callable);
84 
94  template <typename... Ts>
95  Task& precede(Ts&&... tasks);
96 
104  Task& precede(std::vector<Task>& tasks);
105 
114 
124  template <typename... Ts>
125  Task& gather(Ts&&... tasks);
126 
134  Task& gather(std::vector<Task>& tasks);
135 
144 
145 
146  //template <typename... Bs>
147  //Task& broadcast(Bs&&...);
148  //
149  //Task& broadcast(std::vector<Task>&);
150  //Task& broadcast(std::initializer_list<Task>);
151 
152  private:
153 
154  Task(Node&);
155 
156  Node* _node {nullptr};
157 
158  //template <typename S>
159  //void _broadcast(S&);
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& t) : _node {&t} {
170 }
171 
172 // Constructor
173 inline Task::Task(const Task& rhs) : _node {rhs._node} {
174 }
175 
177 //template <typename... Bs>
178 //Task& Task::broadcast(Bs&&... tgts) {
179 // (_node->precede(*(tgts._node)), ...);
180 // return *this;
181 //}
182 //
184 //template <typename S>
185 //inline void Task::_broadcast(S& tgts) {
186 // for(auto& to : tgts) {
187 // _node->precede(*(to._node));
188 // }
189 //}
190 //
192 //inline Task& Task::broadcast(std::vector<Task>& tgts) {
193 // _broadcast(tgts);
194 // return *this;
195 //}
196 //
198 //inline Task& Task::broadcast(std::initializer_list<Task> tgts) {
199 // _broadcast(tgts);
200 // return *this;
201 //}
202 
203 // Function: precede
204 template <typename... Ts>
205 Task& Task::precede(Ts&&... tgts) {
206  (_node->precede(*(tgts._node)), ...);
207  return *this;
208 }
209 
210 // Function: precede
212  _precede(tgts);
213  return *this;
214 }
215 
216 // Function: precede
218  _precede(tgts);
219  return *this;
220 }
221 
222 // Function: gather
223 template <typename... Bs>
224 Task& Task::gather(Bs&&... tgts) {
225  (tgts._node->precede(*_node), ...);
226  return *this;
227 }
228 
229 // Procedure: _gather
230 template <typename S>
231 void Task::_gather(S& tgts) {
232  for(auto& from : tgts) {
233  from._node->precede(*_node);
234  }
235 }
236 
237 // Procedure: _precede
238 template <typename S>
239 void Task::_precede(S& tgts) {
240  for(auto& to : tgts) {
241  _node->precede(*(to._node));
242  }
243 }
244 
245 // Function: gather
247  _gather(tgts);
248  return *this;
249 }
250 
251 // Function: gather
253  _gather(tgts);
254  return *this;
255 }
256 
257 // Operator =
258 inline Task& Task::operator = (const Task& rhs) {
259  _node = rhs._node;
260  return *this;
261 }
262 
263 // Constructor
264 inline Task::Task(Task&& rhs) : _node{rhs._node} {
265  rhs._node = nullptr;
266 }
267 
268 // Function: work
269 template <typename C>
270 inline Task& Task::work(C&& c) {
271  _node->_work = std::forward<C>(c);
272  return *this;
273 }
274 
275 // Function: name
276 inline Task& Task::name(const std::string& name) {
277  _node->_name = name;
278  return *this;
279 }
280 
281 // Function: name
282 inline const std::string& Task::name() const {
283  return _node->_name;
284 }
285 
286 // Function: num_dependents
287 inline size_t Task::num_dependents() const {
288  return _node->_dependents.load(std::memory_order_relaxed);
289 }
290 
291 // Function: num_successors
292 inline size_t Task::num_successors() const {
293  return _node->_successors.size();
294 }
295 
296 }; // end of namespace tf. ---------------------------------------------------
size_t num_dependents() const
queries the number of predecessors of the task
Definition: task.hpp:287
The base class to derive a taskflow class.
Definition: basic_taskflow.hpp:20
Task & gather(Ts &&... tasks)
adds precedence links from other tasks to this
Definition: task.hpp:224
Definition: taskflow.hpp:6
Task & operator=(const Task &)
replaces the contents with a copy of the other task
Definition: task.hpp:258
const std::string & name() const
queries the name of the task
Definition: task.hpp:282
The building blocks of task dependency graphs.
Definition: flow_builder.hpp:13
size_t num_successors() const
queries the number of successors of the task
Definition: task.hpp:292
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:205
Task()=default
constructs an empty task
Task & work(C &&callable)
assigns a new callable object to the task
Definition: task.hpp:270