Skip to Main Content Skip to Search
Product Documentation

C++ Encapsulation Interface Control

About C++ Encapsulation Interface Control

Using the Language option, C++ (Encapsulated), on the Code Generation pane of the Configuration Parameters dialog box, you can generate a C++ class interface to model code. The generated interface encapsulates all required model data into C++ class attributes and all model entry point functions into C++ class methods. The benefits of encapsulation include:

C++ encapsulation also works for right-click builds of nonvirtual subsystems. (For information on requirements that apply, see Configuring C++ Encapsulation Interfaces for Nonvirtual Subsystems.)

The general procedure for generating C++ encapsulation interfaces to model code is as follows:

  1. Configure your model to use an ert.tlc system target file provided by MathWorks.

  2. Select the language option C++ (Encapsulated) for your model.

  3. Optionally, configure related C++ encapsulation interface settings for your model code, using either a graphical user interface (GUI) or application programming interface (API).

  4. Generate model code and examine the results.

To get started with an example, see C++ Encapsulation Quick-Start Example. For more details about configuring C++ encapsulation interfaces for your model code, see Generating and Configuring C++ Encapsulation Interfaces to Model Code and Configuring C++ Encapsulation Interfaces Programmatically. For limitations that apply, see C++ Encapsulation Interface Control Limitations.

C++ Encapsulation Quick-Start Example

This example illustrates a simple use of the C++ (Encapsulated) option. It uses C++ encapsulation to generate interfaces for code from a demo model, without extensive modifications to default settings.

To generate C++ encapsulated interfaces for a Simulink model:

  1. Open a model for which you would like to generate C++ encapsulation interfaces. This example uses the demo model rtwdemo_counter.

  2. Configure the model to use an ert.tlc system target file provided by MathWorks. For example, open the Configuration Parameters dialog box, go to the Code Generation pane, select a target value from the System target file menu, and click Apply.

  3. Optionally, as a baseline for later code comparison, generate code from the model using a different Language parameter setting, C++ or C. (You can set up the build directory naming or location to distinguish your baseline build from later builds of the same model.)

  4. On the Code Generation pane of the Configuration Parameters dialog box, select the C++ (Encapsulated) language option.

    Click Apply.

      Note   To immediately generate the default style of encapsulated C++ code, without exploring the related model configuration options, skip steps 5–9 and go directly to step 10.

  5. Go to the Interface pane of the Configuration Parameters dialog box and examine the Code interface subpane.

    When you selected the C++ (Encapsulated) language option for your model, C++ encapsulation interface controls replaced the default options on the Code interface subpane. See Configuring Code Interface Options for descriptions of these controls. You might want to modify the default settings according to your application.

  6. Click the Configure C++ Encapsulation Interface button. This action opens the Configure C++ encapsulation interface dialog box, which allows you to configure the step method for your generated model class. The dialog box initially displays a view for configuring a void-void style step method (passing no I/O arguments) for the model class. In this view, you can rename the model class and the step method for your model.

    See Configuring the Step Method for Your Model Class for descriptions of these controls.

      Note   If the void-void interface style meets your needs, you can skip steps 7–9 and go directly to step 10.

  7. If you want root-level model input and output to be arguments on the step method, select the value I/O arguments step method from the Function specification menu. The dialog box displays a view for configuring an I/O arguments style step method for the model class.

    See Configuring the Step Method for Your Model Class for descriptions of these controls.

  8. Click the Get Default Configuration button. This action causes a Configure C++ encapsulation interface subpane to appear in the dialog box. The subpane displays the initial interface configuration for your model, which provides a starting point for further customization.

    See Passing I/O Arguments for descriptions of these controls.

  9. Perform this optional step only if you want to customize the configuration of the I/O arguments generated for your model step method.

      Note   If you choose to skip this step, you should click Cancel to exit the dialog box.

    If you choose to perform this step, first you must check that the required option Remove root level I/O zero initialization is selected on the Optimization pane, and then navigate back to the I/O arguments step method view of the Configure C++ encapsulation interface dialog box.

    Now you can use the dialog box controls to configure I/O argument attributes. For example, in the Configure C++ encapsulation interface subpane, in the row for the Input argument, you can change the value of Category from Value to Pointer and change the value of Qualifier from none to const *. The preview updates to reflect your changes. Click the Validate button to validate the modified interface configuration.

    Continue modifying and validating until you are satisfied with the step method configuration.

    Click Apply and OK.

  10. Generate code for the model. When the build completes, the code generation report for rtwdemo_counter appears. Examine the report and observe that all required model data is encapsulated into C++ class attributes and all model entry point functions are encapsulated into C++ class methods. For example, click the link for rtwdemo_counter.h to see the class declaration for the model.

Generating and Configuring C++ Encapsulation Interfaces to Model Code

Selecting the C++ (Encapsulated) Option

To select the C++ (Encapsulated) option, in the Configuration Parameters dialog box, on the Code Generation pane, use the Language menu:

When you select this option, you see the following effects on other panes in the Configuration Parameters dialog box:

Configuring Code Interface Options

When you select the C++ (Encapsulated) option for your model, the C++ encapsulation interface controls shown below replace the Code interface default options on the Interface pane.

