QM  7.0.0
Model-Based Design Tool
Loading...
Searching...
No Matches
$declare

Code Generation$declare1

The $declare ${<model-item>} directive requests generating a declaration of the <model-item> specified between the parentheses. QM can generate declarations for the following item types:

Class Declaration

Class Declaration in C (Header File)

In C, the directive $declare ${<Class>} generates the C "class" declaration, which consists of a C struct with all the attributes of the class and associated C functions for all the operations (see also Key Concept: Object-Oriented Programming in C↑).

/*${AOs::Ship} .............................................................*/
typedef struct Ship {
/* protected: */
QActive super;
/* private: */
uint8_t x;
uint16_t y;
uint8_t exp_ctr;
uint16_t score;
} Ship;
/* public: */
void Ship_ctor(Ship * const me);
extern Ship Ship_inst;
/* protected: */
QState Ship_initial(Ship * const me, void const * const par);
QState Ship_active(Ship * const me, QEvt const * const e);
QState Ship_parked(Ship * const me, QEvt const * const e);
QState Ship_flying(Ship * const me, QEvt const * const e);
QState Ship_exploding(Ship * const me, QEvt const * const e);
Remarks
A class with a State Machine, such as Ship in the example above, contains also the state machine code. This code is specific to the state machine implementation strategy, which in this case is based on the QActive base class from the "QP/C framework"↑.

Class Declaration in C (File Scope)

If the $declare ${<Class>} directive is used in a file scope (inside a .c file), the declaration generates function prototypes with the static keyword, as shown in the code generated by the directive $declare ${AOs::Ship} for the class Ship shown in the screen shot above:

Note
The use of the static keyword for all variables and functions declared at file scope is mandated by the MISRA-C:2012 Rule-8.8 (Required)↑.
/*${AOs::Ship} .............................................................*/
. . .
/* public: */
static void Ship_ctor(Ship * const me);
/* protected: */
static QState Ship_initial(Ship * const me, void const * const par);
static QState Ship_active(Ship * const me, QEvt const * const e);
static QState Ship_parked(Ship * const me, QEvt const * const e);
static QState Ship_flying(Ship * const me, QEvt const * const e);
static QState Ship_exploding(Ship * const me, QEvt const * const e);

Class Declaration in C++

In C++, the directive $declare ${<class>} generates the C++ class declaration. For example, the code snippet below shows the code generated by the directive $declare ${AOs::Ship} for the class AOs::Ship shown in the screen shot above:

//${AOs::Ship} ...............................................................
class Ship : public QP::QActive {
private:
std::uint8_t m_x;
std::uint16_t m_y;
std::uint8_t m_exp_ctr;
std::uint16_t m_score;
public:
static Ship inst;
public:
Ship();
protected:
Q_STATE_DECL(initial);
Q_STATE_DECL(active);
Q_STATE_DECL(parked);
Q_STATE_DECL(flying);
Q_STATE_DECL(exploding);
}; // class Ship
Note
A class with a State Machine, such as Ship in the example above, contains also the state machine members. These members are specific to the state machine implementation strategy, which in this case is based on the QP::QActive base class from the "QP/C++ framework"↑.

Free Attribute Declaration

Free attribute is an attribute (variable) declared in a package outside of any class. The $declare ${<free-attribute>} directive requests generating a declaration of the <free-attribute> specified between the parentheses.

Free Attribute Declaration in C

The following code snippet shows the C code generated by the $declare ${AOs::AO_Ship} code-generation directive:

extern QActive * const AO_Ship;

Free Attribute Declaration in C++

The following code snippet shows the C++ code generated by the $declare ${AOs::AO_Ship} code-generation directive. Please note the C++ namespace generated around the variable. This is because the enclosing package AOs provide the namespace GAME:

namespace GAME {
extern QP::QActive * const AO_Ship;
} // namespace GAME

Free Operation Declaration

Free operation is an operation (function) declared in a package outside of any class. The $declare ${<free-opeation>}> directive requests generating a declaration of the <free-operation> specified between the parentheses.

Free Operation Declaration in C

The following code snippet shows the C code generated by the $declare ${AOs::do_bitmaps_overlap} code-generation directive:

/*${AOs::do_bitmaps_overl~} ................................................*/
bool do_bitmaps_overlap(
uint8_t bmp_id1,
uint8_t x1,
uint8_t y1,
uint8_t bmp_id2,
uint8_t x2,
uint8_t y2);

Free Operation Declaration in C++

The following code snippet shows the C++ code generated by the $declare ${AOs::do_bitmaps_overlap} code-generation directive. Please note the C++ namespace generated around the function. This is because the enclosing package AOs provides the namespace GAME:

namespace GAME {
//${AOs::do_bitmaps_overl~} ..................................................
bool do_bitmaps_overlap(
std::uint8_t bmp_id1,
std::uint8_t x1,
std::uint8_t y1,
std::uint8_t bmp_id2,
std::uint8_t x2,
std::uint8_t y2);
} // namespace GAME

Package Declaration

The $declare ${<package>} directive requests generating a recursive declaration of all classes, free attributes, and free operations in the <package>, as described in the sections above. If a package contains other packages, these packages are recursively declared, as described in the first sentence.

Example of a Package declaration
Note
The QM™ generates code in the same order as they appear in the Model Explorer.

Code Generation$declare1