Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a91882f

Browse files
committed
support context to pass args
1 parent 6855c64 commit a91882f

File tree

8 files changed

+175
-108
lines changed

8 files changed

+175
-108
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ target_link_libraries(
250250
fibonacci ${PROJECT_NAME} tf::default_settings
251251
)
252252

253+
add_executable(condition_dsl ${TF_EXAMPLE_DIR}/condition_dsl.cpp)
254+
target_link_libraries(
255+
condition_dsl ${PROJECT_NAME} tf::default_settings
256+
)
257+
253258
add_executable(condition ${TF_EXAMPLE_DIR}/condition.cpp)
254259
target_link_libraries(
255260
condition ${PROJECT_NAME} tf::default_settings

examples/condition_dsl.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// 2020/08/28 - Created by netcan: https://github.com/netcan
2+
// Task DSL demo
3+
// The example creates the following cyclic graph:
4+
//
5+
// A
6+
// |
7+
// v
8+
// B<---|
9+
// | |
10+
// v |
11+
// C----|
12+
// |
13+
// v
14+
// D
15+
//
16+
// - A is a task that initializes a counter to zero
17+
// - B is a task that increments the counter
18+
// - C is a condition task that loops around B until the counter
19+
// reaches a breaking number
20+
// - D is a task that wraps up the result
21+
#include <taskflow/taskflow.hpp>
22+
#include <taskflow/dsl/task_dsl.hpp>
23+
24+
int main() {
25+
tf::Executor executor;
26+
tf::Taskflow taskflow("Conditional Tasking Demo");
27+
28+
int counter; // owner
29+
30+
// use context to pass args
31+
// context must copyable
32+
struct Context {
33+
int &rcounter; // use counter(borrow)
34+
} context{counter};
35+
36+
def_taskc(A, Context, {
37+
std::cout << "initializes the counter to zero\n";
38+
rcounter = 0;
39+
});
40+
def_taskc(B, Context, {
41+
std::cout << "loops to increment the counter\n";
42+
rcounter++;
43+
});
44+
def_taskc(C, Context, {
45+
std::cout << "counter is " << rcounter << " -> ";
46+
if (rcounter != 5) {
47+
std::cout << "loops again (goes to B)\n";
48+
return 0;
49+
}
50+
std::cout << "breaks the loop (goes to D)\n";
51+
return 1;
52+
});
53+
def_taskc(D, Context, {
54+
std::cout << "done with counter equal to " << rcounter << '\n';
55+
});
56+
57+
taskbuild(
58+
task(A)
59+
-> task(B)
60+
-> task(C),
61+
task(C)
62+
-> fork(B, D)
63+
)(taskflow, context);
64+
65+
// visualizes the taskflow
66+
taskflow.dump(std::cout);
67+
68+
// executes the taskflow
69+
executor.run(taskflow).wait();
70+
71+
assert(counter == 5);
72+
73+
return 0;
74+
}

examples/simple_dsl.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@
22
// A simple example to capture the following task dependencies.
33
// using Task DSL to describe
44
// TaskA -> fork(TaskB, TaskC) -> TaskD
5-
6-
#include <taskflow/taskflow.hpp> // the only include you need
5+
#include <taskflow/taskflow.hpp> // the only include you need
76
#include <taskflow/dsl/task_dsl.hpp> // for support dsl
87

9-
int main(){
8+
int main() {
109
tf::Executor executor;
1110
tf::Taskflow taskflow("simple");
12-
def_task(A, { return []() { std::cout << "TaskA\n"; }; });
13-
def_task(B, { return []() { std::cout << "TaskB\n"; }; });
14-
def_task(C, { return []() { std::cout << "TaskC\n"; }; });
15-
def_task(D, { return []() { std::cout << "TaskD\n"; }; });
11+
def_task(A, { std::cout << "TaskA\n"; });
12+
def_task(B, { std::cout << "TaskB\n"; });
13+
def_task(C, { std::cout << "TaskC\n"; });
14+
def_task(D, { std::cout << "TaskD\n"; });
1615

17-
taskbuild( // +---+
18-
task(A) // +---->| B |-----+
19-
-> fork(B, C) // | +---+ |
20-
-> task(D) // +---+ +-v-+
21-
) {taskflow}; // | A | | D |
22-
// +---+ +-^-+
23-
// | +---+ |
24-
// +---->| C |-----+
25-
// +---+
16+
taskbuild( // +---+
17+
task(A) // +---->| B |-----+
18+
->fork(B, C) // | +---+ |
19+
->task(D) // +---+ +-v-+
20+
)(taskflow); // | A | | D |
21+
// +---+ +-^-+
22+
// | +---+ |
23+
// +---->| C |-----+
24+
// +---+
2625

2726
executor.run(taskflow).wait();
2827
return 0;
2928
}
30-

examples/visualization_dsl.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22
// This example demonstrates how to use 'dump' method to inspect
33
// a taskflow graph.
44
// use task dsl
5-
65
#include <taskflow/taskflow.hpp>
76
#include <taskflow/dsl/task_dsl.hpp> // for support dsl
87

9-
int main(){
8+
int main() {
109
tf::Taskflow tf("Visualization Demo");
1110

1211
// ------------------------------------------------------
1312
// Static Tasking
1413
// ------------------------------------------------------
15-
def_task(A, { return []() { std::cout << "TaskA\n"; }; });
16-
def_task(B, { return []() { std::cout << "TaskB\n"; }; });
17-
def_task(C, { return []() { std::cout << "TaskC\n"; }; });
18-
def_task(D, { return []() { std::cout << "TaskD\n"; }; });
19-
def_task(E, { return []() { std::cout << "TaskE\n"; }; });
14+
def_task(A, { std::cout << "TaskA\n"; };);
15+
def_task(B, { std::cout << "TaskB\n"; };);
16+
def_task(C, { std::cout << "TaskC\n"; };);
17+
def_task(D, { std::cout << "TaskD\n"; };);
18+
def_task(E, { std::cout << "TaskE\n"; };);
2019

2120
taskbuild(
2221
task(A)
2322
-> fork(B, C)
2423
-> task(D),
2524
merge(A, B)
2625
-> task(E)
27-
) {tf};
26+
)(tf);
2827

2928
// std::cout << "[dump without name assignment]\n";
3029
tf.dump(std::cout);
@@ -42,8 +41,5 @@ int main(){
4241
// Dynamic Tasking
4342
// ------------------------------------------------------
4443

45-
4644
return 0;
4745
}
48-
49-

taskflow/dsl/job_trait.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ namespace tf {
99
namespace dsl {
1010
struct JobSignature {};
1111

12-
template <typename J> struct JobCb {
13-
using type = JobCb<J>;
12+
template <typename J, typename CONTEXT> struct JobCb {
1413
using JobType = J;
15-
void build(FlowBuilder &build) { job_ = build.emplace(JobType{}()); }
14+
void build(FlowBuilder &build, const CONTEXT &context) {
15+
job_ = build.emplace(JobType{context}());
16+
}
17+
1618
Task job_;
1719
};
1820

taskflow/dsl/meta_macro.hpp

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,31 @@
11
// 2020/08/30 - Created by netcan: https://github.com/netcan
22
#pragma once
33
#ifdef _MSC_VER
4+
#define TF_EMPTY()
45
#define TF_GET_ARG_COUNT_(...) \
5-
PASTE(TF_GET_ARG_COUNT_I( \
6-
__VA_ARGS__, 299, 298, 297, 296, 295, 294, 293, 292, 291, 290, \
7-
289, 288, 287, 286, 285, 284, 283, 282, 281, 280, 279, 278, 277, \
8-
276, 275, 274, 273, 272, 271, 270, 269, 268, 267, 266, 265, 264, \
9-
263, 262, 261, 260, 259, 258, 257, 256, 255, 254, 253, 252, 251, \
10-
250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, \
11-
237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, \
12-
224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214, 213, 212, \
13-
211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200, 199, \
14-
198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, \
15-
185, 184, 183, 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, \
16-
172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 160, \
17-
159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, \
18-
146, 145, 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, \
19-
133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, \
20-
120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, \
21-
107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, \
22-
93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, \
23-
77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, \
24-
61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, \
25-
45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
26-
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, \
27-
13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ), \
28-
__CUB_empty())
6+
TF_PASTE(TF_GET_ARG_COUNT_I( \
7+
__VA_ARGS__, 120, 119, 118, \
8+
117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, \
9+
104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, \
10+
89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, \
11+
73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, \
12+
57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, \
13+
41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, \
14+
25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, \
15+
8, 7, 6, 5, 4, 3, 2, 1, 0, ), \
16+
TF_EMPTY())
2917

3018
#else
31-
3219
#define TF_GET_ARG_COUNT_(...) \
3320
TF_GET_ARG_COUNT_I( \
34-
__VA_ARGS__, 299, 298, 297, 296, 295, 294, 293, 292, 291, 290, 289, 288, \
35-
287, 286, 285, 284, 283, 282, 281, 280, 279, 278, 277, 276, 275, 274, \
36-
273, 272, 271, 270, 269, 268, 267, 266, 265, 264, 263, 262, 261, 260, \
37-
259, 258, 257, 256, 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, \
38-
245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, \
39-
231, 230, 229, 228, 227, 226, 225, 224, 223, 222, 221, 220, 219, 218, \
40-
217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, \
41-
203, 202, 201, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, \
42-
189, 188, 187, 186, 185, 184, 183, 182, 181, 180, 179, 178, 177, 176, \
43-
175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, \
44-
161, 160, 159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, \
45-
147, 146, 145, 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, \
46-
133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, \
47-
119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, \
48-
105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, \
49-
89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, \
50-
71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, \
51-
53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, \
52-
35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, \
53-
17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, )
21+
__VA_ARGS__, 120, 119, 118, 117, 116, \
22+
115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, \
23+
101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, \
24+
84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, \
25+
66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, \
26+
48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
27+
30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, \
28+
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, )
5429

5530
#endif
5631

@@ -64,24 +39,11 @@
6439
e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91, \
6540
e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, \
6641
e105, e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, \
67-
e117, e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128, \
68-
e129, e130, e131, e132, e133, e134, e135, e136, e137, e138, e139, e140, \
69-
e141, e142, e143, e144, e145, e146, e147, e148, e149, e150, e151, e152, \
70-
e153, e154, e155, e156, e157, e158, e159, e160, e161, e162, e163, e164, \
71-
e165, e166, e167, e168, e169, e170, e171, e172, e173, e174, e175, e176, \
72-
e177, e178, e179, e180, e181, e182, e183, e184, e185, e186, e187, e188, \
73-
e189, e190, e191, e192, e193, e194, e195, e196, e197, e198, e199, e200, \
74-
e201, e202, e203, e204, e205, e206, e207, e208, e209, e210, e211, e212, \
75-
e213, e214, e215, e216, e217, e218, e219, e220, e221, e222, e223, e224, \
76-
e225, e226, e227, e228, e229, e230, e231, e232, e233, e234, e235, e236, \
77-
e237, e238, e239, e240, e241, e242, e243, e244, e245, e246, e247, e248, \
78-
e249, e250, e251, e252, e253, e254, e255, e256, e257, e258, e259, e260, \
79-
e261, e262, e263, e264, e265, e266, e267, e268, e269, e270, e271, e272, \
80-
e273, e274, e275, e276, e277, e278, e279, e280, e281, e282, e283, e284, \
81-
e285, e286, e287, e288, e289, e290, e291, e292, e293, e294, e295, e296, \
82-
e297, e298, e299, size, ...) \
42+
e117, e118, e119, e120, size, \
43+
...) \
8344
size
8445

46+
#define TF_REPEAT_0(func, i, arg)
8547
#define TF_REPEAT_1(func, i, arg) func(i, arg)
8648
#define TF_REPEAT_2(func, i, arg, ...) \
8749
func(i, arg) TF_REPEAT_1(func, i + 1, __VA_ARGS__)
@@ -210,11 +172,5 @@
210172
#define TF_REPEAT_64(func, i, arg, ...) \
211173
func(i, arg) TF_REPEAT_63(func, i + 1, __VA_ARGS__)
212174

213-
#define TF_STR(x) #x
214175
#define TF_CONCATE(x, y) x##y
215-
#define TF_STRING(x) TF_STR(x)
216-
#define TF_PARE(...) __VA_ARGS__
217-
#define TF_EAT(...)
218-
#define TF_PAIR(x) TF_PARE x // PAIR((int) x) => PARE(int) x => int x
219-
#define TF_STRIP(x) TF_EAT x // STRIP((int) x) => EAT(int) x => x
220176
#define TF_PASTE(x, y) TF_CONCATE(x, y)

taskflow/dsl/task_dsl.hpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
namespace tf {
99
namespace dsl {
10-
template <typename = void, typename... Chains> class TaskDsl {
10+
struct EmptyContext {};
11+
template <typename CONTEXT = EmptyContext, typename... Chains> class TaskDsl {
1112
using Links = Unique_t<Flatten_t<TypeList<typename Chain<Chains>::type...>>>;
1213
using Analyzer = typename Links::template exportTo<TaskAnalyzer>;
1314

1415
using AllJobs = typename Analyzer::AllJobs;
15-
using JobsCb = typename Map_t<AllJobs, JobCb>::template exportTo<std::tuple>;
16+
17+
template <typename J> struct JobCbWithContext {
18+
using type = JobCb<J, CONTEXT>;
19+
};
20+
using JobsCb =
21+
typename Map_t<AllJobs, JobCbWithContext>::template exportTo<std::tuple>;
1622

1723
using OneToOneLinkSet = typename Analyzer::OneToOneLinkSet;
1824
template <typename OneToOneLink> struct OneToOneLinkInstanceType {
@@ -23,15 +29,17 @@ template <typename = void, typename... Chains> class TaskDsl {
2329
OneToOneLinkInstanceType>::template exportTo<std::tuple>;
2430

2531
public:
26-
constexpr TaskDsl(FlowBuilder &flow_builder) {
27-
build_jobs_cb(flow_builder, std::make_index_sequence<AllJobs::size>{});
32+
constexpr TaskDsl(FlowBuilder &flow_builder, const CONTEXT &context = {}) {
33+
build_jobs_cb(flow_builder, context,
34+
std::make_index_sequence<AllJobs::size>{});
2835
build_links(std::make_index_sequence<OneToOneLinkSet::size>{});
2936
}
3037

3138
private:
3239
template <size_t... Is>
33-
void build_jobs_cb(FlowBuilder &flow_builder, std::index_sequence<Is...>) {
34-
auto _ = {0, (std::get<Is>(jobsCb_).build(flow_builder), 0)...};
40+
void build_jobs_cb(FlowBuilder &flow_builder, const CONTEXT &context,
41+
std::index_sequence<Is...>) {
42+
auto _ = {0, (std::get<Is>(jobsCb_).build(flow_builder, context), 0)...};
3543
(void)_;
3644
}
3745

@@ -44,6 +52,13 @@ template <typename = void, typename... Chains> class TaskDsl {
4452
JobsCb jobsCb_;
4553
OneToOneLinkInstances links_;
4654
};
55+
56+
template <typename = void, typename... Chains, typename CONTEXT = EmptyContext>
57+
constexpr TaskDsl<CONTEXT, Chains...> taskDsl(FlowBuilder &flow_builder,
58+
CONTEXT &&context = {}) {
59+
return {flow_builder, context};
60+
}
61+
4762
} // namespace dsl
4863
} // namespace tf
4964

@@ -53,9 +68,18 @@ template <typename = void, typename... Chains> class TaskDsl {
5368
///////////////////////////////////////////////////////////////////////////////
5469
// def_task(TASK_NAME, { return a action lambda })
5570
#define def_task(name, ...) \
56-
struct name : tf::dsl::JobSignature { \
57-
auto operator()() __VA_ARGS__ \
58-
}
71+
struct name : tf::dsl::JobSignature, tf::dsl::EmptyContext { \
72+
name(const EmptyContext &context) : EmptyContext(context) {} \
73+
auto operator()() { return [] __VA_ARGS__; } \
74+
};
75+
#define def_taskc(name, Context, ...) \
76+
struct name : tf::dsl::JobSignature, Context { \
77+
name(const Context &context) : Context(context) {} \
78+
auto operator()() { \
79+
/* copy *this(copy CONTEXT to lambda) */ \
80+
return [*this] __VA_ARGS__; \
81+
} \
82+
};
5983

6084
// some_task(A, B, C) means SomeJob
6185
#define some_task(...) auto (*)(tf::dsl::SomeJob<__VA_ARGS__>)
@@ -67,5 +91,5 @@ template <typename = void, typename... Chains> class TaskDsl {
6791
#define task(Job) auto (*)(Job)
6892
// taskbuild(...) build a task dsl graph
6993
#define taskbuild(...) \
70-
tf::dsl::TaskDsl<void TF_PASTE(TF_REPEAT_, TF_GET_ARG_COUNT(__VA_ARGS__))( \
94+
tf::dsl::taskDsl<void TF_PASTE(TF_REPEAT_, TF_GET_ARG_COUNT(__VA_ARGS__))( \
7195
chain, 0, __VA_ARGS__)>

0 commit comments

Comments
 (0)