6

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
nickb
  • 59,313
  • 13
  • 108
  • 143

1 Answers1

5

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 >();
        }
};
nickb
  • 59,313
  • 13
  • 108
  • 143
  • 1
    You should not update the question with the correct answer, just answer the question. People like to see the original issue. – Gregor Brandt Nov 15 '11 at 01:06
  • 1
    @GregorBrandt - My mistake, I've reverted the OP to my original question (I think), which should fix everything. Thanks! – nickb Nov 15 '11 at 01:14
  • 1
    So, basically, you forgot the context? It always helps to explicitly point out what corrections where made. – Xeo Nov 15 '11 at 01:41
  • 2
    @Xeo Good point! The context is necessary because I was incorrectly inheriting from `simple_state`, and you need to inherit from `state`. – nickb Nov 15 '11 at 01:48