Skip to main content

Tips and tricks for the C++ Transformation using Rational Systems Developer or Rational Software Architect, Part 2: Controlling the C++ code generated from UML Models in Rational Systems Developer and Rational Software Architect

Vinod V. Uddaraju (vinod.varma@in.ibm.com), Staff Software Engineer, IBM Japan
Vinod Varma has been a developer working on IBM Rational modeling tools for the past 3 years. He has worked on various modeling tools like Rational Rose, Rational Software Architect, and Rational System Developer.
Pratima Gangalavoi (pratima.g@in.ibm.com), Systems Software Engineer, IBM Japan
Pratima Gangalavoi is a Systems Software Engineer working with Rational Software Architect and Rational Systems Developer at the IBM Rational Software Bangalore lab. She works on the C++ transform for those tools.

Summary:  This article, Part 2 of a series (see Part 1 here), provides tips to help you achieve finer control of the C++ code generated when you run the UML to C++ transformation in IBM® Rational® Software Architect or IBM® Rational® Systems Developer. In addition, it describes how to generate virtual, inline, and friend functions from a UML model. It also describes how to control code generation for template classes, and provides detail on various other aspects of code generation using the UML to C++ transformation.

View more content in this series

Date:  29 Jan 2008
Level:  Intermediate
Activity:  449 views

Generating C++ virtual functions

This section tells you how to create C++ virtual functions from a UML model. To generate a C++ pure virtual function in a class, you need to create a UML operation within a UML class, and ensure that the Abstract Qualifier in the General Properties of the operation is selected.

For example, to create a pure virtual function such as virtual int purevirtualop(int x)=0, perform these steps:

  1. Create a UML operation named purevirtualop with a return parameter of type int and a parameter x of type int.
  2. Select the Abstract and Unique Qualifiers in the Properties tab of the operation, as shown in Figure 1. The code generated for the pure virtual function is shown in Listing 1.


Figure 1. Setting the properties for a pure virtual function
screen capture

Listing 1. Generated code for a pure virtual function in header file

//@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
    public:

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        virtual int purevirtualop(int x)=0;

};  //end class Class1  

To generate a C++ virtual function in a class, you need to create a UML operation within a UML Class and apply the cpp_operation stereotype on the UML operation. This stereotype has three property/value pairs:

  • isFriend
  • isInline
  • isVirtual

The isVirtual property needs to bet set to True. For example, to create a virtual function such as virtual int virtualop(int x), perform the following steps:

  1. Create a UML operation virtualop with a return parameter of type int and a parameter x of type int. Apply the cpp_operation stereotype to it.
  2. Set the properties of the stereotype as follows (shown in Figure 2):
    • isFriend to False
    • isInline to False
    • isVirtual to True
  1. Run the UML to C++ transformation. The code generated for the virtual function in a header file is shown in Listing 2.


Figure 2. Setting the properties for a virtual function
screen capture

Listing 2. Generated code for a virtual function in header file

//@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
    public:

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

};  //end class Class1  


Generating C++ inline functions

This section tells you how to create C++ inline functions from a UML model. To create a C++ inline function in a class, you need to create a UML operation within a UML Class and apply the cpp_operation stereotype on the UML operation. The isInline property of this stereotype needs to be set to True.

For example, to create an inline function such as inline int inlineop(int x), perform these steps:

  1. Create a UML operation named inlineop with a return parameter of type int and a parameter x of type int.
  2. Apply the cpp_operation stereotype to it.
  3. Set the properties of the stereotype as follows (shown in Figure 3).
    • isFriend to False
    • isInline to True
    • isVirtual to False


Figure 3. Setting the properties for an inline function
screen capture

The UML to C++ transformation provides two options for generating code for an inline function. The default option generates code for the inline function in both the header and body files. The header file contains the declaration of the function as shown in Listing 3, and the body file contains the definition of the function as shown in Listing 4.


Listing 3. Generated Code for an inline function in header file

//@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
    public:

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

};  //end class Class1


Listing 4. Generated Code for an inline function in body file
                 
                
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::inlineop(int x) 
{
    //TODO Auto-generated method stub
    return 0;
}

The transformation also provides an option to generate the function definition for an inline function in the header file below the class declaration. To activate this option, perform these steps:

  1. Append the -DINLINE_HEADER=true VM argument to the eclipse.ini file
  2. Next, relaunch Rational System Developer or Rational Software Architect with the -clean option.
  3. Alternatively, you can relaunch Rational System Developer or Rational Software Architect with these command line options: -vmargs -DINLINE_HEADER=true.

The declaration and definition generated for the inline function with this option would all be in the header file, as shown in Listing 5.


Listing 5. Generated Code for an inline function using the DINLINE_HEADER option

