Generating and Controlling C++ Encapsulation Interfaces

Overview of C++ Encapsulation

Using the Real-Time Workshop Embedded Coder language option C++ (Encapsulated), 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 The 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 Real-Time Workshop 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 Real-Time Workshop demo model rtwdemo_counter.

  2. Configure the model to use an ert.tlc system target file provided by The MathWorks. For example, open the Configuration Parameters dialog box, go to the Real-Time Workshop pane, select an appropriate 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 Real-Time Workshop pane of the Configuration Parameters dialog box, select the C++ (Encapsulated) language option.

    Click Apply.

  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. Examine the default settings and modify as appropriate.

  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.

  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.

    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, use the Language menu on the Real-Time Workshop pane of the Configuration Parameters dialog box or Model Explorer:

Selecting this option has 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 default options in the Code interface subpane on the Interface pane.

Configuring the Step Method for Your Model Class

To configure the step method for your model class, click the Configure C++ Encapsulation Interface button, which appears on the Interface pane when you selectC++ (Encapsulated) for your model. This action opens the Configure C++ encapsulation interface dialog box, which allows you to 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 Real-Time Workshop > 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
attachToModel (C++ Encapsulation Interface Control)Attach model-specific C++ encapsulation interface to loaded ERT-based Simulink model
getArgCategory (C++ Encapsulation Interface Control)Get argument category for Simulink model port from model-specific C++ encapsulation interface
getArgName (C++ Encapsulation Interface Control)Get argument name for Simulink model port from model-specific C++ encapsulation interface
getArgPosition (C++ Encapsulation Interface Control)Get argument position for Simulink model port from model-specific C++ encapsulation interface
getArgQualifier (C++ Encapsulation Interface Control)Get argument type qualifier for Simulink model port from model-specific C++ encapsulation interface
getClassNameGet class name from model-specific C++ encapsulation interface
getDefaultConf (C++ Encapsulation Interface Control)Get default configuration information for model-specific C++ encapsulation interface from Simulink model to which it is attached
getNumArgs (C++ Encapsulation Interface Control)Get 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
runValidation (C++ Encapsulation Interface Control)Validate model-specific C++ encapsulation interface against Simulink model to which it is attached
setArgCategory (C++ Encapsulation Interface Control)Set argument category for Simulink model port in model-specific C++ encapsulation interface
setArgName (C++ Encapsulation Interface Control)Set argument name for Simulink model port in model-specific C++ encapsulation interface
setArgPosition (C++ Encapsulation Interface Control)Set argument position for Simulink model port in model-specific C++ encapsulation interface
setArgQualifier (C++ Encapsulation Interface Control)Set 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 M-Script for Configuring the Step Method for a Model Class

The following sample M-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

  


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