|
| 1 | +/* |
| 2 | + * Copyright 2025 Aethernet Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef AETHER_ACTIONS_TIMER_ACTION_H_ |
| 18 | +#define AETHER_ACTIONS_TIMER_ACTION_H_ |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "aether/common.h" |
| 24 | +#include "aether/state_machine.h" |
| 25 | +#include "aether/actions/action.h" |
| 26 | +#include "aether/events/event_subscription.h" |
| 27 | + |
| 28 | +namespace ae { |
| 29 | +class TimerAction : public Action<TimerAction> { |
| 30 | + enum class State : std::uint8_t { |
| 31 | + kStart, |
| 32 | + kWait, |
| 33 | + kTriggered, |
| 34 | + kStopped, |
| 35 | + }; |
| 36 | + |
| 37 | + public: |
| 38 | + TimerAction() = default; |
| 39 | + |
| 40 | + template <typename TActionContext> |
| 41 | + TimerAction(TActionContext&& action_context, Duration duration) |
| 42 | + : Action{std::forward<TActionContext>(action_context)}, |
| 43 | + timer_duration_{duration}, |
| 44 | + state_{State::kStart}, |
| 45 | + state_changed_sub_{state_.changed_event().Subscribe( |
| 46 | + [this](auto) { Action::Trigger(); })} {} |
| 47 | + |
| 48 | + TimerAction(TimerAction const& other) = delete; |
| 49 | + TimerAction(TimerAction&& other) noexcept; |
| 50 | + |
| 51 | + TimerAction& operator=(TimerAction const& other) = delete; |
| 52 | + TimerAction& operator=(TimerAction&& other) noexcept; |
| 53 | + |
| 54 | + TimePoint Update(TimePoint current_time) override; |
| 55 | + |
| 56 | + void Stop(); |
| 57 | + |
| 58 | + private: |
| 59 | + Duration timer_duration_; |
| 60 | + TimePoint start_time_; |
| 61 | + StateMachine<State> state_; |
| 62 | + Subscription state_changed_sub_; |
| 63 | +}; |
| 64 | +} // namespace ae |
| 65 | + |
| 66 | +#endif // AETHER_ACTIONS_TIMER_ACTION_H_ |
0 commit comments