Skip to content

Transition Guards

Sergei Fedorov edited this page Nov 20, 2016 · 2 revisions

A transition guard is a predicate that can be used to select a transition based on some condition. A true value enables the transition, false - prohibits it.

Signature accepting event

struct condition_a {
    template < typename FSM, typename State, typename Event >
    bool
    operator()(FSM const&, State const&, Event const&)
    {
        return some_knowledge() ? true : false;
    }
};

Signature not wanting event

struct condition_b {
    template < typename FSM, typename State >
    bool
    operator()(FSM const&, State const& )
    {
        return some_knowledge() ? true : false;
    }
};

The guard's operator() is passed const references to the immediate enclosing state machine and current state. Optionally it can accept const reference to the event handled. If there is ambiguity (both signatures are defined and both can be used), the operator accepting more parameters is selected.

Predicate Logical Operations

Negation

The predicates can be negated by ::psst::meta::not_ template, e.g. not_<condition_a>. The ::psst::meta::not_ has a type alias not_ defined inside ::afsm::def::state and ::afsm::def::state_machine templates.

Conjunction and Disjunction

Predicates can be combined by ::psst::meta::and_ and ::psst::meta::or_ templates, e.g. and_< condition_a, not_<condition_b> >. Type aliases for those templates inside ::afsm::def::state and ::afsm::def::state_machine templates are and_ and or_ respectively.