//@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
    public:

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

};  //end class Class1

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


Generating C++ friend functions

This section tells you how to generate C++ friend functions from a UML model. To generate a C++ friend function that is a friend of a class, you need to create a UML operation within a UML Class and apply the cpp_operation stereotype on the UML operation. The isFriend property of this stereotype needs to be set to True.

To create a friend function such as friend int Class2::friendop (int x), where Class2 is the class in which the function friendop is defined, perform these steps:

  1. Create a Class2::friendop UML operation with a return parameter of type int and a parameter x of type int.
  2. Apply the cpp_operation stereotype on it.
  3. Set the properties of the stereotype as follows (shown below in Figure 4):
    • isFriend to True
    • isInline to False
    • isVirtual to False

The code generated is shown in Listing 6.


Figure 4. Setting the properties for a friend function
screen capture

Listing 6. Generated Code for a friend function

//@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
    public:

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        friend int Class2::friendop(int x);

};  //end class Class1


Modeling a Template Class

This section discusses modeling a C++ template class in a UML model. To model a C++ template class, you need to create a UML Class and then add template parameters of type Class (or any of the UML primitive types) to this UML Class.

To create a template class such as template class one<class T, int X> {int op(){}};, create a UML class and then add the following two template parameters and one UML operation:

  1. A template parameter of type Class (named T) as shown in Figure 5, by right-clicking source > Models > one and selecting Add UML > Template Parameter > Class.
  2. A template parameter of type Integer, named X, as shown in Figure 6, by right-clicking source > Models > one and selecting Add UML > Template Parameter > Integer.
  3. A UML operation op with, an int as the return type.


Figure 5. Creating a template parameter of type Class
screen capture

Figure 6. Creating a template parameter of type integer
screen capture

The generated code for the template class one has both the declaration and definition for the operation op within the template class declaration, as shown in Listing 7. This is the product behavior in Version 7.0.0.3 and later.


Listing 7. Generated Code for a template class
                 
                
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
template <class T, int X>
class one
{

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


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

};  //end class one

The transformation also provides an option to generate the keyword in place of class for template parameters of type class.

  1. To activate this option, you need to append the VM argument -DTYPENAME_TEMPLATES=true in the eclipse.ini file
  2. Next, relaunch Rational System Developer or Rational Software Architect with the -clean option.
  3. Another option is that you could relaunch Rational System Developer or Rational Software Architect with the command line option -vmargs -DTYPENAME_TEMPLATES=true. With this option, the keyword typename is generated for every template parameter of type class. The code generated with this option is shown in Listing 8.

Listing 8. Generated Code for a template class with DTYPENAME_TEMPLATES option
                 
                
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
template <typename T, int X>
class one
{

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


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

};  //end class one


Creating different extensions for header and body Files

This section deals with creating different file extensions for the generated header and source files corresponding to a UML class. To create different file extensions for the header and source files corresponding to a UML class, apply the cpp_properties stereotype on the UML class. This stereotype has two properties

  • headerFileExtension: This property is used for setting the extension of the header file.
  • bodyFileExtension: This property is used for setting the extension of the body file.

To generate the code for a UML class in a header file with a .inc extension, and a source file with a .in extension, perform these steps:

  1. Create a UML Class class1 and add an operation op with return type int to it.
  2. Apply the cpp_properties stereotype on the UML class and set the headerFileExtension property to .inc and the bodyFileExtension property to .in, as shown in Figure 7.


Figure 7. Setting the properties for different header and body file extensions
screen capture

The generated code will have the files class1.inc and class1.in. These file extensions (.inc and .in) need to be associated to C++ header and source files, so that the C/C++ development toolkit recognizes .inc and .in as valid C++ file extensions. To do this, perform these steps:

  1. Go to Window > Preferences > General > Editors > File Associations.
  2. Click the Content Types link at the top of the page.
  3. Navigate to Text > C Source File > C++ Source File > C++ Header File.
  4. Click Add, and the dialog shown in Figure 8 displays.
  5. In this dialog, specify the extension type as *.inc and click OK.


Figure 8. Setting the extension for the header file
screen capture

  1. Scroll down the file associations section to verify that *.inc has been included as a file association for a C++ header file, as shown in Figure 9.
  2. Click OK. At this point, any file having a .inc extension in a C++ project is considered by the C/C++ development toolkit to be a C++ header file.


Figure 9. The file associations for C++ header file
screen capture

  1. Navigate to Text > C Source File > C++ Source File.
  2. Click Add, and the dialog shown in Figure 10 displays.
  3. In this dialog, specify the extension type as *.in and click OK.


Figure 10. Setting the extension for the source file
screen capture

  1. Scroll down the file associations section to verify that *.in has been included as a file association for a C++ source file, as shown in Figure 11.
  2. Click OK. At this point, any file having a .in extension in a C++ project is considered by the C/C++ development toolkit to be a C++ Source file.


