Interpretation of source code by C++-to-UML transformations

The UML elements that the transformation generates depends on the characteristics of the C++ code that you specify as the source of the transformation. When you run the C++-to-UML transformation, it applies the C++ transformation profile to the model that it generates, and also applies the stereotypes of the profile to the UML elements that it generates.

By default, the transformation generates UML elements that have the same name and visibility as the source C++ elements. If the transformation cannot transform a C++ element, it ignores the element. The following table lists the output that the transformation generates when it transforms C++ code elements into UML model elements.

C++ element UML element
Class
  • Class with the same name and visibility as the source C++ class
  • The UML package hierarchy is the same as the source folder hierarchy in the source C++ project
  • If the C++ class extends a superclass, the transformation generates a generalization relationship between the class and the superclass
Enumeration
  • Enumeration with the same name and visibility, in a package with the same name as the source C++ package
  • For an anonymous enumeration, the transformation generates a UML enumeration that has the «cpp_enum» applied, and sets the isAnonymousEnum property of the stereotype to true
Namespace
  • Package with the «cpp_namespace» stereotype applied
  • The NameSpaceName attribute of the stereotype is equal to the value of the C++ namespace
Nested class, struct, or union
  • Nested class, struct, or union
  • The transformation generates nested classes, structs, or unions that have the same characteristics as top-level classes, structs, or unions
Struct
  • Class with the «cpp_struct» stereotype applied, and with the same name and visibility as the source C++ struct
  • The package hierarchy is the same as the source folder hierarchy in the source C++ project
  • If the C++ struct extends a superclass, the transformation generates a generalization relationship between the class and the superclass
Typedef
  • Class with the «cpp_typedef» applied; the ImplementationType attribute of the stereotype is equal to the value of the typedef specifier
  • For example, the transformation transforms the typedef declaration typedef int MyInt into a class with the «cpp_typedef» applied, and the transformation sets the ImplementationType attribute of the stereotype to int
Union
  • Class with the «cpp_union» stereotype applied, and with the same name and visibility as the source C++ union
  • The package hierarchy is the same as the source folder hierarchy in the source C++ project.
  • If the C++ union extends a superclass, the transformation generates a generalization relationship between the class and the superclass

Global variables and global functions

The transformation transforms global variables into attributes that have the following characteristics: .
  • The «cpp_type» stereotype is applied
  • The isGlobal property is set to true
  • The UML class that contains the generated attribute has the same name as the C++ source file that contains the global variable
The transformation transforms global functions into operations that have the following characteristics:
  • The «cpp_operation» stereotype is applied
  • The isGlobal property is set to true
  • The UML class that contains the generated operation has the same name as the C++ source file that contains the global function

Preprocessor directives

The transformation does not transform preprocessor directives in the C++ code because these C++ elements cannot be represented in UML. You must specify user-defined preprocessor directives in the user-defined section in the C++ source files.

C++ template classes

If the source C/C++ Development Tools (CDT) project includes a template instantiation that instantiates a class that does not exist in the source CDT project, the transformation transforms the template instantiation into a UML primitive type. The transformation transforms the references to the template instantiation into the appropriate reference to the UML primitive type in the target model.

The following table lists how the C++-to-UML transformation transforms template classes when the template instantiation instantiates a class in the source project.
C++ template class UML element
Template class definition
  • Parameterized class, if the template parameter is of type Class
  • The transformation ignores all other template parameters, and also ignores partial template classes
  • The following example demonstrates how the transformation transforms a template class definition into a UML parameterized class:
    template <class T=""> class stack {
       public:
    	void push(T ob);
    	T pop();
    };
  • The transformation generates the following UML parameterized class:
    A UML parameterized class named stack, which has an operation named push and an operation named pop.
Template class instantiation in a typedef definition
  • Class with the same name as the typedef declaration
  • The transformation generates a binding relationship that points from the template class instantiation to the UML parameterized class
  • For example, the transformation transforms the typedef declaration typedef stack<A> myStackA; into the following UML class called myStackA, and creates a binding relationship to the stack class:
    A class diagram with a binding relationship between the template class instantiation and the UML parameterized class.
Template class instantiation not in a typedef definition
  • Class in the template instantiation folder of the target UML model
  • The transformation generates a binding relationship that points from the template class instantiation to the UML parameterized class.
  • The UML class that the transformation generates in the template instantiation folder has the naming scheme shown in the following diagram, and corresponds to the template instantiation in the source C++ file.
  • The following example demonstrates how the transformation transforms a template class instantiation that is not in a typedef definition:
    class B {
    	stack<A> stackA;
    };
  • The transformation generates the following UML class named stack_A, and generates a binding relationship to the stack class:
    A directed association relationship from class B to stack_A, and the binding relationship between stack_A and stack.

C++ member variables in a class, struct, or union

The transformation transforms a C++ member variable in a class, struct, or union into a UML property that has the following characteristics:
  • The generated property has the same name and visibility as the C++ member variable.
  • The transformation sets the Type property to an existing C++ primitive type if the corresponding type of the C++ member variable is a C++ primitive type. Consider the following code:
    class Class1{
        int x;
    } 
    The transformation creates a mapping between the type of the property x in the UML class named Class1, and the int C++ primitive type. The C++ type library contains the int primitive type.

    The following tables list how the transformation sets the Type property if the type of the C++ member variable is a user-defined type.

