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 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 defineGenerated 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).

(available since version 2.3)

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.

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

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

(available since version 2.3)

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

  • No labels