State Machines - Meeting C++ 2013 aftermath 1
At this years Qt Developer Days in Berlin there was a talk that mentioned The State Machine Framework from Qt but I didn't have the time to look into it. I just put it onto my never ending to do list.
I was fortunate enough to also attend Meeting C++ 2013 in Düsseldorf and watched Christophe Henry's talk about Asynchronous programming using the Boost Meta State Machine library. In the beginning of the talk Christophe gave a little overview of what a state machine basically is. I don't remember him mentioning the name but it looked like Harel's Statecharts also referenced from the Qt documentation.
The first thing that came to my mind was: this can fundamentally change the way I code!.
Raise your hand if you never ended up in some kind of spaghetti code when GUI programming was involved and the transitions from one state into the other (funny, I even used to have the same kind of wording) were stored in a myriad of bool variables. I must confess of having done that.
For some time now I was pondering about a way to organize multi platform code. More or less clear was the idea of putting the business logic into a library. This sounds quite easy in theory but providing a clean interface between your data, the logic and the GUI (= outside world) was not. Well, here we are, state machine to the rescue.
What I really liked about Christophe's talk and the boost library was the way the code was organized and being the documentation at the same time. Even someone without a programming background could actually read this! (Stripped of all the comments that is. I am really sure that Peter Sommerlad would have called them "verboten!")
But then I remembered the earlier talk in Berlin where the Qt state machine was mentioned. What's the difference about the implementations? Which one would better suit my needs?
Reading the Qt documentation I came the conclusion that it is really hard wired to the Qt objects, signals and slots and so on. Not exactly what I had in mind and besides that, it's ugly (sorry):
//QStateMachine machine;
QState *s1 = new QState();
QState *s2 = new QState();
QState *s3 = new QState();
//Then, we create the transitions by using the QState::addTransition() function:
s1->addTransition(button, SIGNAL(clicked()), s2);
s2->addTransition(button, SIGNAL(clicked()), s3);
s3->addTransition(button, SIGNAL(clicked()), s1);
//Next, we add the states to the machine and set the machine's initial state:
machine.addState(s1);
machine.addState(s2);
machine.addState(s3);
machine.setInitialState(s1);
//Finally, we start the state machine:
machine.start();
In the introduction of the Boost Meta State Machine there is a list of snags of common state machine libraries, here is one example of how they might look:
state s1 = new State; // a state
state s2 = new State; // another state
event e = new Event; // event
s1->addTransition(e,s2); // transition s1 -> s2
Looks familiar?
So in the next few days I will give the boost library a chance. There is even a complete PDF inside the doc folder in the library source code folder. Good for offline reading on the train.
That's the way I like it…aha…aha
Tony van Eerd gave a quite longish but diverse and very interesting keynote at Meeting C++ 2013 and besides many, many interesting topics he showed an image of a video game console (the brand doesn't really matter here) and talked about how the gamepad was the pure manifestation of a controller in the context of Model View Controller. The buttons on the gamepad are nothing more and nothing less: just buttons that don't know anything about the outside world. Not even their own status, they just tell a status change to the outside world.
I'm just catching a glimpse of an idea of how to organize GUI/code/programming in future. It may seem in a way more complicated than the classic approach but in the end it will pay off.
A big thank you goes out to all the people involved in changing my way of thinking!
Update
On Jolla phone the boost libraries are currently not available. Seems that I have to look into the Qt state machine as well.