4. Implicit statemachine
Leads to ugly state handling code:
void API::startLiveView() {
if (!m_isInitialized) {
if (!initialize()) {
LOGE(“Could not initialize”);
return false;
}
}
if (!m_isCameraStarted {
if (!startCamera()) {
LOGE(“Could not start camera”);
return false;
}
}
...
…
if (m_isLiveviewStarted) {
LOGI(“Already started”);
return true;
}
// Actual code to handle
// starting of live view
...
9. Actual code for live view:
LiveviewStarted::LiveviewStarted() {
// Actual code to handle
// starting of live view
...
}
10. Before:
void API::startLiveView() {
if (!m_isInitialized) {
if (!initialize()) {
LOGE(“Could not initialize”);
return false;
}
}
if (!m_isCameraStarted {
if (!startCamera()) {
LOGE(“Could not start camera”);
return false;
}
}
...
…
if (m_isLiveviewStarted) {
LOGI(“Already started”);
return true;
}
// Actual code to handle
// starting of live view
...
11. Entry / Exit actions
CameraStarted::CameraStarted() {
// Start camera
...
}
CameraStarted::~CameraStarted() {
// Stop camera
...
}
25. Getting Information Out
Not trivial
Asking for states not very nice
Different possible solutions (see also on boost
website)
What we usually do:
Query Event!
26. Getting Information Out: Query
Event
struct EventQueryResolution: public boost::statechart::event<EventQueryResolution>
{
EventQueryResolution(int *w, int *h, bool* s)
: width(w), height(h), success(s)
{
*success = false;
}
int *width;
int *height;
bool *success;
};
State can handle it, set success to true
27. Multithreading
State transitions can only happen in one
thread
Boost statechart supports asynchronous state
machines
Not very convenient to use (I tried ;)
Instead:Wrap state machine!
29. Multithreading: Asynchronous
Wrapper Node
boost statemachine
Event
Event
Event
Client
● Wrap boost statemachine in
synchronous queuing class
● Clients send
(asynchronous) events to
queue
● Queuing classruns in own
thread, processes events
● Can be mixed with
synchronous!
30. More goodies
Catching of unhandled events
Orthogonal states
History
Deferred Events
State Queries
...