If the type of the C++ member variable is a user-defined type, the transformation sets the UML Type property to the value of a UML element that corresponds to the user-defined type in the target UML model. If the transformation cannot determine the UML element in the model that corresponds to the user-defined type, the transformation creates a UML primitive type with the same name as the UML element, and sets the Type of the UML property as equal to the name of the new UML primitive type.

The example in the following table lists how the transformation sets the value of the UML Type property:
C++ code sample Value of the Type property
class Class2{
};

class Class3{
Class2 x1;
};
The transformation sets the Type of the property named x1 in UML class Class3 to Class2, which the transformation creates in the target UML model

The example in the following table lists how the transformation sets the value of the UML Type property when the type of the C++ member variable references a type that is defined in a different CDT project.

C++ code sample Value of the Type property
CDTProject1 project contains a class named Class1.

CDTProject2 project contains a class named Class2:

#include "CDTProject1/Class1"
class Class2{
	Class1 x;
};
The transformation creates a mapping between the type of the UML property x to the visual element that corresponds to the class named Class1 in the C++ source project named CDTProject1

The following table lists additional information that the transformation generates for specific member variables.

C++ member variable Additional characteristics of the transformation output
const isStatic property is true
mutable The transformation applies the «cpp_type» stereotype to the property; the isMutable property of the stereotype is set to true
static isStatic property is true
volatile The transformation applies the «cpp_type» stereotype to the property; the isVolatile property of the stereotype is set to true

C++ member functions in a class, struct, or union

The transformation transforms a C++ member function in a class, struct, or union into a UML operation in the corresponding UML class. The UML operation that the transformation generates has the same name and visibility as the C++ member function. The following table lists the additional information that the transformation generates for specific function parameters.

C++ member function Additional characteristics of the transformation output
Assignment The transformation applies the «cpp_assignment» stereotype; the generated operation has the same name and visibility as the = assignment operator.
Const The transformation sets the isQuery property to true.
Constructor The transformation applies the «cpp_constructor» stereotype. If the constructor methods in the C++ code are overloaded, for each constructor method in the code, the transformation generates a corresponding constructor method in the target model.

If the transformation encounters duplicate constructor methods in the code, the transformation generates a constructor method for the first one that it encounters.

CopyConstructor The transformation applies the «cpp_copy_constructor» stereotype. If the copy constructor methods in the C++ code are overloaded, for each copy constructor method in the code, the transformation generates a corresponding copy constructor method in the target model.

If the transformation encounters duplicate copy constructor methods in the code, the transformation generates a copy constructor method for the first one that it encounters.

Destructor The transformation applies the «cpp_destructor» stereotype. If the destructor methods in the C++ code are overloaded, for each destructor method in the code, the transformation generates a corresponding destructor method in the target model.

If the transformation encounters duplicate destructor methods in the code, the transformation generates a destructor method for the first one that it encounters.

Friend The transformation applies the «cpp_operation» stereotype, and sets the isFriend property to true.
Inline The transformation applies the «cpp_operation» stereotype, and sets the isInline property to true.
Pure virtual The transformation sets the isAbstract property to true.
Static The transformation sets the isStatic property to true.
Virtual The transformation applies the «cpp_operation» stereotype, and sets the isVirtual property to true.

C++ function parameters

The transformation transforms C++ function parameters into UML parameters that have the following characteristics:
  • The generated parameter has the same name as the C++ parameter. If the C++ parameter is a return parameter, the transformation generates a parameter that has the parameter type name.
  • The transformation sets the Type property of the parameter to a primitive type, or to an existing UML element in the model.
  • The transformation sets the default value of the UML parameter as equal to the value of the C++ parameter. Consider the following C++ code:
    class one{
        int op(int  x=5);
    }
    The transformation sets the default value of the generated UML parameter x, in the operation op, to 5.
The following table lists the additional information that the transformation generates for specific function parameters.
C++ function parameter Transformation output
Reference The transformation sets the Direction property to inout
Return The transformation sets the Direction property to return
Regular (not reference or return parameters) The transformation sets the Direction property to in

Type modifiers

C++ member variables, member functions, and parameters might specify type modifiers, such as pointers to types, reference types, and array types. These type modifiers cannot be represented in UML. The following table lists how the transformation transforms type modifiers.

Type modifier Transformation output
Pointer to a C++ type The transformation applies the «cpp_type» stereotype to the UML parameter or property that it generates, and sets the qualifier attribute to a pointer operator. Consider the following C++ code:
class MyClass {
	private: char* name;
};

The transformation generates a UML class named MyClass that contains a name variable with the following properties:

This image shows the qualifier property is set to a pointer operator, which is represented by an asterisk.
Reference to a C++ type The transformation sets the qualifier attribute to a reference operator.

The transformation only applies the «cpp_type» stereotype when it transforms multiple reference types in the source code.

If the transformation transforms a C++ parameter that is a reference type, it sets the direction attribute of the UML parameter to inout.

Consider the following C++ code:

class MyClassClone {
	private:
	MyClassClone clone(MyClassClone& copy);
};

The transformation generates a UML class named MyClassClone that contains a return parameter with the following properties:

This image shows the qualifier property is set to a reference operator, which is represented by an ampersand.
Array

For a single-dimension array, the transformation sets the multiplicity of the UML parameter or property as equal to the value of the single dimension.

The transformation only applies the «cpp_type» stereotype when it transforms multiple dimensional arrays in the source code.

The following example shows how the transformation sets the multiplicity for an attribute of type int x[10]:

This image shows the General page of the Properties view. The multiplicity of the property called x is set to 10.

The transformation sets the lower and upper bound of the multiplicity property to the value of the single dimension of the attribute.


Feedback