jp-embedded / scxmlcc

The SCXML state machine to C++ compiler
GNU General Public License v3.0
138 stars 34 forks source link

sc.get_state #108

Closed hanhao44 closed 4 years ago

hanhao44 commented 4 years ago

Could you give me a example about get_state,this function has a "public" in class sc, how can i use it to get the current state,thanks

jp-embedded commented 4 years ago

You can get the current state in the cur_state variable inside the model. This points to the current state. Note, if you are using parallel states, multiple states can be active, so in this case, cur_state will contain a list of states.

get_state is used to get a pointer to a specific state.

There is also an 'In' method, which can be used to test if the current state is in a specific state.

Here is a modified hello_world.cpp examples, which accesses the current state

template<> void sc::state_actions<sc::state_hello>::enter(sc::data_model &m)
{
    cout << "hello" << endl;    

    cout << "cur_state: " << typeid(*m.cur_state).name() << endl;
}

template<> void sc::state_actions<sc::state_world>::enter(sc::data_model &m)
{
    cout << "world" << endl;    
}

int main(int argc, char *argv[])
{
    sc sc;
    sc.init();

    return 0;
}