Configuring the Step Method for Your Model Class

To configure the step method for your model class, on the Interface pane, click the Configure C++ Encapsulation Interface button, which is available when you selectC++ (Encapsulated) for your model. This action opens the Configure C++ encapsulation interface dialog box, where you can configure the step method for your model class in either of two styles:

Passing No Arguments (void-void).  The Configure C++ encapsulation interface dialog box initially displays a view for configuring a void-void style step method for the model class.

Passing I/O Arguments.  If you select I/O arguments step method from the Function specification menu, the dialog box displays a view for configuring an I/O arguments style step method for the model class.

When you click Get Default Configuration, the Configure C++ encapsulation interface subpane appears in the dialog box, displaying the initial interface configuration. For example:

Configuring C++ Encapsulation Interfaces for Nonvirtual Subsystems

C++ encapsulation interfaces can be configured for right-click builds of nonvirtual subsystems in Simulink models, provided that:

To configure C++ encapsulation interfaces for a subsystem that meets the requirements:

  1. Open the containing model and select the subsystem block.

  2. Enter the following MATLAB command:

    RTW.configSubsystemBuild(gcb)

    where gcb is the Simulink function gcb, returning the full block path name of the current block.

    This command opens a subsystem equivalent of the Configure C++ encapsulation interface dialog sequence that is described in detail in the preceding section, Configuring the Step Method for Your Model Class. (For more information about using the MATLAB command, see RTW.configSubsystemBuild.)

  3. Use the Configure C++ encapsulation interface dialog boxes to configure C++ encapsulation settings for the subsystem.

  4. Right-click the subsystem and select Code Generation > Build Subsystem.

  5. When the subsystem build completes, you can examine the C++ encapsulation interfaces in the generated files and the HTML code generation report.

Configuring C++ Encapsulation Interfaces Programmatically

If you select the Language option C++ (Encapsulated) for your model, you can use the C++ encapsulation interface control functions (listed in C++ Encapsulation Interface Control Functions) to programmatically configure the step method for your model class.

Typical uses of these functions include:

C++ Encapsulation Interface Control Functions

FunctionDescription
attachToModelAttach model-specific C++ encapsulation interface to loaded ERT-based Simulink model
getArgCategoryGet argument category for Simulink model port from model-specific C++ encapsulation interface
getArgNameGet argument name for Simulink model port from model-specific C++ encapsulation interface
getArgPositionGet argument position for Simulink model port from model-specific C++ encapsulation interface
getArgQualifierGet argument type qualifier for Simulink model port from model-specific C++ encapsulation interface
getClassNameGet class name from model-specific C++ encapsulation interface
getDefaultConfGet default configuration information for model-specific C++ encapsulation interface from Simulink model to which it is attached
getNumArgsGet number of step method arguments from model-specific C++ encapsulation interface
getStepMethodNameGet step method name from model-specific C++ encapsulation interface
RTW.configSubsystemBuildOpen GUI to configure C function prototype or C++ encapsulation interface for right-click build of specified subsystem
RTW.getEncapsulationInterfaceSpecificationGet handle to model-specific C++ encapsulation interface control object
runValidationValidate model-specific C++ encapsulation interface against Simulink model to which it is attached
setArgCategorySet argument category for Simulink model port in model-specific C++ encapsulation interface
setArgNameSet argument name for Simulink model port in model-specific C++ encapsulation interface
setArgPositionSet argument position for Simulink model port in model-specific C++ encapsulation interface
setArgQualifierSet argument type qualifier for Simulink model port in model-specific C++ encapsulation interface
setClassNameSet class name in model-specific C++ encapsulation interface
setStepMethodNameSet step method name in model-specific C++ encapsulation interface

Sample Script for Configuring the Step Method for a Model Class

The following sample MATLAB script configures the step method for the rtwdemo_counter model class, using the C++ Encapsulation Interface Control Functions.

%% Open the rtwdemo_counter model
rtwdemo_counter

%% Select ert.tlc as the System Target File for the model
set_param(gcs,'SystemTargetFile','ert.tlc')

%% Select C++ (Encapsulated) as the target language for the model
set_param(gcs,'TargetLang','C++ (Encapsulated)')

%% Set required option for I/O arguments style step method (cmd off = GUI on)
set_param(gcs,'ZeroExternalMemoryAtStartup','off')

%% Create a C++ encapsulated interface using an I/O arguments style step method
a=RTW.ModelCPPArgsClass

%% Attach the C++ encapsulated interface to the model
attachToModel(a,gcs)

%% Get the default C++ encapsulation interface configuration from the model
getDefaultConf(a)

%% Move the Output port argument from position 2 to position 1
setArgPosition(a,'Output',1)

%% Reset the model step method name from step to StepMethod
setStepMethodName(a,'StepMethod')

%% Change the Input port argument name, category, and qualifier
setArgName(a,'Input','inputArg')
setArgCategory(a,'Input','Pointer')
setArgQualifier(a,'Input','const *')

%% Validate the function prototype against the model
[status,message]=runValidation(a)

%% if validation succeeded, generate code and build
if status
    rtwbuild(gcs)
end

C++ Encapsulation Interface Control Limitations

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS