UML and SysML to C++ - Class Code Generator Specification

This section describes the UML and SysML to C++ code generation for structured classes.

This page discusses:

Glossary

Acronym Definition
SPO Software Production Operation
BL Behavior Layer
ML Middleware Layer
BSM Behavior Specific Middleware
TSM Target Specific Middleware

Referenced Documents

Document Internal Reference
Targeted Application Concept [1]
C++ Code Architecture Specification [2]
UML and SysML to C++ - Interface Code Generation Specifications [3]
UML and SysML to C - Statemachine Code Generator Specification [4]

General Concepts

UML Class code generation is part of the components’ generation as presented in [1] and is invoked during the execution of a UML to Cpp operation as presented in [3].

This document focuses on the C++ code generation for the class model.

Supported Elements

Below is an exhaustive list of elements used by the code generator to produce the Code with a summary of the purpose and impact.

Element Description
Attached File File attached to a UML Class. This file is extracted during the code generation.
Class Element for which a cpp file is generated.
Association An Association is a relation between two classes. It can be directed or bidirectional. It can be public, protected, or private.
Aggregation An Aggregation is a kind of association with a meaning of containment. It can be directed or bidirectional. Can be public, protected, or private.
Composition A Composition means that the root class is composed and another class. It can be directed or bidirectional. Can be public, protected, or private.
UML Data Type SysML Value Type Type of a Property, ValueProperty, Argument, or Flow Port.
Flow Property A Flow Property is a member of an Interface Block referenced by a Proxy Port that represents a data transmitted by that port.
Generalization A Class can inherit from another class.
Interface Realization Relation describing the realization of an interface by a class.
Usage An object uses another class. Generate an include.
Dependency Dependency of one class to another class or interface.
Operation There are two ways to use the Operation element: The member of a class representing a method, or the member of an Interface or an Interface Block.
Property There are two different purposes for the property element: A property can be a member of a class representing an attribute. It can be public, protected, or private. A property can be a member of an Interface referenced by a port and represents a data transmitted by that port.
Classifier Behavior A behavior can be a State Machine, an Activity, or an opaque behavior. The behavior can be in a class itself and attached to an operation.
Port Proxy Port Flow Port Element describing a data exchange point with other classes.
Interface There are two different purposes for the interface element: Element describing elements transmitted through a Port element. Abstract class that can be realized by another class (using the Interface Realization).
Interface Block An element describing elements transmitted through a Proxy Port element.
Signal A Signal is a message fired and received between behaviors.
Artifact represents a library that can be either user or system

Code Generator Specification

This section is divided into two parts:

  • The generated files specification
  • The behavior specification

Files Specifications

Generated code Class elements are composed of (at most) two files for each class:

  • <ClassName>.h
  • <ClassName>.cpp
  • internal_types.h

General requirements releated to file content are described in [2].

<ClassName>.h

<ClassName>.h header file describes the content of a class entity. This file declares all the elements associated to a class.

File <ClassName>.h is generated for the each UML class and UML interface element involved in the code generation.

#include to the file <Top_ClassName>_types.h is generated for the selected UML also known as top Class.

<Top_ClassName>_types.h file contains the types used at interface with top UML class.

#include to the file internal_types.h is generated.

internal_types.h file contains the types used in the selected UML class and its sub-elements.

#include "internal_types.h"

Example: Includes

The header declares, in this order: the #include directives, then the forward declarations, then the type definitions.

A forward declaration announces the existence of a type without disclosing its definition. Every type that is only announced in the header is included in the corresponding implementation file, so the generated code never depends on an indirect inclusion to compile.

The namespace is used in the forward declaration if any. See Semantic Code Specifications for more information.

/**********************************************************************************
 * Forward declaration(s)
 **********************************************************************************/
/* Forward declaration of LockManager */
namespace Car::DoorMngt {
class LockManager;
}

Example: Forward declaration

For every other type the current class depends on — through inheritance, composition, aggregation, association or usage — the header generates either an #include or a forward declaration, depending on what the declaration requires:

  • an #include when the definition is required: a base class, a member held by value, an array of values, and any type that cannot be declared ahead of its definition (an enumeration, a type alias, a record type);
  • a forward declaration when the name is enough: a member declared as a pointer or a reference — which is the case of every association, composition and aggregation relation — as well as the parameter and return types of the declared operations, and the classes declared as friends.
#include "LockableElement.h" \* MISRA rule 12.3 violation */

Example: Additional includes

See Semantic Code Specifications for more information.

The namespace is declared here if any. See Semantic Code Specifications for more information.

If any referenced class is declared in a namespace, then this namespace is declared in the using namespace section. See Semantic Code Specifications for more information.

A user-specific section is generated to insert additional includes.

The class is declared using the regular cpp declaration mechanism. For more information on the declaration, see the dedicated section in Semantic Code Specifications.

/** @generated from: <class uuid> */     
namespace Car::DoorMngt {

using namespace Car::DoorMngt;

/* BEG_USER_CODE(header) do not remove */
/* Add your includes here */
/* END_USER_CODE(header) do not remove */

/** @generated_from _19_0_3_16600ad_1654593306068_127462_42636 */    
class LockManager {

  <CLASS MEMBER DECLARATION>

};
} //Car::DoorMngt

Example: Class declaration

<Class name>.cpp

<ClassName>.cpp source file implements functions declared in thecorresponding header file for a class.

File <ClassName>.cpp is generated for the each UML class element involved in the code generation.

#include to the header c file of the corresponding class is inserted.

#include "LockableElement.h"

Example: Include to header of class

The namespace is declared here if any. See Semantic Code Specifications for more information.

If any referenced class is declared in a namespace, then this namespace is declared in the using namespace section. See Semantic Code Specifications for more information.