Figure 11. The file associations for C++ source file
screen capture


Controlling the visibility order within a class

The default option for generating the code is to sort by visibility order. The sort by visibility order dumps the code generated by the transform according to its visibility. By default, the order is private, protected, and public. This means that all the private members of the class element are dumped first, followed by protected and public, respectively. To view the generated code, create a UML model and then add a UML class A. To this class add the following elements:

  • A UML property a of type int and a UML operation abc with return type void. Set the visibility of these two elements to private.
  • A UML property b of type int and a UML operation xyz with return type int. Set the visibility of these two elements to protected.
  • A UML property c of type int and a UML operation pqr with return type bool. Set the visibility of these two elements to public.

The code generated for this UML model looks like that in Listing 9.


Listing 9. The code generated with the sort by visibility order
                 
                
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class A
{

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

    private:

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

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

    protected:

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

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

    public:

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

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

};  //end class A

The UML To C++ transformation provides an option to generate the code in the order of public elements, followed by protected and private, respectively.

  1. To activate this option, append the -DVISIBILITY_ORDER=true vm argument in the eclipse.ini file.
  2. Next, relaunch Rational System Developer or Rational Software Architect with the -clean option.
  3. Alternatively, you could relaunch Rational System Developer or Rational Software Architect with the -vmargs -DVISIBILITY_ORDER=true command line option. The code generated with this option looks like that in Listing 10.

Listing 10. The code generated with the DVISIBILITY_ORDER option
                 
                
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class A
{

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

    public:

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

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        bool pqr(); 
  
    protected:

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

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

    private:

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

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        void abc();  
 
};  //end class A

The UML To C++ transformation also provides an option to generate the class attributes and operations in the same order as they are stored in the UML model.

  1. To enable this option, navigate to Window > Preferences > Modeling > Transformations > UML to C++ Transformation.
  2. Select the Edit Styles page.
  3. In this page, select the Sort By storage option, as shown in Figure 12. The code generated with this option is shown in Listing 11.


Figure 12. Setting the Sort By Storage option
screen capture



Listing 11. The code generated with the DVISIBILITY_ORDER option

//@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 a;
    protected:

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

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

        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
        void abc();
    protected:

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

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

};  //end class Class1


Using the Exclude Path for Include option

The #include statements that are generated in the code from the UML model have relative paths to the included files. For example, consider a UML model that has a dependency between 2 classes (Class1 and Class2), each nested under a different package (say Package1 and Package2). In this case, the code generated in Class1.h looks like that shown in Listing 12. Note the relative path "../Package2/Class2.h" that is generated to the include file (Class2.h).


Listing 12. Generated Code for #include
                 
                
#include "../Package2/Class2.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

};

The UML To C++ transformation provides an option to generate #include statements that contain only the filenames of the corresponding included files. To activate this option go to the properties page of the UML To C++ transformation configuration wizard and select the Suppress path names in include directives option as shown in Figure 13.

The Suppress path names in include directives option is available in Rational Software Architect and Rational System Developer Version 7.0.5 and later. The code generated with this option (from the UML model mentioned above) is shown in Listing 13. Note that only the filename Class2.h is generated.


Figure 13. Setting the Suppress path names in include directives option
screen capture

Listing 13. Generated Code for #include with the DEXCLUDE_PATH_FOR_INCLUDES option

#include "Class2.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

};


What you have learned and what lies ahead

In this article, you have learned how to do the following:

  • Generate C++ virtual functions
  • Generate C++ inline functions
  • Generate C++ friend functions
  • Model a Template Class
  • Create different extensions for header and body Files
  • Control the visibility order within a class
  • Use the Exclude Path for Include option

In Part 3, you will learn how to construct C++ specific models in UML to generate code accordingly.


Resources

Learn

Get products and technologies

Discuss

About the authors

Vinod Varma has been a developer working on IBM Rational modeling tools for the past 3 years. He has worked on various modeling tools like Rational Rose, Rational Software Architect, and Rational System Developer.

Pratima Gangalavoi is a Systems Software Engineer working with Rational Software Architect and Rational Systems Developer at the IBM Rational Software Bangalore lab. She works on the C++ transform for those tools.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=283914
ArticleTitle=Tips and tricks for the C++ Transformation using Rational Systems Developer or Rational Software Architect, Part 2: Controlling the C++ code generated from UML Models in Rational Systems Developer and Rational Software Architect
publish-date=01292008
author1-email=vinod.varma@in.ibm.com
author1-email-cc=clarkega@us.ibm.com
author2-email=pratima.g@in.ibm.com
author2-email-cc=clarkega@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers