joque
task orchestration library
task.hpp
Go to the documentation of this file.
1 #pragma once
23 
24 #include "job.hpp"
25 
26 #include <functional>
27 #include <map>
28 #include <memory>
29 #include <span>
30 #include <string>
31 #include <type_traits>
32 #include <vector>
33 
34 namespace joque
35 {
36 
39 struct resource
40 {
41  std::string name;
42 };
43 
44 template < typename T >
45 using ref_vec = std::vector< std::reference_wrapper< const T > >;
46 
48 struct task
49 {
52 
57 
61 
64 
68 
71  bool hidden = false;
72 };
73 
80 struct task_set
81 {
83  std::map< std::string, task > tasks;
85  std::map< std::string, task_set > sets;
86 };
87 
91 template < typename T, typename Fun >
92 requires( std::same_as< std::remove_cvref_t< T >, task_set > )
93 void for_each_task( T& ts, Fun&& f );
94 
97 void for_each_add_dep( task_set& ts, const task& dep );
98 
101 void add_dep_to_each( task& t, const task_set& ts );
102 
105 void run_each_after( task_set& ts, const task& t );
106 
109 void run_after_all_of( task& t, const task_set& ts );
110 
111 void invalidated_by_all_of( task& t, const task_set& ts );
112 
113 template < typename T, typename Fun >
114 void for_each_task_impl( T& ts, Fun&& f, const std::string& prefix )
115 {
116  for ( auto& [name, t] : ts.tasks )
117  f( ( prefix + "/" ).append( name ), t );
118  for ( auto& [name, s] : ts.sets )
119  for_each_task_impl( s, f, ( prefix + "/" ).append( name ) );
120 }
121 
122 template < typename T, typename Fun >
123 requires( std::same_as< std::remove_cvref_t< T >, task_set > )
124 void for_each_task( T& ts, Fun&& f )
125 {
126  for_each_task_impl( ts, f, "/" );
127 }
128 
129 } // namespace joque
MIT License.
Definition: dag.hpp:27
std::string name
Definition: task.hpp:41
std::vector< std::reference_wrapper< const T > > ref_vec
Definition: task.hpp:45
ref_vec< resource > resources
Resources used by this task, only one task can access any resource at single point in time.
Definition: task.hpp:67
ref_vec< task > run_after
Tasks that should be executed before this task.
Definition: task.hpp:60
void add_dep_to_each(task &t, const task_set &ts)
Recursively adds dependency on each task in ts to task t, except for t itself.
bool hidden
In case this is set to true, this task should not be visible in standard reports.
Definition: task.hpp:71
void invalidated_by_all_of(task &t, const task_set &ts)
std::map< std::string, task > tasks
Tasks of this set.
Definition: task.hpp:83
void for_each_add_dep(task_set &ts, const task &dep)
Recursively adds dependency on dep for each task in ts, except for dep itself.
void run_after_all_of(task &t, const task_set &ts)
Recursively adds run after relationship so that task t is run after all tasks in set ts.
std::map< std::string, task_set > sets
Subsets of this set.
Definition: task.hpp:85
job_ptr job
Job being executed for the task.
Definition: task.hpp:51
ref_vec< task > invalidated_by
Tasks which invalidation also invalidates this task.
Definition: task.hpp:63
Fun && f
Definition: task.hpp:93
void run_each_after(task_set &ts, const task &t)
Recursively adds run after relationship so that all tasks in ts are run after t.
ref_vec< task > depends_on
Dependencies of the task - all of these should be executed before this task.
Definition: task.hpp:56
void for_each_task_impl(T &ts, Fun &&f, const std::string &prefix)
Definition: task.hpp:114
requires(std::same_as< std::remove_cvref_t< T >, task_set >) void for_each_task(T &ts
Recursively executes function f for each task in set ts.
Abstraction to model resource used by tasks.
Definition: task.hpp:40
Single task that should be executed by the system.
Definition: task.hpp:49
A set of tasks that contains either tasks or another sets.
Definition: task.hpp:81
Custom unique_ptr wrapper that simplifies syntax of tasks.
Definition: job.hpp:73