The namespace is declared here if any. See Semantic Code Specifications for more information.

A user-specific section is generated to insert additional includes and namespaces.

#include "VehicleDoorsManager.h"
namespace Car::DoorMngt {
using namespace std;
using namespace Doors;
using namespace Car::DoorMngt;
/* BEG_USER_CODE(header) do not remove */
/* Add your includes here */
/* END_USER_CODE(header) do not remove */

<CLASS OPERATION IMPLEMENTATION>

Example: class implementation

internal_types.h

The internal_type.h file describes the data types used by the properties and the operations in the model.

File internal_type.h is generated for the input element which is a Class.

General requirements related to file content and structure are described in [2].

#include to the file stdbool_itf.h is generated.

stdbool.h file defines bool.

Each type used by at interface level Class is generated. See Semantic Code Specifications for more details depending on the elements.

#include <stdbool.h>

<DATA TYPES DECLARATION>

Example: Data types declaration

Based on the dependency between types, each type referencing another type is defined after this type

Each type definition is protected by a #ifdef, #define, #endif mechanism.

Each type is defined via the C typedef mechanism.

/**********************************************************************
 * Data Type <type> definition
 * @generated_from <uuid>
 **********************************************************************/
#ifndef <type>_DEFINITION
#define <type>_DEFINITION
typedef <base_type> <type>;
#endif

Example: Type declaration

Semantic Code Specifications

This section describes the behavior of the code generator depending on the UML element.

Package with C++ namespace stereotype

If a package declares the stereotypes C++ Namespace, then this package is used to declare a namespace in the generated code.

Namespace declaration

nested namespaces

Nested namespace

If c++ version is c++11 namespaces definition shall have this pattern:

namespace A {
    namespace B {
        …
    }
}

If c++ version is greater than c++11, namespaces definition shall have this pattern

namespace A::B {
    …
}

If a UML class is created in a package using c++ Namespace stereotype, then its forward declaration is done in a namespace.

namespace Car::DoorMngt {
class LockManager;
}

Example: Class forward declaration with namespace for c++ version greater than c++11

namespace Car {
    namespace DoorMngt {
        class LockManager;
    }
}

Example: Class forward declaration with namespace for c++ version equal to c++11

If a UML class is created in a package using c++ Namespace stereotype, then its declaration is done in a namespace.

namespace Car::DoorMngt {
class LockManager{
};
} //Car::DoorMngt

Example: Class declaration with namespace for c++ version greater than c++11

namespace Car {
    namespace DoorMngt {
        class LockManager{
        };
    } //DoorMngt
} //Car

Example: Class declaration with namespace for c++ version equal to c++11

If a UML class is created in a package using c++ Namespace stereotype, then a “using namespace” is inserted in the class which depends on the current class.

using namespace Car::DoorMngt;

Example: Using namespace declaration

Class

If generalization relation is created from a class to another one, then this inheritance relation is reproduced in class declaration.

Generalization

Visibility inheritance is supported through C++ generalization stereotype on generalization relation

    /** @generated_from _2026x_1ca00d3_1768838226712_433953_1328 */
    class cls: protected Parent {... 

Example: Class declaration with generalization

virtual inheritance is supported through C++ generalization stereotype on generalization relation

/** @generated_from _2026x_1ca00d3_1768838226712_433953_1328 */

class cls: public virtual Parent {...

If an operation is set to virtual via CppAnsi profile, generated code has a virtual keyword, and any operation which overrides the virtual one has an override suffix.

Operation override

In parentClass.h:

class parentClass{

public:
    void op1();
    virtual void op2();
};

In ChildClass.h:

#include "parentClass.h"

class ChildClass: public parentClass{

public:
    void op2() override;
};

If a class uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set to “gcc”, then the generated code has the keyword __attribute__((packed)).

/** @generated from: <class uuid> */
class __attribute__((packed)) A {
};

Example: Class declaration with packed

If a class uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the “alignment” attribute is set to a valid value, then the generated code includes the keyword alignas({alignment}).

/** @generated from: <class uuid> */
class alignas({alignment}) A {
};

Example: Class declaration using alignas

The keywords alignas({alignment}) and __attribute__((packed)) cannot be applied simultaneously to the same type or variable. When both are defined, the generated code uses alignas({alignment}) and omit __attribute__((packed)).

Nested class

A Nested class is not supported.

A declaration of a class/struct or union may appear within another class. Such declaration declares a nested class.

Friend class
Friend relation between classes

A friend class relation is configured through a usage relation applying the “C++ Friend” stereotype.

A friend class is the target of a usage relation with the stereotype “C++ Friend”. In such case, this class declares the class being the source of relation as a friend of it in the generated code.

class FriendClass{
    friend class ClassA;

Example: Property

Global functions and variables
Global functions and variables

Global functions and variables are declared by using an unnamed class with “C++Global” stereoptype.

Global functions and variables are mapped to operations and attributes into an unnamed class with stereotype «C++Global».

The generated files are named global.cpp and global.h.

If a global variable uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the “alignment” attribute is set to a valid value, then the generated code includes the keyword alignas({alignment}).

alignas({alignment}) int myGlobalVariable

Example: Global variable declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).
Final class

A UML class with isLeaf or isFinalSpecialization set to true is generated as final.

class FinalClass final  {…}

Composition relation – black diamond

CLASS MEMBERS DECLARATION

if a composition relation is created from a class to another one, a variable is created in the source class of relation to point to target class.

if a composition relation is created from a class to another one as not directional, a pointer variable is created in the target class of relation to point to the source class.

Not directional composition relation

class A {
    /**
     * @brief Attribute for composition relation to B:b
     * @generated from: _2022x_16600ad_1674047956690_871296_20640
     */
    B b;
}

Example: variable in source class

class B {
    /**
     * @brief Attribute for aggregation relation to A:a
     * @generated from: _2022x_16600ad_1677666453655_204390_17509
     */
    A *a;
};

Example: variable in target class for not directional relation

if an composition relation exists from a class to another one with the cardinality set to a finite range and upper limit > 1, an array of variables is used.

if an composition relation exists from a class to another one with the cardinality set to a range including “*“, a list of variables is used.

Aggregation relation – white diamond

CLASS MEMBERS DECLARATION

if an aggregation relation is created from a class to another one with a cardinality not set or set to 1, a pointer variable is created in the source class of relation to point to target class.

if an aggregation relation is created from a class to another one as not directional, a pointer variable is created in the target class of relation to point to the source class.

Aggregation relation

class A {
    /**
     * @brief Attribute for association relation to B:b
     * @generated from: _2022x_16600ad_1677666453655_544305_17510
     */
    B *b; 
};

Example: variable in source class

class B {
    /**
     * @brief Attribute for aggregation relation to A:a
     * @generated from: _2022x_16600ad_1677666453655_204390_17509
     */
    A *a;
};

Example: variable in target class for not directional relation

if an aggregation relation exists from a class to another one with the cardinality set to a finite range and upper limit > 1, an array of pointer variables is used.

if an aggregation relation exists from a class to another one with the cardinality set to a range including “*“, a list of pointer variables is used.

Association relation

CLASS MEMBERS DECLARATION

if a directed association relation exists from a class to another one with a cardinality not set or set to 1, a pointer variable is created in the source class pointing to the target.

if an non-directed association relation exists between 2 classes with the cardinality of the releation not set or set to 1, a pointer variable is created in each class to point to the other class.

Association relation

class A {
    /**
     * @brief Attribute for aggregation relation to B:b
     * @generated from: _2022x_16600ad_1677666453655_204390_17509
     */
    B *a;
};

class B {
    /**
     * @brief Attribute for aggregation relation to A:a
     * @generated from: _2022x_16600ad_1677666453655_204390_17509
     */
    A *a;
};

Example: association between 2 classes

if an association relation exists between 2 classes, with the cardinality set to a finite range and upper limit > 1, an array of pointer variables is used.

