39 template <
typename T,
typename ChainType,
typename StatusType, StatusType failure_status>
44 using state = std::function<StatusType(
T*)>;
76 return *
static_cast<ChainType*
>(
this);
88 throw std::logic_error{
"From-state was already set. Did you forget to append "
89 "on_success() or on_failure()?"};
92 return *
static_cast<ChainType*
>(
this);
104 throw std::logic_error{
"To-state was already set. Did you forget to append "
105 "on_success() or on_failure()?"};
108 return *
static_cast<ChainType*
>(
this);
116 throw std::logic_error{
"Both from-state and to-state must be set with "
117 "from(...).to(...)."};
140 return *
static_cast<ChainType*
>(
this);
154 throw std::logic_error{
"When using on_any_failure(), from-state must be unset."};
160 return *
static_cast<ChainType*
>(
this);
168 throw std::logic_error{
"From-state or to-state set but not commited with .on()"};
173 throw std::logic_error{
"Cannot run without a start state."};
176 long current_state_id;
182 current_state_id =
get_id(next_state);
204 auto key = std::make_tuple(state_id,
status);
217 return get_id(fn.value());
223 return *(
long*)(
char*)&fn;
227 template <
typename T>
250 template <
typename T>
270 template <
typename T>
272 template <
typename T>
274 template <
typename T>
276 template <
typename T>
278 template <
typename T>
280 template <
typename T>
BinaryFiniteStateMachine & on_success()
BoolFiniteStateMachine & on_success()
virtual StatusType run() const
std::optional< state > m_init_to_state
std::optional< state > m_init_from_state
FiniteStateMachine(T &context)
std::optional< state > get_next_state(const long state_id, const StatusType status) const
static const StatusType failure_status_value
FiniteStateMachine(T *context)
ChainType & start_from(state start_state)
ChainType & on_any_failure()
long get_id(const state &fn) const
ChainType & on(StatusType status)
long get_id(std::optional< state > fn) const
std::map< long, std::optional< state > > m_states
std::optional< state > m_start_state
std::function< StatusType(T *)> state
FiniteStateMachine(std::shared_ptr< T > context)
std::map< std::tuple< long, StatusType >, long > m_transitions
std::optional< state > m_default_fail_state
ChainType & from(state from_state)
ChainType & to(state to_state)
This file offers overloads of toIce() and fromIce() functions for STL container types.
BoolFiniteStateMachine(T *) -> BoolFiniteStateMachine< T >
User-defined template argument deductions.
BinaryFiniteStateMachine(T *) -> BinaryFiniteStateMachine< T >