Skip to content

Attributes

Attributes are a vital part of every class in UML2. For the most part, the translation to C-code is straightforward. For example, type respectively name will be equivalent in the generated code and the model.

However, there are a few specialities to enable features of C which are not part of UML2:

  • If you specify the stereotype extern, the attribute will be extern (also available in the Toolbox).
  • Static
    • static attributes will be generated as top level variables.
    • private static attributes will be hidden from other compilation units
    • public static attributes will be available by including the .h file of the class.
  • private/public affects only top level attributes (for example attributes of static classes, see static classes for more information).
  • For non-static attributes, the attribute will be set to the initial value when calling the constructor-method.
  • For static attributes, the attribute will be declared and defined at once.
  • readonly attributes can be created with the readonly stereotype.

Defines

Whenever you decorate an attribute with the define stereotype, a #define will be generated for it in the code.

There are two types of defines:

Type of define Generated code
define without DefaultValue #define propertyName
define with DefaultValue #define propertyName propertyValue

The defines will be put in the declaration of the class (the .h file).

By setting the Model Setting Consider define scope to Yes the code generation will consider if the define is private or public and generate the define accordingly (private = .c file, public = .h file)

Typedefs

Whenever you decorate an attribute with the typedef stereotype, a typedef will be generated for it in the code. Specify the value in the typedef in the DefaultValue field. For example, if you want to define 'bool' to be an 'int', add a typedef with the name bool and the default value int.

Typedefs Typedefs

The typedefs will be put in the declaration of the class (the .h file).

Tip

Since you will most likely want to use the type you defined with the typedef stereotype we reccomend to use Primitives for generating typedefs.

Readonly

Whenever you decorate an attribute with the readonly stereotype, a readonly attribute will be generated. In Enterprise Architect versions < 12, you can also use the readonly checkbox in the attribute settings. In C, this means that the attribute will be generated as const.

Multiplicity

If the Multiplicity of an Attribute is set > 1 (Lowerbound or Upperbound) the variable or define generated for this will be an array.

You can generate an array of unknown bound by setting the Upperbound multiplicity to *.

Multiplicity

Custom Array

You can use the Custom Array Stereotype together with the ArrayValue Tagged Value to define custom array expressions. This can for example be used to create multi-dimensional arrays.

Example

[1][2][3][a]

int attr1[1][2][3][a]