Boost logo

Boost Users :

Subject: Re: [Boost-users] [statechart] Is it possible to have nested (sub)states in an asynchronous state machine?
From: Gurcan Gulesir (gurcan.gulesir_at_[hidden])
Date: 2011-06-09 09:09:12


Hi Andreas,

Many thanks for your help first of all!

Upon your request, I wrote a small but complete program that demonstrates
the issue. This program consists of 5 source code files:

1) main.cpp
2) StateMachine.hpp
3) StateMachine.cpp
4) SuperState.hpp
5) SubState.hpp

The contents of these files are listed at the end of this e-mail. You should
be able to compile these listings and then run the executable successfully.
Depending on your compiler flags, you may receive one warning that says "/*
within comment". This is intentional, so please ignore the warning.

To reproduce my problem regarding the nesting of states, please modify the
listings in three locations, as explained in the comments in SuperState.hpp
and SubState.hpp, and then re-compile. You should receive multiple errors
pointing to the Boost Statechart library.

Looking forward to your feedback.

Thanks & Regards,
Gurcan

----------------------------------------
1) main.cpp
----------------------------------------

#include <iostream>

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/thread/thread.hpp>
#include <boost/statechart/asynchronous_state_machine.hpp>
#include <boost/statechart/fifo_worker.hpp>

#include "StateMachine.hpp"

int main() {
    std::cout<<"\n\nPress any key and then <CR> to exit\n\n"<<std::endl;
    char key;

    boost::statechart::fifo_scheduler<> scheduler(true);
    boost::statechart::fifo_scheduler<>::processor_handle handle =
scheduler.create_processor<StateMachine> ();
    scheduler.initiate_processor(handle);
    boost::thread
aThread(boost::bind(&boost::statechart::fifo_scheduler<>::operator(),&schedu
ler, 0));

    std::cin >> key;

    scheduler.destroy_processor(handle);
    scheduler.terminate();
    aThread.join();

    return 0;
}

----------------------------------------
2) StateMachine.hpp
----------------------------------------

#ifndef StateMachine_hpp
#define StateMachine_hpp

#include <boost/statechart/asynchronous_state_machine.hpp>

class StateMachine;

class SuperState;

namespace boost {
namespace statechart {
template<>
inline void asynchronous_state_machine<StateMachine,
SuperState>::initiate_impl() {}
}}

class StateMachine: public
boost::statechart::asynchronous_state_machine<StateMachine, SuperState> {

public:
    StateMachine(my_context ctx) : my_base(ctx) {}

private:
    virtual void initiate_impl();
};

#endif

----------------------------------------
3) StateMachine.cpp
----------------------------------------

#include <boost/statechart/state_machine.hpp>

#include "StateMachine.hpp"
#include "SuperState.hpp"

void StateMachine::initiate_impl() {
    boost::statechart::state_machine<StateMachine, SuperState>::initiate();
}

----------------------------------------
4) SuperState.hpp
----------------------------------------

#ifndef SuperState_hpp
#define SuperState_hpp

#include <iostream>
#include <boost/statechart/simple_state.hpp>

#include "StateMachine.hpp"
#include "SubState.hpp"

class StateMachine;

class SubState;

//MODIFICATION 1 of 3: UNCOMMENT THE FOLLOWING LINE
//class SuperState: public boost::statechart::simple_state<SuperState,
StateMachine, SubState> {

//MODIFICATION 2 of 3: COMMENT OUT THE FOLLOWING LINE
class SuperState: public boost::statechart::simple_state<SuperState,
StateMachine> {

public:

    SuperState(){
        std::cout<<"Entered into SuperState."<<std::endl;
    }

};

#endif

----------------------------------------
5) SubState.hpp
----------------------------------------

//MODIFICATION 3 of 3: COMMENT OUT THE FOLLOWING LINE
/*

#ifndef SubState_hpp
#define SubState_hpp

#include <iostream>
#include <boost/statechart/simple_state.hpp>

#include "SuperState.hpp"

class SuperState;

class SubState: public boost::statechart::simple_state<SubState, SuperState>
{

public:

    SubState(){
        std::cout<<"Enteres into SubState."<<std::endl;
    }

};

#endif
/**/


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net