
Hello Alexander, Wednesday, December 20, 2006, 12:15:57 AM, you wrote:
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(); };
[snip on_process]
In this example, all states and state data are private.
This is not quite what I meant. In your example the Data structure holds variables specific to Paused and Running states (let's assume the Operational state is only used as a simple base class, not a final state). But instead of being hidden inside of them it becomes the whole machine's member. Had I ten states with their specific data, the my_fsm class would get too crowded. Besides, nothing prevents you to access or modify this data from, say, "Running on_process(id<1>, Initial, integral_c<event,Start>) const;" handler which has nothing to do with neither Operational nor Running or Paused states. From my point of view a state should be a part of the state machine since it may contain some specifiŅ data. It may not be a lightweight compound and it may want to keep its internal state through the whole machine's lifetime, which may be long enough and even cyclic. -- Best regards, Andrey mailto:andysem@mail.ru