Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

C++ is one of the most widely used object−oriented languages in the industry. Rational Rose supports integration with C++ through its code−generation and reverse−engineering capabilities. In this chapter, we'll discuss how to generate C++ code from your Rational Rose model and how to reverse engineer C++ code into a Rose model.

There are three versions of C++ integration with Rational Rose. First is the C++ add−in that has been available in Rose since October 1994. It contains a great deal of functionality, but can prove difficult to use. The second is an add−in for Microsoft's Visual C++. This add−in is simpler to use, but will integrate only with Microsoft's Visual C++. Rational, therefore, has introduced the third add−in, ANSI C++, which is an easy−to−use C++ code−generation and reverse−engineering tool that will work with C++ tools other than Microsoft's Visual C++.

In this chapter, we will discuss the ANSI C++ and Visual C++ add−ins. The original C++ add−in is still supported, but will not be covered here. We'll discuss the code−generation properties that can be set, and take a close look at how each Rose model element is implemented in the code.

Setting C++ code−generation properties

Mapping Rose elements to C++ constructs

Generating C++ code from your Rose model

Reverse engineering C++ code into your Rose model

Generating Code in ANSI C++ and Visual C++

You will need to follow these steps to generate code in ANSI C++:

1.

 

Create components (see Chapter 10, "Component View").

2.

 

 

Assign classes to components (see Chapter 10).

3.

 

 

Set the code−generation properties.

4.

 

 

Select a class or component to generate on a Class or Component diagram.

5.

Select Tools → ANSI C++ → Generate Code.

 

6.

417

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

Select Tools → ANSI C++ → Browse Header or Browse Body to view the generated code.

The first step in code generation is to create components for the classes. In ANSI C++, the file is located in the class file. The component has only the Root directory. Once components have been created and the classes mapped, the next step is to set the code−generation properties for your classes, components, operations, and other model elements. The code−generation properties control certain aspects of the code that is generated.

If you are generating code in Visual C++, you will use a wizard. To start the wizard, select Tools → Visual C++ → Update Code. The Visual C++ Code Update tool will start, and a welcome screen will be displayed. Click Next to continue. Rose will display the Select Components and Classes window. Before you can generate a class in Visual C++, the class must be assigned to a component. If you have not assigned the class to a component, select the Create a VC++ Component and Assign New Classes to It (Ctrl+R) option in the wizard window. Using this option, you can create as many components as you need before you generate the code. Then select the components and/or classes in your model for which you wish to generate code.

To change the code−generation properties for your Visual C++ components and classes, right−click the VC++ folder on this screen. You can then edit any of the code−generation properties, such as the container class, to support relationship multiplicity, to automatically generate a constructor and destructor, and to automatically generate Get and Set operations or other member functions. The code−generation properties are discussed in greater detail in the upcoming section, "Visual C++ Code−Generation Properties."

Once all classes have been assigned to components, you have selected the classes and/or components you wish to generate, and all code−generation properties have been set, click Next to continue. A summary page will be displayed to let you know which classes or components were generated and which errors were encountered during the code−generation process.

Rose will use a lot of information from the model to generate code. For example, it will look at the multiplicity, role names, containment, and other details of each relationship. It will look at the attributes, operations, visibility, and other details of each class. From all of the information you entered using the specification windows for the various model elements, Rose will gather what it needs to generate code.

Converting a C++ Model to an ANSI C++ Model

Previous versions of Rose contained a C++ add−in, but not an ANSI C++ add−in. As we discussed earlier, the ANSI C++ add−in is a more user−friendly version of the C++ code−generation and reverse−engineering tool. Although you can continue to use the C++ add−in, Rose provides a means of converting an older C++ model to an ANSI C++ model.

First, select Add Ins → Add In Manager, and be sure the ANSI Converter add−in is selected. Select Tools → ANSI C++ → Convert from Classic C++.

You can choose the following options:

Convert selected classes, which will convert any classes that you selected on a Class diagram before starting the conversion wizard

418

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

Convert all classes in selected packages, which will convert all classes within any package(s) that you selected on a Class diagram before starting the conversion wizard

Convert all classes in selected package and its subpackages, which will convert all classes within the package that you selected on a Class diagram, as well as all classes in subpackages of the selected package

Convert Entire Model, which will convert all classes in all packages

Warning This operation will make significant changes to your model, so it's a good idea to make a backup of the model before performing the conversion.

ANSI C++ Code−Generation Properties

C++ code generation using Rational Rose is extremely flexible. You have full control over what gets generated and many of the details of how the generated code will look. For example, for each class, you can decide if a constructor, copy constructor, and destructor will automatically be created. For each attribute, you control the visibility, name, and whether Get and Set operations should automatically be created. For each operation, you control the name, parameters, visibility, and return type.

In ANSI C++, you can manage these types of settings using the code−generation properties and the C++ class and component specification windows. The ANSI C++ add−in includes code−generation properties for classes, attributes, operations, operation parameters, packages, components, associations, and generalizations. You can see all of these properties by selecting Tools → Options, and then selecting the ANSI C++ tab.

Code−generation properties can be set for the entire model or for a specific model element. You can change the default code−generation properties for the entire model by selecting Tools → Options, then selecting the ANSI C++ tab. Code−generation properties can be set for a single class, attribute, operation, or other model element, which will override the default setting. To do so, open the specification window for the model

419

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

element, select the ANSI C++ tab, modify the property value, and press the Override button. In the following sections, we'll examine the code−generation properties for classes, attributes, operations, operation parameters, packages, components, associations, and generalizations.

Class Properties

Class properties are the ANSI C++ code−generation properties that apply specifically to classes. These properties will let you change the class name, decide whether or not the class should be generated or reverse engineered, set the header filename, and set other class−specific properties.

There are two places to set these properties. To set them for all classes, select Tools → Options, then click the ANSI C++ tab and select Class from the drop−down list box. To set them for only one class, select the ANSI C++ tab on the class specification window and edit the properties there.

Using stereotypes, you can control the generation of keywords such as "static" or "virtual." Rose generates the text of the class stereotype in front of the "class" keyword.

Table 13.1 lists the ANSI C++ class properties, their purposes, and their default values.

Table 13.1: Class Code−Generation Properties

Property

Purpose

Default

Synchronize

Controls whether the class will participate in code

True

 

generation and reverse engineering.

 

CodeName

The name of the class in the generated code.

By default, Rose will use

 

 

the class name in the

 

 

model.

ImplementationType

Controls whether a class is generated using a class

<blank> (generates class

 

definition or elemental data type.

definition)

420