UML-to-C++ transformations

This content applies to version 7.5.4 or later. The UML-to-C++ transformation transforms UML model elements into C++ code.

Valid transformation sources

The following table lists the UML source objects that the transformation can convert into C++ code, depending on the design contract management protocol (DCMP) that you specify in the transformation configuration:
DCMP UML source objects
Conceptual Models Seed Concrete Models You can select the following elements from one or more UML projects in the current workspace:
  • Models
  • Packages
  • Non-nested classifiers:
    • Classes
    • Enumerations
    • Interfaces
    • Components
Conceptual Models Drive Development
Reconciled Modeling You can select one or more elements in the preceding list.

If you select multiple packages or non-nested classifiers as the source of the UML-to-C++ transformation, they must be contained in the same UML model.

If you select one or more non-nested classifiers as the source of the UML-to-C++ transformation, the reverse, or C++-to-UML, transformation determines the root-level package of the first non-nested classifier that the reverse transformation processes. The reverse transformation generates its output in this root-level package.

You can specify the model elements that the transformation should convert to C++ code when you create or edit a transformation configuration, or by selecting model elements directly in the model. Selected model elements are the source for a transformation. The transformation generates an error message if you select both a container and its model elements or the same model element twice.

You can use components to organize the elements of a model that you want to convert to C++ code. If you select a component, the UML-to-C++ transformation converts related packages and classes to C++ code when a component has a usage relationship to packages and classes.

Valid names for transformation sources

Names for UML source elements can begin with a lowercase letter, uppercase letter, or an underscore (_). The subsequent characters can be lowercase letters, uppercase letters, or numbers. You can use a colon (:) in an operation name to specify fully qualified names for friend functions. The transformation prefixes a name that begins with a number or an invalid character with an underscore (_). The transformation checks that names in a relevant namespace are unique; if the names are not unique, the transformation assigns the duplicate name a suffix that consists of an underscore and an incremental number; for example, _1.

To improve the readability of the generated code and to minimize the differences between the UML model and generated code, correct the unusable names in the source UML model before you run the transformation.

Valid transformation targets

A valid transformation target is a C++ project. If you create a target container, you can create a standard make C++ project, which uses either a makefile that you create or an existing makefile, or you can create a managed make project, which generates the makefile for you automatically.

When the UML-to-C++ transformation runs, it generates output in the target C++ project, and the output has the same hierarchy as the elements in the source UML model. If you select the Reconciled Modeling DCMP in the transformation configuration, when the C++-to-UML, or reverse, transformation generates UML elements in the same hierarchy as the C++ project.

Customization of generated C++ code

The UML-to-C++ transformation invokes a JET transformation to generate the C++ code. You can create an overriding JET transformation and modify the templates in that transformation to include code elements, such as organization-specific comments, in the generated code. When you create a configuration for the UML-to-C++ transformation, you must specify the identifier of the overriding JET transformation.

Transformation of UML model elements

The following table lists how the UML-to-C++ transformation transforms UML model elements into C++ code.

UML model element C++ code element
Association relationship Class attribute
Class Class (.h and .cpp files)
Dependency relationship Inclusion, forward reference, or none
Note: Settings on the C++ Properties page of the Properties window determine what is generated in the header and implementation files.
Enumeration Enum (.h file)
Generalization relationship Class inheritance
Operation Operation
Package Folder
Parameter Operation argument
Property Class attribute
Template binding relationship Instantiated class (.h file)
Template class Header file (.h file) that has the same name as the UML template class
Note: The method body code is also generated in the header file.
Template parameter Parameter for a parameterized class

For more information about how the transformation transforms UML model elements, see the related reference topic at the end of this topic.

Trace relationships between source model elements and transformation output

To understand how the elements of a source model relate to the elements in a target project, you can have a transformation create trace relationships from the source model elements to the generated elements in the target project. You can then view the trace relationships in a topic diagram by creating and running model queries.

Creating trace relationships is useful in large projects that might contain multiple models and multiple transformation configurations. You can also create and run traceability queries that display how the transformation output relates to software specifications and model elements.

When you run a transformation that is configured to generate trace relationships between the source model and transformation output, for each UML element that supports the @generated tag, the transformation adds a source code comment to the transformation output. The comment begins with //@uml.annotations and contains a unique identifier for the model element. If you rename elements in the source UML model, this unique identifier enables the merge functionality to refactor and merge the appropriate elements. Also, if you rename, move, or delete elements from the source UML model, and these elements have corresponding C++ visual representations, when you rerun the UML-to-C++ transformation, the transformation updates the references to the C++ visual representations.
Note: If you move a UML element out of the scope of its top-level class, when you rerun the transformation, the merge algorithm does not refactor or merge the renamed element because the scope of the element changed.

The transformation does not alter the source model when you specify that the transformation generate trace relationships.

To view the relationships between source model elements and the transformation output, you can visualize the source model element in a diagram and drag the code element that the transformation generates into that diagram. The following table lists the following things: the UML representation for a source model element named Class1, which contains an integer attribute named attribute1, and two operations named Operation1 and Operation2, which do not have any parameters; the code that the transformation generates; and the class diagram that shows the trace relationship between the elements.
UML element Transformation output Derived relationship in a class diagram
This image shows a UML class named Class1 that has an integer attribute and two operations that do not have any input or output parameters.
#ifndef CLASS1_H
#define CLASS1_H
//Begin section for file Class1.h
//TODO: Add definitions that you want preserved
//End section for file Class1.h

//@uml.annotationsderived_abstraction="platform:/resource/MyModels/UMLModel.emx#_biwQUHdCEduVaMk1fL7Qcw"
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1 {

    //Begin section for Class1
    //TODO: Add attributes that you want preserved
    //End section for Class1

    private:

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        int attribute1;
    public:

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        int Operation1();

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        int Operation2();

};  //end Class Class1

#endif
This image shows the abstraction relationship, with the derive stereotype applied, between Class1 and its visual representation.

C++ standard operations

Standard operations are constructors, copy constructors, virtual and non-virtual destructors, assignment operators, and getter and setter methods. You can specify which standard operations the UML-to-C++ transformation generates for the classes, structs, and unions in a UML mode. You can also add a stereotype, which represents one of the standard operations, to the operation of a class in your model. The UML-to-C++ transformation applies the visibility of the UML operation to the generated standard operations, but ignores the name and parameters of the UML operation.

Location of transformation output

The transformation generates C++ code files in the C++ projects and folders that you specify as the target of the transformation.

Integration with team support

The transformation provides integration functionality with IBM® Rational® Team Concert, CVS, Rational ClearCase®, and Rational ClearCase LT version control systems, which enables you to automatically check out files or add new files. You must enable team capabilities to work with configuration management systems.


Feedback