Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Boost statechart, how can I transition to a state unconditionally?

I have a state A that I would like to transition to its next state B unconditionally, once the constructor of A has completed. Is this possible?

I tried posting an event from the constructor, which does not work, even though it compiles. Thanks.

Edit: Here is what I've tried so far:

struct A : sc::simple_state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A()
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};

This yields the following runtime assertion failure:

Assertion failed: get_pointer( pContext_ ) != 0, file /includ
e/boost/statechart/simple_state.hpp, line 459
like image 921
nickb Avatar asked Oct 12 '11 18:10

nickb


People also ask

What is boost statechart?

The Boost Statechart library is a framework that allows you to quickly transform a UML statechart into executable C++ code, without needing to use a code generator. Thanks to support for almost all UML features the transformation is straight-forward and the resulting C++ code is a nearly redundancy-free textual description of the statechart.

What is the difference between state and statecharts?

States in the statechart may be hierarchical, i.e. contain other states and transitions. Statechart is used to show the state space of a given algorithm, the events that cause a transition from one state to another, and the actions that result from state change.

What are States and transitions in statechart?

Statechart has states and transitions. Transitions may be triggered by user-defined conditions (timeouts or rates, messages received by the statechart, and Boolean conditions). Transition execution may lead to a state change where a new set of transitions becomes active.

Why can't I transition from simple_state to state?

The problem is that you cannot inherit from simple_state to achieve the desired transition, you must inherit from state, which then requires that you modify the constructor accordingly.


1 Answers

I've updated my OP with my solution, here it is so I can close the question.

The problem is that you cannot inherit from simple_state to achieve the desired transition, you must inherit from state, which then requires that you modify the constructor accordingly.

struct A : sc::state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A( my_context ctx ) : my_base( ctx )
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};
like image 132
nickb Avatar answered Oct 19 '22 08:10

nickb