How to Generate C++ from UML (Round-Trip)?
Round-trip engineering is the ability to generate model from source code and generate source code from model and keep them synchronized. You can make use of round-trip engineering to keep your implementation model and source code up-to-date, so as to produce up-to-date description on your model.
Generating/Updating code from whole project
You can generate C++ code from all classes in current project. To generate code from project:
- Select Tools > Code > Generate Java Code... from the toolbar. .
- Select C++ as the Language.
- In the Generate Code dialog box, specify the mapping between model and source path. Model is a UML element that acts as a container of other elements. Classes and packages under a model will be generated to the mapped source path. You can add multiple model-to-source-path mapping by pressing the + button. If you are not using model to structure your project or if you want to generate all classes in project to the same folder, regardless of their parent model, keep model to be <root>.
The mappings between models and source paths are defined - Optionally configure the advanced code generation options by clicking Advanced Options.... Read the section Advanced Options in this chapter for details about the options.
- Click OK to proceed with generation.
NOTE: | Description in model elements is generated as comment in code |
Generating/Updating code from opening class diagram
You can generate C++ code from an opening class diagram that contains the class(es) you want to generate code. To generate code from class diagram:
- Right click on the class diagram background and select Utilities > C++ Round-trip > Generate Code from the popup menu.
- In the Generate Code dialog box, specify the source path where you want the code to be generated. Model is a UML element that acts as a container of other elements. Notice that source path is set for model, not for diagram. Classes and packages under a model will be generated to the mapped source path. You can add multiple model-to-source-path mapping by pressing the + button. If you are not using model to structure your project, or if you want to generate all classes in project to the same folder, regardless of their parent model, keep model to be <root>.
The mappings between models and source paths are defined NOTE: If you have generated code for once, the Generate Code dialog box will not appear next time when you generate/update code,
for any diagram. If you want to configure the model-to-source-path mapping or to configure options,
you can run a code generation for project (refer to the previous section for detail). - Optionally configure the advanced code generation options by clicking Advanced Options.... Read the section Advanced Options in this chapter for details about the options.
- Click OK to proceed with generation.
NOTE: | Description in model elements is generated as comment in code |
Generating/Updating code from chosen classes
You can generate C++ code from specific class or classes. To generate code from class/classes:
- Select the class(es) and right click on them, then select C++ Round-trip > Generate Code from the popup menu.
- In the Generate Code dialog box, specify the source path where you want the code to be generated. Model is a UML element that acts as a container of other elements. Notice that source path is set for model, not for diagram. Classes and packages under a model will be generated to the mapped source path. You can add multiple model-to-source-path mapping by pressing the + button. If you are not using model to structure your project, or if you want to generate all classes in project to the same folder, regardless of their parent model, keep model to be <root>.
The mappings between models and source paths are defined NOTE: If you have generated code for once, the Generate Code dialog box will not appear next time when you generate/update code,
for any class selection. If you want to configure the model-to-source-path mapping or to configure options,
you can run a code generation for project (refer to the previous section for detail). - Optionally configure the advanced code generation options by clicking Advanced Options.... Read the section Advanced Options in this chapter for details about the options.
- Click OK to proceed with generation.
NOTE: | Description in model elements is generated as comment in code |
An overview of Generate Code dialog box
An overview of Generate Code dialog box |
|
|||||||||||||||||||||||||||||||||
A description of Generate Code dialog box |
Advanced options
You can configure the advanced options for more control of the code by clicking the Advanced Options... button in Generate Code dialog box. In the Code Synchronization dialog box popped up, there are four categories (tabs) of settings you can configure. Below is a description.
Code
Code configuration |
|
||||||||||
A description of code configuration |
Brace and Indentation
Brace and indentation configuration |
|
||||||||||||||
A description of brace and indentation configuration |
New Lines
New lines configuration |
|
||||||||||
A description of new lines configuration |
Template
Template configuration |
|
||||||||
A description of template configuration |
To ignore classes in generation
You can make certain UML class not to generate code against code generation by ignoring them. To ignore class(es), click Ignore Classes... in Generate Code dialog box. In the second Generate Code dialog box that popped up, select the class(es) to ignore and click > to move them to the ignore list. Click OK to confirm.
The class IgnoreMe is ignored |
Related Resources
The following resources may help you to learn more about the topic discussed in this page.
Chapter 4. C++ Round-trip | Table of Contents | 2. Generate or update UML classes from C++ code |