    /**
     * @generated_brief Private attribute for association relation to array<SuperSetRole2*,2>:superRole2
     * @generated_from _2024x_3_c3a0313_1761929246565_867216_1031
     */
    array<SuperSetRole2*,2> superRole2;

Example: association to another class with a cardinality [0..2]

if an association relation exists between 2 classes, with the cardinality set to a range including “*“, a list of pointer variables is used.

Usage relation

A usage relation is used to declare that a class usage another element. It is translated into an include of the element in the generated code.

A usage relation can be used from a Class to an Interface, another Class or UML artifact.

If a Class declares a usage relation to another entity, the corresponding #include is generated.

For a Class object, the generated #include in the context of a Usage relation is generated by adding automatically the suffix “.hpp”

For an interface object, the generated #include in the context of a Usage relation is generated by using the name of the interface.

For an artifact object, the generated #include in the context of a Usage relation is generated by using the name of the artifact.

/**********************************************************************************
 * Includes declaration
 **********************************************************************************/
#include <stdio.h>

If the “C++ Include” stereotype applied on the usage relation carries the value “Class Forward”, a forward declaration of the used class is generated in the header instead of an #include, and the #include is generated in the implementation file. The value “None” generates neither.

If no “C++ Include” stereotype is applied on the usage relation, the generated include is generated as a user include using ….

If a “C++ Include” stereotype is applied on the usage relation, then it is possible to generate a user include using “” or a system include using <…>. either in generated header or implementation.

For optimization reasons, two new stereotypes are proposed as replacement of C++Include:

“C++ Location“ stereotype applies on usage relation. It supersedes use of “C++ Include“ stereotype. It helps to specify whether used element is targeted for source header or implementation.

“C++ Kind“ stereotype applies on UML artifact. It supersedes information eventually set in “C++ Include“ stereotype.

Example: artifact with “C++IncludeKind“ set to System in Class.hpp

#include <Artifact.hpp>

Example: artifact with “C++IncludeKind“ set to User in Class.hpp

#include "Artifact.hpp"

Example: usage relation with “C++Location“ set to Header in Class.hpp

#include "UsedClass.hpp"

Example: usage relation with “C++Location“ set to Implementation in Class.cpp

#include "UsedClass.hpp"

Interface Realization

Interface Realization is implemented thanks to an include to <Interface>.h.

See Interface chapter for more information.

Interface realization

class ifc{
public:
    virtual void op()=0;
};
#include "ifc.h"
class clazz: public ifc{

public:
    void op() override;
    void clazzOp();
};

Property

CLASS MEMBERS DECLARATION

Each property declared in a UML class is replicated in the generated code.

Property declaration in class

A brief description is generated for any property describing type and name of the variable.

Additional documentation is generated if the documentation section is field in the model.

Type of the property is used as declared in the model.

Visibility of the property is used as declared in the model.

The default value is set as declared in the model.

public:
    /**
     * @brief Attribute for composition relation to LockEnum:lockStatus
     * This is my documentation for LockControl
     * @generated_from _19_0_3_16600ad_1654594678283_920922_43338
     */
    LockEnum lockStatus = Closed;

Example: Property

Pointer and reference declaration

If a type modifier “*” or “&” is set for a property, then it is used to declare a pointer or a reference to a variable.

public:
    /**
     * @brief Attribute for composition relation to String:pointerVar
     * @generated_from _19_0_3_16600ad_1654594678283_920922_43338
     */
    String *pointerVar;

