-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathaction_executor.h
113 lines (92 loc) · 2.78 KB
/
action_executor.h
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#ifndef TBOX_FLOW_EXECUTOR_H_20221112
#define TBOX_FLOW_EXECUTOR_H_20221112
#include <deque>
#include <array>
#include <tbox/base/defines.h>
#include <tbox/event/loop.h>
#include <tbox/util/variables.h>
namespace tbox {
namespace flow {
class Action;
/**
* 动作执行器
*
* FIXME: 没有做对Action::block()功能的支持
*/
class ActionExecutor {
public:
ActionExecutor();
virtual ~ActionExecutor();
NONCOPYABLE(ActionExecutor);
IMMOVABLE(ActionExecutor);
public:
using ActionId = int;
using ActionCallback = std::function<void(ActionId)>;
using Callback = std::function<void()>;
/**
* \brief 追加一个���执行的动作
*
* \param action 动作对象
* \param prio 优先级,0:紧急,1:普通,2:低,默认为普通
*
* \return ActionId 动作ID
*/
ActionId append(Action *action, int prio = 1);
//! 获取当前正在执行的动作的ID
ActionId current() const;
//! 取消当前的动作
bool cancelCurrent();
/**
* \brief 取消指定的动作
* \param action_id 动作ID
*
* \return true 如果找到该动作并删除成功
* \return false 没有找到该动作
*/
bool cancel(ActionId action_id);
//! 取消所有动作
void cancelAll();
//! set callbacks
void setActionStartedCallback(const ActionCallback &cb) { action_started_cb_ = cb; }
void setActionFinishedCallback(const ActionCallback &cb) { action_finished_cb_ = cb; }
void setAllFinishedCallback(const Callback &cb) { all_finished_cb_ = cb; }
inline util::Variables& vars() { return vars_; }
private:
ActionId allocActionId();
void schedule();
private:
struct Item {
ActionId id;
Action *action;
};
std::array<std::deque<Item>, 3> action_deque_array_;
ActionId action_id_alloc_counter_ = 0;
int curr_action_deque_index_ = -1; //!< 当前正在运行中的动作所在的队列,-1表示没有动作
ActionCallback action_started_cb_;
ActionCallback action_finished_cb_;
Callback all_finished_cb_;
int cb_level_ = 0;
util::Variables vars_;
};
}
}
#endif //TBOX_FLOW_EXECUTOR_H_20221112