QM  6.1.2
Model-Based Design Tool
Loading...
Searching...
No Matches
$define

$declare$define1

The $define ${<model-item>} directive requests generating a definition of the <model-item>. QM can generate definitions for the following item types:

Remarks
Code generation for State Machines (either as part of Classes or separatetly) is described in separate Section Generating Code for State Machines.

Class Definition

Class Calc with a State Machine used in the following examples of code generation

The directive $define ${<Class>} generates the class definition, which consists of all operations (member functions) defined in the class as well as all static attributes (class variables). Additionally, if the class has a State Machine, the State Machine code will also be generated.

Note
QM generates code only for operations with non-empty operation code filed in the Class Operation Property Sheet. This allows the developer to provide external definitions of such operations (member functions) in other modules.

Class Definition in C

The following listing shows fragments of the generated code for the class Calc in C. The code shows class operations, such as `Calc_eval().

/*$define${SMs::Calc} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
/*${SMs::Calc} .............................................................*/
Calc Calc_inst;
/*${SMs::Calc::eval} .......................................................*/
static bool Calc_eval(Calc * const me,
double op,
uint8_t oper)
{
double result;
. . .
if ((result < -99999999.0) || (99999999.0 < result)) {
BSP_display_error(" Error 1 "); /* out of range */
return false;
}
if ((-0.0000001 < result) && (result < 0.0000001)) {
result = 0.0;
}
BSP_display(result);
return true;
}
. . .
/* Hierarchical State Machine code... */
. . .
/*$enddef${SMs::Calc} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
Remarks
The Calc class operation in the C code above is defined as static because the QM code generator detected that the Calc class declaration was also placed in the same file scope. This use of static is mandated by MISRA-C:2012 Rule 8.8.
Note
Please refer to Section Basic Modeling | Working with Class Operations | Class Operations in C for more information about special class operations, such as constructors and destructors.

Class Definition in C++

The following listing shows fragments of the generated code for the class Calc in C++. The code shows class operations, such as Calc::Calc() constructor and the Calc::eval() operation.

//$define${SMs::Calc} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//${SMs::Calc} ...............................................................
Calc Calc::inst;
//${SMs::Calc::Calc} .........................................................
Calc::Calc()
: QHsm(Q_STATE_CAST(&Calc::initial))
{}
//${SMs::Calc::eval} .........................................................
bool Calc::eval(
double op,
uint8_t oper) noexcept
{
double result;
. . .
if ((result < -99999999.0) || (99999999.0 < result)) {
BSP_display_error(" Error 1 "); // out of range
return false;
}
if ((-0.0000001 < result) && (result < 0.0000001)) {
result = 0.0;
}
BSP_display(result);
return true;
}
//$enddef${SMs::Calc} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note
Please refer to Section Basic Modeling | Working with Class Operations | Class Operations in C++ for more information about special class operations, such as constructors and destructors.

Free Attribute Definition

Free attribute AO_Ship with an "initializer" used in the code generation examples

Free Attribute Definition in C

The following listing shows fragments of the generated code for the free attribute AO_Ship in C.

/*${Shared::AO_Ship} .......................................................*/
QActive * const AO_Ship = &Ship_inst.super;
/*$enddef${Shared::AO_Ship} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
Note
Note the attribute initialization with the provided initializer, which is necessary at the point of definition because the attribute is const and cannot be initialized later.

Free Attribute Definition in C++

The following listing shows fragments of the generated code for the free attribute AO_Ship in C++.

//$define${Shared::AO_Ship} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace GAME {
//${Shared::AO_Ship} .........................................................
QP::QActive * const AO_Ship = &Ship::inst;
} // namespace GAME
//$enddef${Shared::AO_Ship} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note
Note the attribute initialization with the provided initializer, which is necessary at the point of definition because the attribute is const and cannot be initialized later.

Free Operation Definition

Free operation gc() in a package with namespace QF_ resulting in QF_gc()

Free Operation Definition in C

The following listing shows fragments of the generated code for the free operation gc() in C.

/*$define${QF::QF-dyn} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
. . .
/*${QF::QF-dyn::gc} ........................................................*/
void QF_gc(QEvt const * const e) {
. . .
QF_CRIT_STAT // <====
QF_CRIT_ENTRY();
QF_MEM_SYS();
if (e->refCtr_ > 1U) {
. . .
}
}
Note
The free operation gc() in a package QF-dyn with namespace QF_ resulting in QF_gc()

Free Operation Definition in C++

The following listing shows fragments of the generated code for the free operation gc() in C.

//$define${QF::QF-dyn} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace QP {
namespace QF {
. . .
//${QF::QF-dyn::gc} ..........................................................
void gc(QEvt const * const e) noexcept {
std::uint_fast8_t const poolNum = QEvt_getPoolId_(e);
if (poolNum != 0U) { // is it a pool event (mutable)?
QF_CRIT_STAT
. . .
}
}
. . .
Note
The free operation gc() in a package QF-dyn with namespace QF:: resulting in QF::gc()

Package Definition

The $define ${<package>} directive requests generating a recursive definition 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 defined, as described in the first sentence.

Note
The QM™ generates code in the same order as they appear in the Model Explorer.

$declare$define1