    /**
     * @brief Attribute for composition relation to Integer:referenceVar
     * @generated_from _19_0_3_16600ad_1654594678283_920922_43338
     */
Integer &referenceVar = referenceDefault;

Example: Pointer and reference

List declaration

If the multiplicity for a property is set to “*”, the variable is declared as a list.

If the multiplicity for a property is set to “*”, using namespace std is added to in top of file in the dedicated section.

    /**
     * @brief Attribute list<Boolean>:myList
     * @generated from: <variable uuid>
     */

    list<Boolean> myList;

Example: List declaration

Array declaration
Array configuration

if the property applies the stereotype “C++ Attributes” and “array” value is configured, variable is generated as an array<Variable Type, length>.

    /**
     * @brief Attribute for composition relation to array<Integer,2>:configuration
     * @generated from: _2022x_2_1ca00d3_1683299592870_873326_17375
     */
    array<Integer,2> configuration;

Example: Array declaration

Const

if isReadOnly attribute of the property is set to true, the variable is declared as a const variable.

    /**
     * @brief Attribute Boolean:debugMode
     * @generated_from _2022x_2_1ca00d3_1686732945801_136413_17517
     */
    const Boolean debugMode;

Example: const declaration

Static

if isStatic attribute of the property is set to true, the variable is declared as a static variable.

    /**
     * @brief Attribute Boolean:debugMode
     * @generated_from _2022x_2_1ca00d3_1686732945801_136413_17517
     */
    static Boolean debugMode;

Example: static declaration

Packed

If the property uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set on “gcc”, then the generated code shall have the keyword __attribute__((packed)), except for bitfield, pointer and reference properties.

public:
    /**
     * @brief <brief description>
     * @generated_from <class uuid>
     */
    int __attribute__((packed)) myProperty;

Example: Property declaration with packed

Aligned

If the property uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code includes the keyword alignas({alignment}) except for bitfield and reference properties.

public:
    /**
     * @brief <brief description>
     * @generated_from <class uuid>
     */
    alignas({alignment}) int myProperty1;
    alignas({alignment}) int *myProperty2;

Example: Property declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).

Operation

CLASS MEMBERS DECLARATION

Operation declaration in class

Each operation declared in a UML Class is generated in the header file.

A brief description is generated for any property describing type and name of the variable.

Additional documentation is generated if the documentation section is field in the model.

Visibility of the operation is used as declared in the model.

public:
    /** @generated_from _2022x_16600ad_1673944282153_805594_18447 */
    Boolean lockDoors();

Example: Operation

Default Constructor

A default constructor is automatically declared without any parameter for class object even if not declared in the class in the following cases:

  • when the class contains a statemachine or an activity as classifier behavior.
  • when the class contains operations referencing an activity or an statemachine as method.
class Door{
 /**
  * @brief Constructor
  * @generated from: <function uuid>
  */
  Door();
};

Example: Constructor

User-specific Constructor

If an operation uses the stereotype “C++ Constructor” or “Create” or “constructor” then the function is declared as a constructor in header file.

Explicit constructor
Explicit constructor configuration

If an operation uses the stereotype “C++ Constructor” and attribute Explicit is set to true then the function is generated as an explicit constructor.

class Door{
 /**
  * @brief Constructor
  * @generated from: <function uuid>
  */
  explicit Door();
};

Example: Explicit constructor

Destructor

If an operation uses the stereotype “C++ Destructor” then the function is declared as a destructor in header file.

If an operation uses the stereotype “Destroy” then the function is declared as a destructor in header file.

If an operation uses the stereotype “destructor” then the function is declared as a destructor in header file.

class Door{
 /**
  * @brief Destructor
  * @generated from: <function uuid>
  */
  ~Door();
};

Example: Destructor

A UML abstract class or UML interface without any declared operation has a pure virtual destructor declared in the generated C++ class

public:
    /**
     * @generated_brief Public operation ~AbstractEmptyClass
     * @reverse_ignore
     */
    virtual ~AbstractEmptyClass() = 0;
    ...

   

Getter / setter

Getter/setter can be configured through the contextual menu:

Getter and setter menu

Class with getter and setter of a property

Getter and setter are declared in a header file for each function declared accordingly in the model.

class Door {
 /**
  * @brief Setter method for attribute WindowPosition
  * this is an example of documentation from model
  * @generated from: _2022x_16600ad_1677147742721_382790_17804
  */
 void setWindowPosition(ControlEnum /*in*/ windowPosition);

 /**
  * @brief Getter method for attribute WindowPosition
  * @generated from: _2022x_16600ad_1677147745041_9971_17808
  */
 ControlEnum getWindowPosition();
};

Example: getter & setter

Inline

If an operation uses the stereotype “C++ operation” and attribute Inline is set to true then the function is generated as an inline function.

/** @generated_from _2022x_16600ad_1677575910398_769750_17568 */
    inline bool isLocked();

Example: Inline

Virtual/NoVirtual

Any operation has a virtual keyword applied, unless a C++ANSI_novirtual stereotype is applied.

Virtual applied by default

    /**
     * @generated_brief Public operation op
     * @generated_from _2026x_1_1ca00d3_1770811110998_990206_1732
     */
    virtual void op();

The C++_novirtual stereotype inhibits the default enabling of a virtual mechanism for an operation. When applied to a package, all operations that belong to subclasses are not virtual. When applied to a class, all class operations is not virtual When applied to an operation, this operation is not virtual

NoVirtual applied to a class

