Skip to content

Structures

You can add structures and generate code for them by simply using the appropriate stereotype. If the structure is part of another class, the code for the structure will be written with the code for the file. If the structure is not part of a class, a new file will be generated for the structure.

Struct

Then define the members of the structure as attributes

Struct

Unions

To have better support for embedded code generation and support smaller memory footprints we added the union stereotype to the Embedded Engineers repository.

You can add a union to your class and generate code for it.

To do so, add a new class to your model and use the stereotype union on this class. If the union is part of another class, the code for the union will be written with the code for the file. If the union is not part of a class, a new file will be generated for the union.

Embedding

If you do not want to generate a declaration and implementation for each struct, enumeration or union you need inside your code you can also embed structs within other classes.

Embedded Structure

typedef enum NestedEnumPublicEnum {
    a = 0, 
    b,
    c
} NestedEnumPublic;

typedef struct NestedStructPublicStruct
{
    MyType attrA;
} NestedStructPublic;

typedef union NestedUnionUnion {
    int fieldA;
    long fieldB;
} NestedUnion;

typedef int MyType;

typedef struct ClassWithNestedElementsStruct
{
    NestedStructPublic NestedStruct;
} ClassWithNestedElements;
typedef enum NestedEnumPrivateEnum {
    d = 0, 
    e,
    f
} NestedEnumPrivate;

typedef struct NestedStructPrivateStruct
{
    int attrB;
} NestedStructPrivate;

typedef struct NestedStructPrivateStereotypeStruct
{
    int attrC;
} NestedStructPrivateStereotype;

Info

You can modify the tags for generated structs, enums & unions (e.g. NestedStructPublicStruct) by using the element alias combined with the model setting Use Element Alias as Tag Name. An empty alias can be used to create anonymous structs/enums/unions. C++ enums do not generate any tags.

Nesting

Since C and C++ allow for nesting/embedding structs and unions into a single structure we now also support this feature with Embedded Engineer.

To use this feature simply link a union or struct via an Aggregation link to its new "Parent" you may also define a name if needed.

Nested Structure

typedef struct NestedStructsStruct
{
    union
    {
        char Full;
        struct
        {
            byte dd;
            byte mm;
            int yyyy;
        } Date;
        struct
        {
            char B0 : 1;
            char B1 : 2;
        };
    } Example;
} NestedStructs;

Taken from the CodeGenerationCoponentsTest example for MagicDraw or Enterprise Architect package.

Enterprise Architect: EmbeddedEngineerComponents Test --> Components.AnsiC.Structural Elements.Structures Magic Draw: EmbeddedEngineerComponents Test --> Components.AnsiC.Structural Elements.Structures