159#include <type_traits>
162namespace aitoolkit::fsm {
168 template <
typename T>
171 virtual ~state() =
default;
173 virtual void enter(T& blackboard) {};
174 virtual void exit(T& blackboard) {};
176 virtual void pause(T& blackboard) {};
177 virtual void resume(T& blackboard) {};
179 virtual void update(T& blackboard) {};
186 template <
typename T>
189 template <
typename S,
typename T>
197 template <
typename T>
203 template <state_trait<T> S>
205 if (m_current_state) {
206 m_current_state->exit(blackboard);
209 m_current_state = std::make_unique<S>(
state);
210 m_current_state->enter(blackboard);
213 m_current_state->pause(blackboard);
221 if (m_current_state) {
222 m_current_state->exit(blackboard);
223 m_current_state =
nullptr;
233 if (m_current_state) {
234 m_current_state->pause(blackboard);
244 if (m_current_state) {
245 m_current_state->resume(blackboard);
257 if (m_current_state) {
258 m_current_state->update(blackboard);
264 bool m_paused{
false};
272 template <
typename T>
278 template <state_trait<T> S>
280 if (!m_state_stack.empty()) {
281 auto& current_state = m_state_stack.back();
282 current_state->pause(blackboard);
285 state.enter(blackboard);
286 m_state_stack.push_back(std::make_unique<S>(
state));
293 if (!m_state_stack.empty()) {
294 auto& current_state = m_state_stack.back();
295 current_state->exit(blackboard);
296 m_state_stack.pop_back();
299 if (!m_state_stack.empty()) {
300 auto& current_state = m_state_stack.back();
301 current_state->resume(blackboard);
309 if (!m_state_stack.empty()) {
310 auto& current_state = m_state_stack.back();
311 current_state->update(blackboard);
316 std::vector<state_ptr<T>> m_state_stack;
std::unique_ptr< state< T > > state_ptr
Heap-allocated pointer to a state.
Definition fsm.hpp:187