Documentation

Customize C++ Encapsulation Interface to Generated Code

Encapsulate a C++ class interface in generated code. Customize C++ class interfaces for referenced models and how models interact through those interfaces.

Configure a Model with Custom C++ Encapsulation Interface

If starting with a new model,

  1. Create a model with at least one inport and at least one outport.

  2. Select Configuration Parameters > Code Generation and set the Programming language to C++.

  3. Select Configuration Parameters > Interface, click on Code interface packaging, and select C++ class.

  4. Click on Configure C++ Class Interface and customize the interface as needed. See Apply Custom C++ Encapulation Interface later in this example. Also see see Control Generation of C++ Class Interfaces in the Embedded Coder documentation.

About C++ Encapsulation Interfaces

A generated C++ class encapsulates model data and methods. Using Embedded Coder®, the encapsulation interface is configurable through model-neutral configuration parameters and model-specific configurations. The interface supports two styles of model step method interfaces (default step method and I/O arguments step method). The I/O arguments style interface can be used to customize the step method prototype, and the Default step method style interface can be used to achieve the maximized supported feature set including virtual bus across model boundaries and multitasking interface.

open_system('rtwdemo_cppclass');

Generate Code from the Example Model

  1. Open the example model rtwdemo_cppclass.

  2. Save a copy of the model to your current working folder.

  3. To generate code, in the model, double-click the blue button. Or, build the model by pressing Ctrl+B. A code generation report is displayed.

  4. Use the code generation report to inspect the source in the generated files using the hyperlinks embedded in the report.

  5. Observe the customized model step and initialization classes ModelClass::step_method and ModelClass::initialize inside the file rtwdemo_cppclass.cpp.

  6. To view the interface configuration, in the model, double-click the yellow button (labeled View Interface Configuration). Or, select Configuration Parameters > Code Generation > Interface and click Configure C++ Class Interface.

  7. Observe the customized arguments for the step function and how these relate to the generated code.

Change to Default Arguments and Rebuild Model

If starting with a new model, you must populate the initial configuration for the C++ class interface.

  1. To populate the initial argument configuration for the model initialize and step functions, click Get Default Configuration.

  2. To close the Model Interface dialog box, click Apply then OK.

  3. To close Configuration Parameters, click OK.

  4. To generate code, in the model, double-click the blue button. Or, build the model by pressing Ctrl+B. A code generation report is displayed.

  5. Use the code generation report to inspect the customized model step and initialization classes ModelClass::step_method and ModelClass::initialize inside the file rtwdemo_cppclass.cpp.

  6. Observe the default arguments for the step function and how these relate to the generated code.

Apply Custom Arguments and Rebuild Model

  1. To view the interface configuration, in the model, double-click the yellow button (labeled View Interface Configuration). Or, select Configuration Parameters > Code Generation > Interface and click Configure C++ Class Interface.

  2. Move Port Name Out1 to top of the Order and set its Category to Value.

  3. To close the Model Interface dialog box, click Apply then OK.

  4. To close Configuration Parameters, click OK.

  5. To generate code, in the model, double-click the blue button. Or, build the model by pressing Ctrl+B. A code generation report is displayed.

  6. Use the code generation report to inspect the customized model step and initialization classes ModelClass::step_method and ModelClass::initialize inside the file rtwdemo_cppclass.cpp.

  7. Observe the custom arguments appear in the generated code.

Compare Default Interface (Maximum Support) with I/O Arguments Interface

The rtwdemo_cppclass model provides buttons to toggle the function specification for the C++ class interface between the default step method (in multitasking mode) and the I/O arguments step method.

  1. To configure the function specification for the default step method, in the rtwdemo_cppclass_refmid model, double-click the yellow button (labeled Use Default Interface in MultiTasking Mode).

  2. To generate code, in the model, double-click the blue button. Or, build the model by pressing Ctrl+B. A code generation report is displayed.

  3. Use the code generation report to inspect the model step and initialization classes, then close the report.

  4. To configure the function specification for the I/O arguments step method, in the rtwdemo_cppclass_refmid model, double-click the yellow button (labeled Use I/O Arguments Interface).

  5. To generate code, in the model, double-click the blue button. Or, build the model by pressing Ctrl+B. A code generation report is displayed.

  6. Use the code generation report to inspect the model step and initialization classes, then close the report.

Apply C++ Class Interface with Referenced Models

The rtwdemo_cppclass_refmid model is one of the referenced models in the example model rtwdemo_cppclass.

This referenced model shows the ability to generate a customizable C++ class interface to code generated by Embedded Coder for a referenced model in the middle of a model reference hierarchy.

  1. Open the rtwdemo_cppclass_refmid referenced model by double-clicking on the Subsystem_with_MdlRef_blks block then by double-clicking on the middle_instance_1 block.

  2. To view the interface configuration, in the rtwdemo_cppclass_refmid model, double-click the yellow button (labeled View Interface Configuration). Or, select Configuration Parameters > Code Generation > Interface and click Configure C++ Class Interface.

  3. Observe that the configuration for the class interface for the referenced model differs from the class interface for the top-level model (for example, the name space differs).

open_system('rtwdemo_cppclass_refmid');

Apply Mixed C/C++ Interface Styles in a Model Reference Hierarchy

The rtwdemo_cppclass_refc model is one of the referenced models in the example model rtwdemo_cppclass.

This referenced model shows that mixed interface styles can be used within a model reference hierarchy. This model uses the C code interface style (compiled with a C++ compiler), while the other models in the same hierarchy use C++ class interfaces.

  1. Open the rtwdemo_cppclass_refc referenced model by double-clicking on the Subsystem_with_MdlRef_blks block then double-clicking on the ref_c_instance block.

  2. Select Configuration Parameters > Code Generation > Interface. Observe that Code interface packaging is set to Nonreusable function (for a C function prototype).

  3. To view the Step function preview for the function prototype, click on Configure Model Functions, then click Validate.

open_system('rtwdemo_cppclass_refc');

More About

For more details and limitations, see Control Generation of C++ Class Interfaces in the Embedded Coder documentation.

Was this topic helpful?