Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

Rose will also generate code for the operation in the implementation file. We just examined the header file for the Flight class, so now let's take a look at the implementation file for this class.

#include "c:/Flight.h"

//##ModelId=3954129803B3

boolean Flight::AddPassenger(int PassengerID)

{

}

//##ModelId=395412A00093

boolean Flight::RemovePassenger(int PassengerID)

{

}

//##ModelId=395412A80121 int Flight::CancelFlight()

{

}

As you can see, Rose includes each operation in the implementation file. Developers now need to go into the implementation file and code each operation between the opening and closing braces.

Visual C++ Code Generation

For the most part, the code generated in Visual C++ is the same as that generated in ANSI C++, and follows the rules described in the "Generated Code" section earlier in this chapter. Each class in the model becomes a class in the Visual C++ project. Attributes become data members in Visual C++, and the data type, default value, access type, and other code−generation settings from Rose are directly translated into Visual C++. Operations create member functions in Visual C++. The code−generation add−in examines the operation visibility, parameters, parameter data types, return type, and other code−generation properties to create the member function. Attributes are created for relationships, as described in the "Generated Code" section.

The differences between ANSI C++ and Visual C++ code generation lie in the components. Each component in the Rose model will become either a Visual C++ project or an IDL file within a project. Classes are assigned to components during the code−generation process. Interfaces are generated in the IDL files, and are created using the process described earlier in the "Visual C++ and ATL Objects" section.

Reverse Engineering ANSI C++

To reverse engineer in ANSI C++, you must first select a component. All or some of the classes assigned to that component can then be reverse engineered.

To reverse engineer one or more classes that are not in the Rose model at all:

1.

Create a new component in the Component view.

2.

443

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

Open the ANSI C++ specification window for the new component.

3.

In the Project Files area, click Add Files to browse the source file(s) you want to reverse engineer.

4.

Click OK to close the ANSI C++ specification window.

5.

Right−click the component and select ANSI C++ → Reverse Engineer.

6.

Select the class(es) to reverse engineer and press OK.

7.

The classes will be brought into the Rose model and added to the Logical view, under a package named C++ Reverse Engineered.

Note To reverse engineer classes into a package other than C++ Reverse Engineered, open the C++ specification window for the component and change the package name in the Reverse Engineering Root Package field.

To reverse engineer a class that already exists in your model, right−click its component and select ANSI C++ → Reverse Engineer. Select the class(es) to reverse engineer and press OK. Alternately, you can right−click the class you want to reverse engineer and select ANSI C++ → Reverse Engineer.

When you reverse engineer a class, all of its attributes, operations, and relationships are reverse engineered as well. The C++−to−Rose mapping we've discussed in the past several sections controls how the C++ code is represented in the Rose model.

444

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

Reverse Engineering Visual C++

You can reverse engineer a Visual C++ project into an existing component or into a new component in Rose. To begin either process, select Tools → Visual C++ → Update Model from Code. Rose will display a list of existing components in your Rose model.

To reverse engineer the project into a new component, right−click Visual C++ and select Add Component. Select the Visual C++ project to reverse engineer into the new component.

To reverse engineer the project into an existing component, right−click the appropriate component, select Properties, and select the element(s) in the project that you wish to reverse engineer.

Click Next to show a summary, and then Finish to complete the reverse−engineering process. The elements in the project will be mapped to Rose elements as we've discussed in this chapter.

Summary

In this chapter, we took a look at how various Rose model elements are implemented in C++. Using the code−generation properties for classes, packages, attributes, operations, roles, and other model elements, you have a great deal of control over what gets generated.

Again, the steps needed to generate ANSI C++ code are:

1.

Create components.

2.

Assign classes to components.

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++ → Class Customization.

6.

Select Tools → ANSI C++ → Code Generation.

7.

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

The steps to generate Visual C++ code are:

1.

Create component(s) corresponding to the project(s).

2.

445

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

Select Tools → Visual C++ → Model Assistant.

3.

Select Tools → Visual C++ → Update Code.

4.

Select the component(s) to update.

5.

Set code−generation properties.

6.

Select Finish to complete the process.

The steps needed to reverse engineer ANSI C++ code are:

1.

Create a new component.

2.

Use the ANSI C++ specification window to select the file(s) to reverse engineer.

3.

Right−click the component and select ANSI C++ → Reverse Engineer.

4.

Select the class(es) to reverse engineer and press OK.

The steps needed to reverse engineer Visual C++ code are:

1.

Select Tools → Visual C++ → Update Model from Code.

2.

Select an existing component to reverse engineer, or create a new component.

3.

Select the project element(s) to reverse engineer.

In the next chapter, we'll examine the Java add−in for Rose. Using this powerful feature, you can model Java classes, interfaces, attributes, operations, relationships, JAR files, and WAR files. Through the new support for J2EE, you can now also model EJBs, servlets, and other constructs. The enhanced functionality in Rose 2001, 2001A, and 2002 provide the ability to create a complete model of your Java application. The forward− and reverse−engineering features provide you with the ability to keep the code and the object model consistent.

446