-
Notifications
You must be signed in to change notification settings - Fork 1
/
doppl_task_loop.hpp
58 lines (45 loc) · 1.65 KB
/
doppl_task_loop.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef _DOPPL_TASK_LOOP
#define _DOPPL_TASK_LOOP
#include "doppl_libs.hpp"
#include "doppl_forward_decl.hpp"
#include "doppl_state_member.hpp"
namespace doppl {
template<typename T, typename... Ts>
class task_loop {
private:
SM<T> initial_state;
SM<T> next_state;
SM<T> finish_state;
std::future<void> current_state_future;
public:
int operator() (SM<T, Ts...>& init, std::promise<T>& yield, Ts&... args) {
//Set init
initial_state.set(init, args...);
//Infinite loop espace trigger
bool is_running = true;
//Set finish state
finish_state.set([&is_running] (std::promise<T>& yield, SM<T>& next, SM<T>& finish) { is_running = false; });
//Default next state assignment
next_state.set(finish_state);
//Initial state assignment (lazy)
current_state_future = std::async(std::launch::deferred, [&] () {
initial_state.get()(yield, next_state, finish_state);
});
//Program loop
try {
while(is_running) {
//Run lazily assigned state code
current_state_future.get();
//Set next state (lazy)
current_state_future = std::async(std::launch::deferred, [&] () {
next_state.get()(yield, next_state, finish_state);
});
}
} catch(std::exception& e) {
return 1;
}
return 0;
};
};
};
#endif