Considerations when you rerun UML-to-C++ transformations

The UML-to-C++ transformation adds the @generated tag to certain generated elements. When you rerun a transformation, the transformation overwrites specific elements that have an @generated tag. To protect changes that you make to the generated code, you must remove specific instances of the @generated tag. To represent C++ elements that you cannot model, add code to user-defined sections in the transformation output.

Elements that the transformation generates

The UML-to-C++ transformation adds the @generated tag to the following elements:

Protecting generated code

When you rerun a UML-to-C++ transformation, by default, the transformation does not overwrite method bodies that the transformation generates, or that a user creates. The transformation always preserves method bodies, whether or not you remove the @generated tag.

If you do not remove the @generated tag from the following elements, the transformation overwrites them, whether they are top-level or nested:
  • Classes
  • Structs
  • Unions
  • Typedefs
  • Enumerations

Although the transformation always preserves method bodies, you must remove the @generated tag from a method to preserve structural changes to the method. For example, you must remove the @generated tag for the method if you change the method signature, add initializers to a constructor, add exceptions to methods, or add comments to the source code.

If you do not remove the @generated tag, the transformation overwrites the elements to which the @generated tag applies. The transformation also overwrites the following elements:
  • Mutable, const, volatile, static, auto, and register keywords
  • Members of classes
  • Parameters
Note: To add the method to the source UML model, run the reverse transformation.
The following table lists examples of elements that the transformation preserves and overwrites. The Modified code column lists the code that contains the following changes:
When you rerun the transformation, it does the following things:
  • Preserves the inheritance because the @generated tag was removed
  • Overwrites the changes to the method signature because the @generated tag was not removed
  • Does not preserve the changes to the method body because the transformation cannot locate the method to which the new method body belongs
  • Preserves the changes to the Operation2 method body
UML model element Generated code Modified code Generated code after you rerun the transformation
This image shows a UML class named Class1 that contains an attribute of type integer, and two operations that do not have any parameters. Class1.h:
#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

//@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
Class1.cpp:
#include "Class1.h"
//Begin section for file Class1.cpp
//TODO: Add definitions that you want preserved
//End section for file Class1.cpp


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation1() 
{
    //TODO Auto-generated method stub
    return 0;
}


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation2() 
{
    //TODO Auto-generated method stub
    return 0;
}
Class1.h:
#ifndef CLASS1_H
#define CLASS1_H
//Begin section for file Class1.h
#include Class2.h
//End section for file Class1.h

class Class1:public Class2
{

    //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(int parameter1);

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


};  //end Class Class1
#endif
Class1.cpp:
#include "Class1.h"
//Begin section for file Class1.cpp
//TODO: Add definitions that you want preserved
//End section for file Class1.cpp


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation1(int parameter1) 
{
    int j = 1;
    return j;
}


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation2() 
{
    int i = 2;
    return i;
}
Class1.h:
#ifndef CLASS1_H
#define CLASS1_H
//Begin section for file Class1.h
#include Class2.h
//End section for file Class1.h

class Class1:public Class2
{

    //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
Class1.cpp:
#include "Class1.h"
//Begin section for file Class1.cpp
//TODO: Add definitions that you want preserved
//End section for file Class1.cpp


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation1() 
{
    //TODO Auto-generated method stub
    return 0;
}


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation2() 
{
    int i = 2;
    return I;
}

Protecting new code

The transformation does not overwrite attributes, nested classes, structs, unions, or enumerations that you add to the C++ code unless you accidentally add the @generated tag to these elements.
Note: To add attributes, nested classes, structs, unions, or enumerations to the source UML model, run the reverse transformation.
You can also add code to the user-defined sections that the transformation creates. The transformation does not overwrite or remove code that you specify within a user-defined section. The following table lists where the transformation generates user-defined sections, and provides examples of how to use them.
Transformation output Location and use of the user defined sections
Header files and body files The transformation adds one user-defined section to the beginning of each of these files. You can use this section to specify the following types of statements:
  • #include
  • #defines
  • using
Classes, unions, structs, enumerations You can use these user-defined sections to specify attributes, operations, or both, that you cannot model in UML. For example, you can specify function pointer attributes, such as int myFunction(int* myOtherFunction)attributes in a user-defined section.

Although the @generated tag and user-defined sections offer flexibility, to avoid possible problems, you should make structural changes to the source UML model instead of the generated code. You can also run the reverse transformation to add new methods or attributes to the source UML model.

Example of how to add an attribute to source code

The following table lists these items:
  • UML model element
  • Code that the transformation generates for this model element
  • Code that contains a new attribute named attribute2, added to a header file, but not in a user-defined section
  • Code that contains a new attribute named attribute2, added to a user-defined section
Although this example demonstrates how to add an attribute, you can add operations and nested elements in the same way.
UML model element Generated code Attribute added outside a user-defined section Attribute added to a user-defined section
This image shows a UML class named Class1 that contains an attribute, and an operation that does not have any parameters.
Class1.h:
#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

//@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();

};  //end Class Class1
#endif
Class1.cpp:
#include "Class1.h"
//Begin section for file Class1.cpp
//TODO: Add definitions that you want preserved
//End section for file Class1.cpp


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation1() 
{
    //TODO Auto-generated method stub
    return 0;
}	
Class1.h:
#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

//@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;

	  bool attribute2;
    public:

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

};  //end Class Class1

#endif
Class1.cpp:
#include "Class1.h"
//Begin section for file Class1.cpp
//TODO: Add definitions that you want preserved
//End section for file Class1.cpp


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation1() 
{
    //TODO Auto-generated method stub
    return 0;
}
Class1.h:
#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

//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{

    //Begin section for Class1
        bool attribute2;
    //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();

};  //end Class Class1

#endif
Class1.cpp:
#include "Class1.h"
//Begin section for file Class1.cpp
//TODO: Add definitions that you want preserved
//End section for file Class1.cpp


//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::Operation1() 
{
    //TODO Auto-generated method stub
    return 0;
}

Feedback