Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame^] | 1 | #include "aos/common/actions/actions.h" |
| 2 | |
| 3 | namespace aos { |
| 4 | namespace common { |
| 5 | namespace actions { |
| 6 | |
| 7 | void ActionQueue::EnqueueAction(::std::unique_ptr<Action> action) { |
| 8 | if (current_action_) { |
| 9 | LOG(INFO, "Queueing action, canceling prior\n"); |
| 10 | current_action_->Cancel(); |
| 11 | next_action_ = ::std::move(action); |
| 12 | } else { |
| 13 | LOG(INFO, "Queueing action\n"); |
| 14 | current_action_ = ::std::move(action); |
| 15 | current_action_->Start(); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | void ActionQueue::CancelCurrentAction() { |
| 20 | LOG(INFO, "Canceling current action\n"); |
| 21 | if (current_action_) { |
| 22 | current_action_->Cancel(); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | void ActionQueue::CancelAllActions() { |
| 27 | LOG(DEBUG, "Cancelling all actions\n"); |
| 28 | if (current_action_) { |
| 29 | current_action_->Cancel(); |
| 30 | } |
| 31 | next_action_.reset(); |
| 32 | } |
| 33 | |
| 34 | void ActionQueue::Tick() { |
| 35 | if (current_action_) { |
| 36 | if (!current_action_->Running()) { |
| 37 | LOG(INFO, "Action is done.\n"); |
| 38 | current_action_ = ::std::move(next_action_); |
| 39 | if (current_action_) { |
| 40 | LOG(INFO, "Running next action\n"); |
| 41 | current_action_->Start(); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool ActionQueue::Running() { return static_cast<bool>(current_action_); } |
| 48 | |
| 49 | bool ActionQueue::GetCurrentActionState(bool* has_started, bool* sent_started, |
| 50 | bool* sent_cancel, bool* interrupted, |
| 51 | uint32_t* run_value, |
| 52 | uint32_t* old_run_value) { |
| 53 | if (current_action_) { |
| 54 | current_action_->GetState(has_started, sent_started, sent_cancel, |
| 55 | interrupted, run_value, old_run_value); |
| 56 | return true; |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | bool ActionQueue::GetNextActionState(bool* has_started, bool* sent_started, |
| 62 | bool* sent_cancel, bool* interrupted, |
| 63 | uint32_t* run_value, |
| 64 | uint32_t* old_run_value) { |
| 65 | if (next_action_) { |
| 66 | next_action_->GetState(has_started, sent_started, sent_cancel, interrupted, |
| 67 | run_value, old_run_value); |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | } // namespace actions |
| 74 | } // namespace common |
| 75 | } // namespace aos |