Skip to content

Commit 9326462

Browse files
committed
updated core
1 parent a21a91e commit 9326462

6 files changed

Lines changed: 132 additions & 75 deletions

File tree

examples/run.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main(){
1010
tf::Taskflow taskflow("Demo");
1111

1212
auto A = taskflow.emplace([&](){ std::cout << "TaskA\n"; }).name("A");
13-
auto B = taskflow.emplace([&](auto& subflow){
13+
auto B = taskflow.emplace([&](tf::Subflow& subflow){
1414
std::cout << "TaskB\n";
1515
auto B1 = subflow.emplace([&](){ std::cout << "TaskB1\n"; }).name("B1");
1616
auto B2 = subflow.emplace([&](){ std::cout << "TaskB2\n"; }).name("B2");

examples/subflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, char* argv[]) {
4141
// Task A
4242
[] () { std::cout << "TaskA\n"; },
4343
// Task B
44-
[cap=std::vector<int>{1,2,3,4,5,6,7,8}, detached] (auto& subflow) {
44+
[cap=std::vector<int>{1,2,3,4,5,6,7,8}, detached] (tf::Subflow& subflow) {
4545
std::cout << "TaskB is spawning B1, B2, and B3 ...\n";
4646

4747
auto B1 = subflow.emplace([&]() {

taskflow/core/flow_builder.hpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -365,35 +365,30 @@ auto FlowBuilder::emplace(C&&... cs) {
365365
// Function: emplace
366366
template <typename C>
367367
Task FlowBuilder::emplace(C&& c) {
368-
369-
// dynamic tasking
370-
if constexpr(std::is_invocable_v<C, Subflow&>) {
371-
auto n = _graph.emplace_back(std::in_place_type_t<Node::DynamicWork>{},
372-
[c=std::forward<C>(c)] (Subflow& fb) mutable {
373-
// first time execution
374-
if(fb._graph.empty()) {
375-
c(fb);
376-
}
377-
});
378-
return Task(n);
379-
}
380-
// condition tasking
381-
else if constexpr(std::is_same_v<typename function_traits<C>::return_type, int>) {
368+
369+
// static task
370+
if constexpr(is_static_task_v<C>) {
382371
auto n = _graph.emplace_back(
383-
std::in_place_type_t<Node::ConditionWork>{}, std::forward<C>(c)
372+
std::in_place_type_t<Node::StaticWork>{}, std::forward<C>(c)
384373
);
385374
return Task(n);
386375
}
387-
// static tasking
388-
else if constexpr(std::is_same_v<typename function_traits<C>::return_type, void>) {
376+
// condition task
377+
else if constexpr(is_condition_task_v<C>) {
389378
auto n = _graph.emplace_back(
390-
std::in_place_type_t<Node::StaticWork>{}, std::forward<C>(c)
379+
std::in_place_type_t<Node::ConditionWork>{}, std::forward<C>(c)
391380
);
392381
return Task(n);
393382
}
394-
// placeholder
395-
else if constexpr(std::is_same_v<C, std::monostate>) {
396-
auto n = _graph.emplace_back();
383+
// dynamic task
384+
else if constexpr(is_dynamic_task_v<C>) {
385+
auto n = _graph.emplace_back(std::in_place_type_t<Node::DynamicWork>{},
386+
[c=std::forward<C>(c)] (Subflow& fb) mutable {
387+
// first time execution
388+
if(fb._graph.empty()) {
389+
c(fb);
390+
}
391+
});
397392
return Task(n);
398393
}
399394
else {
@@ -975,15 +970,15 @@ Task& Task::work(C&& c) {
975970
}
976971

977972
// static tasking
978-
if constexpr(std::is_same_v<typename function_traits<C>::return_type, void>) {
973+
if constexpr(is_static_task_v<C>) {
979974
_node->_work.emplace<Node::StaticWork>(std::forward<C>(c));
980975
}
981976
// condition tasking
982-
else if constexpr(std::is_same_v<typename function_traits<C>::return_type, int>) {
977+
else if constexpr(is_condition_task_v<C>) {
983978
_node->_work.emplace<Node::ConditionWork>(std::forward<C>(c));
984979
}
985980
// dyanmic tasking
986-
else if constexpr(std::is_invocable_v<C, Subflow&>) {
981+
else if constexpr(is_dynamic_task_v<C>) {
987982
_node->_work.emplace<Node::DynamicWork>(
988983
[c=std::forward<C>(c)] (Subflow& fb) mutable {
989984
// first time execution
@@ -992,10 +987,6 @@ Task& Task::work(C&& c) {
992987
}
993988
});
994989
}
995-
// placeholder
996-
else if constexpr(std::is_same_v<C, std::monostate>) {
997-
_node->_work.emplace<std::monostate>();
998-
}
999990
else {
1000991
static_assert(dependent_false_v<C>, "invalid task work type");
1001992
}

taskflow/core/task.hpp

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,73 @@
44

55
namespace tf {
66

7+
8+
// ----------------------------------------------------------------------------
9+
// Task Traits
10+
// ----------------------------------------------------------------------------
11+
12+
/**
13+
@struct is_static_task
14+
15+
@brief determines if a callable is a static task
16+
*/
17+
template <typename C>
18+
struct is_static_task {
19+
20+
using task_traits = function_traits<C>;
21+
22+
constexpr static bool value = (
23+
std::is_same<typename task_traits::return_type, void>::value &&
24+
task_traits::arity == 0
25+
);
26+
};
27+
28+
template <typename C>
29+
constexpr bool is_static_task_v = is_static_task<C>::value;
30+
31+
/**
32+
@struct is_dynamic_task
33+
34+
@brief determines if a callable is a dynamic task
35+
*/
36+
template <typename C>
37+
struct is_dynamic_task {
38+
39+
using task_traits = function_traits<C>;
40+
41+
using arg = typename std::conditional<task_traits::arity==1,
42+
typename task_traits::template argument_t<0>,
43+
void
44+
>::type;
45+
46+
constexpr static bool value = (
47+
std::is_same<typename task_traits::return_type, void>::value &&
48+
std::is_same<arg, Subflow&>::value
49+
);
50+
};
51+
52+
template <typename C>
53+
constexpr bool is_dynamic_task_v = is_dynamic_task<C>::value;
54+
55+
/**
56+
@struct is_condition_task
57+
58+
@brief determines if a callable is a condition task
59+
*/
60+
template <typename C>
61+
struct is_condition_task {
62+
63+
using task_traits = function_traits<std::decay_t<C>>;
64+
65+
constexpr static bool value = (
66+
std::is_same<typename task_traits::return_type, int>::value &&
67+
task_traits::arity == 0
68+
);
69+
};
70+
71+
template <typename C>
72+
constexpr bool is_condition_task_v = is_condition_task<C>::value;
73+
774
// ----------------------------------------------------------------------------
875
// Task
976
// ----------------------------------------------------------------------------
@@ -129,10 +196,8 @@ class Task {
129196

130197
/**
131198
@brief resets the task handle to null
132-
133-
@return @c *this
134199
*/
135-
Task& reset();
200+
void reset();
136201

137202
/**
138203
@brief queries if the task handle points to a task node
@@ -228,9 +293,8 @@ inline Task& Task::name(const std::string& name) {
228293
}
229294

230295
// Procedure: reset
231-
inline Task& Task::reset() {
296+
inline void Task::reset() {
232297
_node = nullptr;
233-
return *this;
234298
}
235299

236300
// Function: name

taskflow/utility/traits.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ struct function_traits<R(Args...)> {
166166

167167
// function pointer
168168
template<typename R, typename... Args>
169-
struct function_traits<R(*)(Args...)> : public function_traits<R(Args...)> {
169+
struct function_traits<R(*)(Args...)> : function_traits<R(Args...)> {
170170
};
171171

172172
// function reference
173173
template<typename R, typename... Args>
174-
struct function_traits<R(&)(Args...)> : public function_traits<R(Args...)> {
174+
struct function_traits<R(&)(Args...)> : function_traits<R(Args...)> {
175175
};
176176

177177
// immutable lambda

0 commit comments

Comments
 (0)