    /**
     * @generated_brief Public operation op
     * @generated_from _2026x_1_1ca00d3_1770811110998_990206_1732
     */
    void op();

If an operation uses the stereotype “C++ operation” and the attribute Virtual is set to true then the function is generated as a virtual function. It supersedes C++_novirtual stereotype.

/** @generated_from _2022x_16600ad_1677248284517_509336_17224 */
    virtual void virtualOperation();

Example: Virtual

A virtual attribute set to an operation superseeds C++ novirtual mechanism.

If an operation uses the stereotype “C++ constructor” and the attribute Virtual is set to true, then the function is generated without the keyword “virtual”.

“Virtual” is not allowed for C++ constructors.

An operation has the override keyword if its source UML operation refers another UML operation through a ‘redefined operation’ or sync element attribute.

Redefined operation

class Parent {

public:
    /**
     * @generated_brief Public operation op
     * @generated_from _2026x_1ca00d3_1760023469726_951385_1331
     */
    virtual void op();

class Child: public Parent {
public:
    /**
     * @generated_brief Public operation op
     * @generated_from _2026x_1ca00d3_1760081097259_183445_1353
     */
    void op() override;
Pure Virtual

If an operation uses abstract attribute set to true, then the function is generated as a pure virtual function. A virtual function has no implementation in source code.

/** @generated_from _2022x_16600ad_1677248284517_509336_17224 */
    virtual void virtualOperation()=0;

Example: Pure Virtual

An operation generated from a UML Interface is pure virtual.

Volatile

If an operation uses the stereotype “C++ operation” and the attribute Volatile is set to true then the function is generated as a volatile function.

/** @generated_from _2022x_16600ad_1677248284517_509336_17224 */
    void volatileOperation() volatile;

Example: volatile

Static

If the attribute isStatic is set to true for an operation then the operation is generated as a static function.

    /**
     * @generated_brief Public operation staticFunction
     * @generated_from _2022x_2_1ca00d3_1697710303899_846231_4060
     */
    static Integer staticFunction();
Const

If the attribute isQuery is set to true for an operation, then the operation is generated as a const function.

/** @generated_from _2022x_16600ad_1677248284517_509336_17224 */
    void ConstFunction() const;

Example: const

Friend
Friend relation between operation and class

A friend operation relation is configured through a usage relation applying the “C++ Friend” stereotype from an operation to a class.

A friend class is the target of a usage relation with the stereotype “C++ Friend”. In such a case, this class declares the operation being the source of relation as a friend operation in the generated code. This requirement is not supported. See validation rules.

class FriendClass{
    friend class ClassA;

Example: Property

Function pointer

Function pointer not yet supported. Add a validation rule.

Operator

Operator not yet supported. Add a validation rule.

Exception

Exception not yet supported. Add a validation rule.

Operation behavior

A behavior can be attached to an operation to represent its implementation. This can be done by using the “Method” attribute of an operation.

If an is activity assigned to an operation, then a variable typed with the struct representing the activity is declared in the private section of the class containing the operation.

class SeatController{
private:
    /**
     * Instance of Activity associated to operation keyPressed
     */
    keyPressed_struct keyPressed_Activity;
}

Example: Operation with activity

CLASS OPERATION IMPLEMENTATION

Each operation declared in a UML Class is generated in the source file.

Operation declaration in class

A brief description is generated for any property describing the type and name of the variable.

Additional documentation is generated if the documentation section is field in the model.

User code section is inserted in operation to let the user fills his behavior.

If any, a default return code is inserted in the user code section.

/** @generated_from _2022x_16600ad_1673944282153_805594_18447 */

Boolean VehicleDoorsManager::lockDoors(){

    /* BEG_USER_CODE(_2022x_16600ad_1673944282153_805594_18447) do not remove */
    /* Add your code here */
    return 0;
    /* END_USER_CODE(_2022x_16600ad_1673944282153_805594_18447) do not remove */
}

Example: Operation implementation

Default Constructor

A default constructor is automatically generated without any parameter for a class object even if not declared in the class in the following cases:

