
Phil Endecott wrote:
Andrey Semashev wrote:
Phil Endecott wrote:
Dear All,
Here's another FSM example which I hope is a bit less trivial than the last one. It's for the escape sequence handling in a terminal emulator.
My view is that the ad-hoc version of the code is sufficiently readable and efficient for my needs, and that the Boost.FSM version does not improve on it.
Actually, the ad-hoc code is a good example for what is known as spagetti-style
Can you elaborate on why you say that? To me, "spagetti" code is code where the flow-of-control goes all over the place in a complex and not easily discernible pattern. In my "ad-hoc" implementation, the main flow-of-control is contained in a single function which all fits on the screen at the same time and has if and switch statements nested at most 4 deep.
Exactly, that's why I call it spagetti. I'm easily lost in all these conditions in the function and, since the function is not complete, I would expect the function to be much bigger than the screen.
If it's the depth of nesting that you dislike it could easily be flattened like this:
Better, but not as good as I'd like to. The code is getting close to what I have presented, and in the end it would become quite similar, except for that switch-case across states in your code would be equivalent to the FSM in mine.
Below is an improved version of the terminal, with corrected mistakes and categorized events.
A couple of questions: - Does it correctly handle digits in normal mode?
Yes. Since "digit" derives from the "printable_char", digits go into "on_process(printable_char const&)".
- How can I access the screen and cursor position from outside? (Or, if I can't access the content of the states from outside, how can I access an externally-stored screen and cursor from inside the states?)
Yes, you can access any state or its base through the "get" method: m_FSM.get< Common >().screen;