QM  4.1.1
Code-Generation Directives

The QM™ code generation process consists of replacing the code-generation directives in the file templates in the model with the corresponding pieces of auto-generated code in the files generated on disk. All other code fragments surrounding the code-generation directives in the file-templates are simply copied to the files on disk. The picture below illustrates the process:

QM Code Generation as Expansion of Code-Generation Directives

Declarations versus Definitions

To work effectively with the code-generation directives in QM™, you need to understand the difference between declarations and definitions in C or C++.


A declaration introduces an identifier and describes its type, be it a type name (struct/class/typedef), object (variable), or a function. A declaration is what the C or C++ compiler needs to accept references to that identifier. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // forward declaration
class bar { bar(); ... }; // no extern allowed for class declarations
The QM™ code generator applies the keyword extern only for declarations of variables.


A definition actually instantiates/implements the identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
bar::bar() {};
In C and C++ a definition can be used in the place of a declaration, meaning that a definition also provides the type information that the compiler needs to accept references to the defined identifier.

General Syntax

The general syntax of the code-generating directives in the QM file templates is $<directive>(<model-item>) or $<directive>${<model-item>}, where $<directive> is one of:

and <model-item> denotes a Fully-Qualified Name of the model item to expand.

The QM tool supports two alternative versions of the code-generating directives:
the old syntax: $<directive>(<model-item>);
and the new (recommended) syntax: $<directive>${<model-item>}

The use of Fully-Qualified Item Names is necessary to unambiguously identify model items for code generation. For example, a class Ship residing inside the package AOs will be specified inside a code-generating directive as AOs::Ship.

Example of $declare() and $define() Directives

Creating Fully-Qualified Names by Drag-n-Drop

Such fully-qualified item names can be, of course, typed by hand in the QM file templates. However, to avoid errors, the Model Explorer allows you to drag-n-drop a model item onto a file-template to create a fully-qualified item name, as illustrated in the animation below:

Creating a Fully-Qualified Item Name by Drag-n-Drop
The code-generation directives are recognized by the QM syntax highlighter and are shown in red color and underlined in the QM Code Editors.

Next: $declare${}