  • when the class contains a statemachine or an activity as classifier behavior.
  • when the class contains operations referencing an activity or a statemachine as method.

A default constructor is automatically generated to allow proper initialization of behaviors.

/**
 * Constructor generated automatically to initialize data structure
 * @generated_from _2024x_2_16600ad_1719557654818_342936_1322
 */
A::A(){
    /* Call hierarchical constructors */
    myOp_struct_ctor(&A_myOp_Activity, this);
}

Example: Default Constructor

User-specific Constructor

If an operation uses the stereotype “C++ Constructor” or “Create” or “constructor” then the function is generated as a constructor in the source code.

/**
 * @brief Constructor
 * @generated_from _2022x_16600ad_1677659222512_81989_17260
 */
Door::Door(){

/* BEG_USER_CODE(_2022x_16600ad_1673944282153_805594_18447) do not remove */
    /* Add your code here */
    /* END_USER_CODE(_2022x_16600ad_1673944282153_805594_18447) do not remove */

}

Example: Constructor

Explicit Constructor

If an operation uses the stereotype “C++ Constructor” and attribute Explicit is set to true, then the function is generated as a regular constructor.

Note that the explicit keyword only appears within the class definition. See: https://en.cppreference.com/w/cpp/language/explicit

/**
 * @brief Constructor
 * @generated_from _2022x_16600ad_1677659222512_81989_17260
 */
Door::Door(){

/* BEG_USER_CODE(_2022x_16600ad_1673944282153_805594_18447) do not remove */
    /* Add your code here */
    /* END_USER_CODE(_2022x_16600ad_1673944282153_805594_18447) do not remove */

}

Example: Explicit constructor generated as standard constructor

Getter/Setter

Getter and setter are generated for each function declared accordingly in the model and content is automatically generated to access or modify the associated attribute.

/**
 * @brief Setter method for attribute WindowPosition
 * @generated_from _2022x_16600ad_1677147742721_382790_17804
 */

void Door::setWindowPosition(ControlEnum /*in*/ windowPosition){

    WindowPosition = windowPosition;
}

/**
 * @brief Getter method for attribute WindowPosition
 * @generated_from _2022x_16600ad_1677147745041_9971_17808
 */

ControlEnum Door::getWindowPosition(){

    return WindowPosition;
}

Example: getter & setter

Inline

If an operation uses the stereotype “C++ operation” and the attribute Inline is set to true, then the function is generated as an inline function.

/** @generated_from _2022x_16600ad_1677575910398_769750_17568 */
inline bool LockableElement::isLocked(){

    /* BEG_USER_CODE(_2022x_16600ad_1677575910398_769750_17568) do not remove */
    /* Add your code here */
    return 0;
    /* END_USER_CODE(_2022x_16600ad_1677575910398_769750_17568) do not remove */
}

Example: Inline

Operation behavior

If an opaque behavior is assigned to an operation, then this opaque behavior is replicated in the operation body.

void ClassA::OperationWithBeh(){

    /* BEG_USER_CODE(_2024x_1ca00d3_1700818869010_751926_3013) do not remove */
FriendClass c;
    c.privateOp();
    /* END_USER_CODE(_2024x_1ca00d3_1700818869010_751926_3013) do not remove */
}

Example: Operation with behavior

If an activity is assigned to an operation, then this activity is invoked in the operation body.

The invoke phase consist of a first initialization performed once followed by the call of the activity code.

/**
 * @brief Public operation keyPressed
 * @generated_from _2024x_3_16600ad_1741101627358_33745_20297
 */
void SeatController::keyPressed(Key /*in*/ key) {

    static Boolean first = true;
    if(first){
        _basic_keyPressed_init(&keyPressed_Activity);
        keyPressed_Activity.classifier = this;
        first = false;
    }
    keyPressed_Activity.key=key;
    keyPressed_Activity.WALL_CLOCK = tal_GetTime();
    (void)_basic_keyPressed(&keyPressed_Activity);

}

Example: Operation with activity

Final operation

A UML operation with attribute isLeaf set to true generates a final method.

void op() final

Parameter

The type of parameter of operation with direction set as “return” is used as return type of the operation.

The parameter of operation with direction set as “in” or “out” or “inout” is used as parameter of the operation.

The type of parameter of operation with direction set as “in” or “out” or “inout” is used in parameter declaration of the operation.

The direction “in” or “out” or “inout” of a parameter is used for documentation purposes. A parameter with direction set to in is declared as const.

Operation with in and inout arguments

    /**
     * @generated_brief Public operation op
    @generated_from 2026x_1ca00d3_1768838229586_841776_1329
      */
    virtual void op(const Integer /*in*/ x, Boolean /*in*/ y);

Example: parameters

Pointer declaration

If a type modifier “*” is set for a parameter, then it is used to declare a pointer to a variable.

/**
 * @generated_from _2022x_16600ad_1677147742721_382790_17804
 */
void Door_setWindowPosition(Door* self, ControlEnum* /*out*/ windowPosition);

Example: Pointer

List declaration

If the multiplicity for a property is set to “*”, the parameter is declared as a list.

If the multiplicity for a property is set to “*”, using namespace std is added to in top of file in the dedicated section.

    /** @generated from: _2022x_2_1ca00d3_1687790982594_316859_17456 */
    list<HistoryEntry> getHistoryRange(Integer /*in*/ from, Integer /*in*/ to);


