
Andrey Semashev wrote:
A pointer to the state's data doesn't help if the state is actually destroyed when being left (the pointer is freed and the data is lost). And storing such pointer in a place somewhere out of the state (i.e. in a base state, a state machine or its some common storage like virtual bases in my implementation) means that data is exposed and is no longer specific to the state. IMO, this breaks the natural state encapsulation that is one of the main ideas of FSM concept.
struct my_fsm : fsm<my_fsm> { // Events enum event { Start, Pause, Stop }; private: struct Data { /* ... */ }; // States struct Operational { Data* data_ptr; }; struct Paused : Operational {}; struct Running : Operational {}; struct Stopped {}; struct Initial {}; Data m_data; // Paused and Running will point to it. Running on_process(id<1>, Initial, integral_c<event,Start>) const; Paused on_process(id<2>, Running, integral_c<event,Pause>) const; Running on_process(id<3>, Paused, integral_c<event,Pause>) const; Stopped on_process(id<4>, Operational, integral_c<event,Stop> ) const; // Last function is most interesting because it defines transitions // for all states derived from Operational. public: my_fsm(); }; Running my_fsm::on_process( id<1> , Initial , integral_c<my_fsm::event,event::Start> ) const { Running result; result.data_ptr = &m_data; return result; } In this example, all states and state data are private. -- Alexander Nasonov http://nasonov.blogspot.com We need not think alike to love alike. -- Francis David -- This quote is generated by: /usr/pkg/bin/curl -L http://tinyurl.com/veusy \ | sed -e 's/^document\.write(.//' -e 's/.);$/ --/' \ -e 's/<[^>]*>//g' -e 's/^More quotes from //' \ | fmt | tee ~/.signature-quote