QM 5.1.3
Generating State Machine Code

In QM™ a State Machine can be associated only with a class that is a direct or indirect subclass of the QP Framework base class QHsm, shown in blue in the class diagram below.

Main Classes in the QP/C and QP/C++ frameworks
Note
QM™ supports classes and inheritance regardless of the target programming language, which currently can be either C or C++. The Application Note "Simple Object-Oriented Programming in C" describes how the QP/C and QP-nano frameworks as well as the QM™ code generator implement classes and inheritance in portable ANSI C.

Implementation Strategies

The QHsm base class (1) provides the basic interface init() and dispatch() for initializing a state machine and for dispatching events to it, respectively. The specific implementations of the state machine interface in the QHsm base class and its subclass QMsm (3) determine the following state machine implementation strategies for code generation:

The QHsm/QActive-Style Implementation Strategy

The QHsm and QActive classes from the class diagram above re-implement the basic state machine interface, and so they provide an alternative state machine implementation strategy that was originally designed for manual coding of HSMs, but now can also benefit from automatic code generation by QM™

The older QHsm/QActive-style state machines are less efficient in time (CPU cycles) and space (e.g., stack usage) than the newer QMsm/QMActive-Style State Machines. This is because the QHsm/QActive-style implementation strategy requires discovering the transition-sequences (sequences of exit/entry/initial actions) at runtime as opposed to code-generation time.

Note
You should consider QHsm/QActive-style state machines only when you are still interested in manual coding or maintaining your state machines. (But then you will be working against the strictly forward-engineering nature of QM™)


QMsm/QMActive-Style Implementation Strategy

The QMsm and QMActive classes from the class diagram above provide a very efficient state machine implementation strategy, which requires the assistance of the QM™ tool (as an advanced "state machine compiler") to generate the complete transition-sequences at code-generation time. The resulting code is still highly human-readable, but is not suitable for manual coding or maintaining.

The lab tests indicate that the QMsm/QMActive-style state machines can be about twice as fast as the QHsm-style state machines (considering only the state machine "housekeeping" code excluding any user-supplied actions, see the next section below). Additionally, the QMsm/QMActive-style state machines require less runtime support (smaller event processor) and use about 70% less of stack space for the dispatch() operation than QHsm/QActive-style state machines.

Note
The QMsm/QMActive-style state machines are highly recommended over the older QHsm/QActive-style state machines described earlier. You should consider QHsm/QActive-style state machines only when you are still interested in manual coding or maintaining your state machines. (But then you will be working against the strictly forward-engineering nature of QM™)
Attention
Starting from version 4, QM™ requires a QM license certificate to generate the QMsm state machine implementation strategy. If you don't have a valid license, the QM Code Generator will report the following licensing errors:

Action Code

Accessing Attributes (me-> Pointer)

Accessing Event Parameters (e-> Pointer)


Next: Generating Comments