    /** @generated from: _2022x_2_1ca00d3_1687792555350_758911_17497 */
    void getdoorIdHistory(Integer /*in*/ doorId, list<LockEnum> /*out*/ actions);

Example: parameter as list

Array declaration

if the parameter applies the stereotypes “C++ Attributes” and the “array” value is configured, a parameter is generated as an array<Parameter Type, length>.

Array configuration

/** @generated from: _2022x_2_1ca00d3_1685001263779_527188_17508 */
    void setConfiguration(array<Integer,2> /*in*/ configuration);

Example: parameter as array

Variable length parameter list

A variable length parameter list is not yet supported. Add a validation rule.

Dependency relation

In case of a dependency relation between two objects (UML Class or UML Interfaces), the target elements of the relation are automatically generated if the source of the relation is part of the generation.

Flow Port

A variable is expected to be created in the Class to store the values exchanged through the Flow Ports of the Class.

For each Flow Port, a variable typed with the type referenced by the Flow Port is generated.

For each Flow Port, a variable named with the name of the Port and a suffix “_data” is generated.

public:
    /**
     * Data for Flow Port flowPort
     * @generated from: _19_0_3_16600ad_1654593633874_525666_43081
     */
    Integer flowPort_data;

Example: Flow Port variable

Proxy Port & Interface Block and Flow Property

A variable is expected to be created in the Class to store the values exchanged through the Flow Properties transmitted through proxy Ports of the Class.

For each Flow Property contained in an Interface Block that is referenced by a Proxy Port, a variable typed with the type referenced by the Flow Property is generated.

For each Flow Property contained in an Interface Block that is referenced by a Proxy Port, a variable named with the name of the Proxy Port, an “_” and the Flow Property name is generated.

public:
    /**
     * Data for Flow Property myFlowProperty of Interface Block myItf in Proxy Port myPort
     * @generated from: _19_0_3_16600ad_1654593633874_525666_43081
     * @generated from: _19_0_3_16600ad_1654593633874_525666_43082
     * @generated from: _19_0_3_16600ad_1654593633874_525666_43083
     */
    Integer myPort_myItf;

Example: Flow Property variable

Port & Interface & Property

A variable is expected to be created in the Class to store the values exchanged through the Properties transmitted through Ports of the Class.

For each Property contained in an Interface that is referenced by a Port, a variable typed with the type referenced by the Property is generated.

For each Property contained in an Interface that is referenced by a Port, a variable named with the name of the Port, an “_” and the Property name is generated.

public:
    /**
     * Data for Property myFlowProperty of Interface myItf in Port myPort
     * @generated from: _19_0_3_16600ad_1654593633874_525666_43081
 * @generated from: _19_0_3_16600ad_1654593633874_525666_43082
 * @generated from: _19_0_3_16600ad_1654593633874_525666_43083
     */
    Integer myPort_myItf;

Example: Flow Property variable

Interface

A UML interface is represented by a C++ class with pure virtual operations. As a rationale, only a header file is generated.

A file named <interface name>.h is created, except for C++ standard Library headers.

Classifier Behavior

CLASS MEMBERS DECLARATION

A variable typed with the structure of the behavior is generated for the Classifier behavior with public visibility.

See [4] for more information on the structure naming. All structures are named through the same pattern.

A variable having the name of the behavior is generated for the Classifier behavior with public visibility.

public:
    /**
     * Data for Behavior Statemachine
     * @generated from: _19_0_3_16600ad_1654593633874_525666_43081
     */
    Statemachine_struct Statemachine;

Example: Classifier behavior variable

DataType & Value Type & Enumeration

DataTypes generation in class are not yet supported.

A type can be a redefinition of an existing type by using a Generalization relation.

Redefinition of Real, Boolean, Integer value_types

Data Types and Value Types are used to type the data contained in classes.

DATA TYPES DECLARATION

Each Data Type or Value Type that is referenced by a Property, Parameter, or Operation argument are expected to be defined in <ClassName>_internal_type.h

Primitive Type

Data types defined in C++ ANSI profile are supported. No typedef needs to be generated.

Data type defined in C++ ANSI Profile

Two additional ANSI types are also provided: ‘short’ and ‘unsigned short’.

Data types defined in UML Standard Profile/MagicDraw profile/datatypes are supported. typedef is generated or not according to following requirements.

Supported MagicDraw Data types

Datatypes defined in “UML Standard Profile/MagicDraw profile/datatypes” “Color”, “StructuredExpression”, “date” and “XML” are not supported.

Data types defined in “UML Standard Profile/UML 2 Metamodel/Primitive Types” are supported. typedef is generated according to following requirements.

Supported UML2 Data types

Datatype defined in “UML Standard Profile/UML 2 Metamodel/Primitive Types” “UnlimitedNatural” is not supported.*

Not supported UML2 Data types

Data types defined in “SysML/Libraries/PrimitiveValueTypes” are supported. typedef is generated according to the following requirements.

Supported UML2 Data types

Datatypes defined in “SysML/Libraries/PrimitiveValueTypes” “Complex” and “Number” are not supported.

A Data Type or a Value Type having one of the following names are not defined:

  • char,
  • bool,
  • int,
  • double,
  • float,
  • long,
  • long double,
  • long int,
  • short int,
  • signed char,
  • unsigned char,
  • unsigned int,
  • unsigned long int,
  • unsigned short int,
  • wchar_t.

Primitive types provided by <stdint.h> or <csdtint> are supported:

  • int8_t, int16_t, int32_t, int64_t,

  • uint8_t, uint16_t, uint32_t, uint64_t,

  • int_least8_t, int_least16_t, int_least32_t, int_least64_t,

  • uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t,

  • int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t,

  • uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t,

  • intptr_t, uintptr_t

Primitive types provided by <math.h> or <cmath> are supported:

  • float_t,double_t

If an Integer type of the following profiles is referenced directly or indirectly in the design, an Integer type will be generated:

An integer is defined as an int.

#ifndef Integer_DEFINITION
#define Integer_DEFINITION
typedef int Integer;
#endif

Example: Integer type declaration

If a Boolean type of the following profiles is referenced directly or indirectly in the design, a Boolean type is generated:

A Boolean is defined as a bool.

#ifndef Boolean_DEFINITION
#define Boolean_DEFINITION
typedef bool Boolean;
#endif

Example: Boolean type declaration

If a Real type of the following profiles is referenced directly or indirectly in the design, a Real type is generated:

Real is defined as a double.

#ifndef Real_DEFINITION
#define Real_DEFINITION
typedef double Real;
#endif

Example: Real type declaration

If a String type of the following profiles is referenced directly or indirectly in the design, a String type is generated:

A String is defined as a char*.

#ifndef String_DEFINITION
#define String_DEFINITION
typedef char* String;
#endif

Example: String type declaration

If a primitive type uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the “compiler” parameter is set to “gcc”, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}).

alignas({alignment}) typedef int alignedInteger;

Example: Primitive type declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).
Array Type
data1 is defined as an array

An array is generated when a property, a parameter or a type references another type with a multiplicity greater than 1.

An array is defined with a naming convention as follows: Array_<size>_<referencedType>.

/**********************************************************************
 * Data Type Array_3_Integer definition
 **********************************************************************/
#ifndef Array_3_Integer_DEFINITION
#define Array_3_Integer_DEFINITION
typedef Integer Array_3_Integer[3];
#endif /* Array_3_Integer */

Example: Array definition in model

If an array type uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the “compiler” parameter is set to “gcc”, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}).

alignas({alignment}) typedef int Array_3_Integer[3];

Example: Array type declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).
Structure Type
Structure definition in model

A struct is defined for a value type or a data type containing one or multiple properties.

/**********************************************************************
 * Data Type Struct definition
 **********************************************************************/
#ifndef Struct_DEFINITION
#define Struct_DEFINITION
typedef struct {
    unsigned int data1;
    double data2;
    bool data3;
} Struct;
#endif /* Struct */

Example: structure definition

If the structure type uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set on “gcc”, then the generated code shall have the keyword __attribute__((packed)).

typedef struct __attribute__((packed)) {
    int attribute1;
    int attribute2;
} StructType;

Example: Structure declaration with packed

If the property of the structure type uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set on “gcc”, then the generated code is to have keyword the __attribute__((packed)), except for bitfield, pointer, and reference properties.

typedef struct {
    int attribute1;
    struct OtherStrucType __attribute__((packed)) attribute2;
} StructType;

Example: Property structure declaration with packed

If the structure type uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}).

typedef struct alignas({alignment}) {
    int attribute1;
    int attribute2;
} StructType;

Example: Structure declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).

If the property of the structure type uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}) except for bitfield and reference properties.

typedef struct {
    int attribute1;
    alignas({alignment}) struct OtherStrucType attribute2;
    alignas(16) int *attribute3;
} StructType;

Example: Property structure declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).
Enumeration Type

An enum is identified as an Enumeration in the design.

Enumeration definition in model

An enum is defined for an enumeration.

/**********************************************************************
 * Data Type ControlEnum definition
 * @generated_from _2022x_16600ad_1673975868491_889391_20271
 **********************************************************************/
#ifndef ControlEnum_DEFINITION
#define ControlEnum_DEFINITION
enum ControlEnum {
    Idle,
    Open,
    Close
};
#endif /* ControlEnum */

If the enumeration uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set on “gcc”, then the generated code shall have the keyword __attribute__((packed)).

enum __attribute__((packed)) EnumPacked{
    literal1,
    literal2
};

Example: Enumeration declaration with packed

If an enumeration literal has the attribute “value” of C++ANSI Profile defined, then the value of that attribute is used in the generated code. The “value” attribute must be a signed integer.

enum EnumPacked{
    literal1 = 1,
    literal2
};

Example: Enumeration declaration with attribute “value” defined

If an enumeration literal has a specification of kind ‘Literal Integer’ defined, then the value of that specification is used in the generated code.

If an enumeration literal has both a specification and the attribute “value” of C++ANSI Profile defined, then the “value” is used in the generated code instead of the specification.

If neither the specification nor the “value” attribute of the C++ ANSI Profile is defined for an enumeration literal, then the name of the enumeration literal is used in the generated code.

If the enumeration uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}).

enum alignas({alignment}) MyEnumeration {
    literal1,
    literal2
};

Example: Enumeration declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).
Union Type

If a union uses the stereotype “C++Packed”, and the attribute “packed” is set to true, and the “compiler” parameter is set to “gcc”, then the generated code is to have the keyword __attribute__((packed)).

typedef union __attribute__((packed)) MyUnion {
    int i;
    double d;
} MyUnionType;

Example: Union declaration using packed

If a union uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}).

typedef alignas({alignment}) union MyUnion {
    int i;
    double d;
};

Example: Union declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).

Signal

DATA TYPES DECLARATION

Each Signal that is received or produced by any behavior is expected to be defined in <ClassName>_internal_type.h

If a component contains one or multiple signals, a trigger_type is generated.

/**********************************************************************
 * Data Type trigger_type definition
 **********************************************************************/
#ifndef trigger_type_DEFINITION
#define trigger_type_DEFINITION
typedef bool trigger_type;
#endif /* trigger_type */

If a signal references one or multiple properties, a dedicated structure is generated for the data transmitted with the signal.

/**********************************************************************
 * Data Type Signal definition
 **********************************************************************/
#ifndef Signal_DEFINITION
#define Signal_DEFINITION
typedef struct {
    Integer s1;
    Integer s2;
} Signal;
#endif /* Signal */

Example: Signal content declaration

A signal type is declared through a structure named SignalEvent_<signal_nme>l_type. This structure contains at least one trigger field with type trigger_type to store the event activation.

If a signal references one or multiple properties, an addition field named <signal_name> is added to the structure.

/**********************************************************************
 * Data Type SignalEvent_Signal_type definition
 **********************************************************************/
#ifndef SignalEvent_Signal_type_DEFINITION
#define SignalEvent_Signal_type_DEFINITION
typedef struct {
    trigger_type trigger;
    Signal data;
} SignalEvent_Signal_type;
#endif /* SignalEvent_Signal_type */

If the signal uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set on “gcc”, then the generated code is to have the keyword __attribute__((packed)).

typedef struct __attribute__((packed)) {
    trigger_type trigger;
    Signal data;
} SignalEvent_Signal_type;

Example: Signal declaration with packed

If the property of the signal uses the stereotype “C++Packed” and the attribute “packed” is set to true and the “compiler” parameter is set on “gcc”, then the generated code is to have the keyword __attribute__((packed)), except for bitfield, pointer and reference properties.

typedef struct {
    trigger_type trigger;
    Signal __attribute__((packed)) data;
} SignalEvent_Signal_type;

Example: Property signal declaration with packed

If the signal uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}).

typedef struct alignas({alignment}) {
    trigger_type trigger;
    Signal data;
} SignalEvent_Signal_type;

Example: Signal declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).

If the property of the signal uses the stereotype “C++Aligned”, and the selected C++ version is greater than or equal to C++11, and the attribute “alignment” is set to a valid value, then the generated code is to include the keyword alignas({alignment}) except for bitfield and reference properties.

typedef struct {
    trigger_type trigger;
    alignas({alignment}) Signal data;
} SignalEvent_Signal_type;

Example: Property signal declaration using alignas

A valid value for “alignment” is either:

  • an integer that is a power of two, or
  • the name of an existing type (for example, an existing class or a standard C++ type such as int).