By setting the model configuration parameter Code interface packaging to
C++ class
, you can generate a C++ class interface to model
code. The generated interface encapsulates required model data into C++ class attributes and
model entry-point functions into C++ class methods. The benefits of C++ class encapsulation
include:
Greater control over access to model data
Ability to multiply instantiate model classes
Easier integration of model code into C++ programming environments
C++ class encapsulation also works for right-click builds of nonvirtual subsystems. (For information on requirements that apply, see Configure C++ Class Interfaces for Nonvirtual Subsystems.)
To generate C++ class interfaces in model code:
Configure your model to use an ERT-based system target file provided by MathWorks®.
In the Configuration Parameters dialog box, set Language to
C++
.
Optionally, configure related C++ class interface settings for your model code, using a graphical user interface (GUI) or application programming interface (API).
Generate model code and examine the results.
To get started with an example, see Simple Use of C++ Class Control. For more details about configuring C++ class interfaces for model code, see Customize C++ Class Interfaces Using Graphical Interfaces and Customize C++ Class Interfaces Programmatically. For limitations that apply, see C++ Class Interface Control Limitations.
For an example of C++ class code generation, see Customize Interface to Generated C++ Code That Is Called By C Code.
This example illustrates a simple use of C++ class
code
interface packaging. It generates C++ class code interfaces from an example model, without
extensive modifications to default settings.
For details about setting C++ class parameters, see the sections that follow this example, beginning with Customize C++ Class Interfaces Using Graphical Interfaces.
To generate C++ class interfaces for a Simulink® model:
Open a model for which you would like to generate C++ class code interfaces. This
example uses the model rtwdemo_counter
. Save a copy of the model to a writable
location.
Configure the model to use an ERT-based system target file provided by MathWorks.
Set model configuration parameter Language to
C++
. The setting for model configuration parameter
Code interface packaging changes to C++
class
. Save the model configuration changes by clicking
Apply.
Note
To immediately generate the default style of C++ class code, without exploring related model configuration options, skip steps 4 to 8.
Open the Interface pane of the Configuration Parameters dialog
box and examine the Code interface section. When you select
C++ class
code interface packaging for your model,
additional C++ class interface controls appear in the Code
interface section. See Configure C++ Code Interface Parameters for descriptions of these controls. Modify
the parameter settings according to your interface requirements.
Open the Configure C++ class interface dialog box by clicking the Configure C++ Class Interface button. Use the dialog box to configure the step method for your generated model class. The dialog box initially displays a view for configuring a Default step method for the model class. In this view, you can specify the model class name, step method name, and namespace for your model.
See Configure Step Method for Your Model Class for descriptions of these controls.
If the default interface style meets your needs, skip steps 6 to 8.
If you want root-level model input and output to be arguments on the step method,
set Function specification to I/O arguments step
method
. The dialog box displays a view for configuring an I/O arguments
style step method for the model class. See Configure Step Method for Your Model Class for descriptions of the
controls.
Click the Get Default Configuration button. A Configure C++ class interface subpane appears. The subpane displays the initial interface configuration for your model. See Passing I/O Arguments for descriptions of the controls.
If you want to configure I/O arguments generated for your model step method,
continue modifying and validating the interface until you are satisfied with the step
method configuration.Use the dialog box controls to configure I/O argument attributes.
For example, in the Configure C++ class 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. To save your changes, click Apply and
OK.
If you choose not to configure I/O arguments, click Cancel to exit the dialog box.
Generate code for the model. In the Code view, review the
generated code. Observe that required model data is encapsulated into C++ class
attributes and model entry-point functions are encapsulated into C++ class methods. For
example, select file rtwdemo_counter.h
to see the class declaration
for the model.
If you configured custom I/O arguments for the model step method (optional step 8),
examine the generated code for the step method in rtwdemo_counter.h
and
rtwdemo_counter.cpp
. The arguments should reflect your changes. For
example, if you made the suggested changes for the Input
argument, the input argument should appear as const int32_T
*arg_Input
.
To configure a model to generate C++ class interfaces, set the model configuration
parameter Language to C++
.
This parameter setting:
Disables model configuration parameters that C++ class
does not support. For details, see C++ Class Interface Control Limitations.
Adds additional C++ class interface parameters.
When you configure a model for select C++ class
code
interface packaging, the Interface pane shows parameters that apply
to a C++ interface.
Multi-instance code error diagnostic
Specifies the severity level for diagnostics displayed when a model violates requirements for generating multi-instance code.
None
— Proceed with build without displaying
a diagnostic message.
Warning
— Proceed with build after
displaying a warning message.
Error
(default) — Abort build after
displaying an error message.
Remove error status field in real-time model data structure
Specifies whether to omit the error status field from the generated real-time
model data structure rtModel
(off by default). Selecting this
option reduces memory usage.
Selecting this option can cause the code generator to omit the
rtModel
data structure from generated code.
Include model types in model class
Includes model type definitions within the class namespace of the model. Model type definitions include:
Root-level inports and outports
Block inputs and outputs
DWork vectors
Block parameters and constant parameters
Continuous states
The real-time model data structure (rtM
)
The generated code reduces the MISRA 7-3-1 violations.
Parameter visibility
Specifies whether to generate the block parameter structure as a
public
, private
, or
protected
data member of the C++ model class
(private
by default).
Parameter access
Specifies whether to generate access methods for block parameters for the C++
model class (None
by default). You can select noninlined
access methods (Method
) or inlined access methods
(Inlined method
).
External I/O access
Specifies whether to generate access methods for root-level I/O signals for the
C++ model class (Inlined structure-based method
by
default). If you want to generate access methods, you have the following
options:
Generate noninlined or inlined access methods.
Generate per-signal or structure-based access methods. You can generate a series of set and get methods on a per-signal basis, or generate just one set method that takes the address of an external input structure as an argument and, for external outputs (if applicable), just one get method that returns a reference to an external output structure. The generated code for structure-based access methods has the following general form:
class ModelClass { ... // Root inports set method void setExternalInputs(const ExternalInputs* pExternalInputs); // Root outports get method const ExternalOutputs & getExternalOutputs() const; }
This parameter affects generated code only if you are using the default style step method for your model class, not if you are explicitly passing arguments for root-level I/O signals by using an I/O arguments style step method. For more information, see Passing Default Arguments and Passing I/O Arguments.
External I/O visibility
Specifies whether to generate the root-level I/O structure as a
public
, private
, or
protected
data member of the C++ model class
(private
by default).
Configure C++ Class Interface
Opens the Configure C++ class interface dialog box, which enables you to configure the step method for your model class. For more information, see Configure Step Method for Your Model Class.
Interface parameters that are related, but are less commonly used, appear under Advanced parameters:
Terminate function required
Specifies whether to generate the
method (on by
default). This function contains model termination code and must be called as part of
system shutdown.model
_terminate
Combine signal/state structures
Specifies whether to combine global block signals and global state data into one data structure in the generated code (cleared by default). Selecting this parameter reduces RAM and improves readability of the generated code.
Internal data visibility
Specifies whether to generate internal data structures, such as Block I/O, DWork
vectors, Runtime model, Zero-crossings, and continuous states, as
public
, private
, or
protected
data members of the C++ model class
(private
by default).
Internal data access
Specifies whether to generate access methods for internal data structures, such as
Block I/O, DWork vectors, Runtime model, Zero-crossings, and continuous states, for
the C++ model class (None
by default). You can select
noninlined access methods (Method
) or inlined access
methods (Inlined method
).
Generate destructor
Specifies whether to generate a destructor for the C++ model class (selected by default).
Use dynamic memory allocation for model block instantiation
For a model containing Model blocks, specifies whether generated
code must use dynamic memory allocation, during model object registration, to
instantiate objects for referenced models configured with a C++ class interface
(cleared by default). If you select this parameter, during instantiation of an object
for the top model in a model reference hierarchy, the generated code uses the operator
new
to instantiate objects for referenced models.
Selecting this parameter frees a parent model from having to maintain information about referenced models beyond its direct children. Clearing this parameter means that a parent model maintains information about its referenced models, including its direct and indirect children.
If you select this parameter, a bad_alloc
exception might
be displayed, per the C++ standard, if an out-of-memory error occurs during the
use of new
. Provide code to catch and process the
bad_alloc
exception in case an out-of-memory error occurs for
a new
call during construction of a top model object.
If you select Use dynamic memory allocation for model block instantiation and the base model contains a Model block, the build process might generate copy constructor and assignment operator functions in the private section of the model class. The purpose of the functions is to prevent pointer members within the model class from being copied by other code. For more information, see Model Class Copy Constructor and Assignment Operator.
To configure the step method for your model class, on the Code Generation > Interface pane, click the Configure C++ Class Interface button,
which is available when you select C++ class
code interface
packaging for a model. In the Configure C++ class interface dialog box that appears,
configure the step method for your model class in either of two styles:
The Default step method
supports single-rate models and
multirate models. You can configure the model for single-tasking operation or
multi-tasking operation. This method also supports virtual bus crossing boundaries.
The I/O arguments step method
supports single-rate models
and multirate models. You can configure the model for single-tasking operation.
Passing Default Arguments. The Configure C++ class interface dialog box initially displays a view for configuring a Default step method for the model class.
Step method name
Allows you to specify a step method name other than the default,
step
.
Class name
Allows you to specify a model class name other than the default,
.model
ModelClass
Namespace
Allows you to specify a namespace for the model class. If specified, the namespace is emitted in the generated code for the model class. The Namespace parameter provides a means of scoping C++ model classes. In a model reference hierarchy, you can specify a different namespace for each referenced model.
Step function preview
Displays a preview of the model step function prototype as currently configured. The preview display is dynamically updated after you validate your current configuration.
The list of step function arguments has an entry for each of the model’s
root-level I/O ports. This list does not include model parameter arguments that can
appear in the generated code when the model is used as a referenced model. For
example, a model sldemo_mdlref_counter_paramargs
has an inport
with argument name arg_input
, an outport with argument name
arg_output
, and a saturation block whose limits have workspace
parameter argument names lower_saturation_limit
and
upper_saturation_limit
.
The step function preview for this model is:
sldemo_mdlref_counter_paramargsModelClass :: step ( arg_input, * arg_output )
The function prototype in the generated code differs from the preview. The prototype in the generated code (with the additional model parameter arguments) is:
sldemo_mdlref_counter_paramargsModelClass::step ( real_T arg_input, real_T *arg_output, real_T rtp_lower_saturation_limit, real_T rtp_upper_saturation_limit)
Validate
Validates your current model step function configuration. The Validation pane displays the status and an explanation of a failure.
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.
Get Default Configuration
Click this button to get the initial interface configuration that provides a starting point for further customization.
Step function preview
Displays a preview of the model step function prototype as currently configured. The preview dynamically updates as you make configuration changes.
Validate
Validates your current model step function configuration. The Validation pane displays the status and an explanation of a failure.
When you click Get Default Configuration, the Configure C++ class interface subpane appears in the dialog box, displaying the initial interface configuration. For example:
Step method name
Allows you to specify a step method name other than the default,
step
.
Class name
Allows you to specify a model class name other than the default,
.model
ModelClass
Namespace
Allows you to specify a namespace for the model class. If specified, the namespace is emitted in the generated code for the model class. The Namespace parameter provides a means of scoping C++ model classes. In a model reference hierarchy, you can specify a different namespace for each referenced model.
Order
Displays the numerical position of each argument. Use the Up and Down buttons to change argument order.
Port Name
Displays the port name of each argument (not configurable using this dialog box).
Port Type
Displays the port type, Inport
or Outport
,
of each argument (not configurable using this dialog box).
Category
Displays the passing mechanism for each argument. To change the passing
mechanism for an argument, select Value
,
Pointer
, or Reference
from
the argument's Category menu.
Argument Name
Displays the name of each argument. To change an argument name, click in the argument's Argument name field, position the cursor for text entry, and enter the new name.
Qualifier
Displays the const
type qualifier for each argument. To
change the qualifier for an argument, select an available value from the argument's
Qualifier menu. The possible values are:
none
const
(value)
const*
(value referenced by the pointer)
const*const
(value referenced by the pointer and the
pointer itself)
const &
(value referenced by the reference)
Tip
When a model includes a referenced model, the const
type
qualifier for the root input argument of the referenced model's specified step
function interface is set to none
and the qualifier for the source
signal in the referenced model's parent is set to a value other than
none
, code generation honors the referenced model's interface
specification by generating a type cast that discards the const
type qualifier from the source signal. To override this behavior, add a
const
type qualifier to the referenced model.
Embedded Coder® provides namespace control for scoping model classes generated using C++ class code interface packaging. In the Configure C++ class interface dialog box, use the Namespace parameter to specify a namespace for a model class. If specified, the namespace is emitted in the generated code for the model class. To scope the C++ model classes in a model reference hierarchy, you can specify a different namespace for each referenced model.
For an example of namespace control, see the example model rtwdemo_cppclass
. This
model assigns namespaces as follows:
TopNS
for top-level model
rtwdemo_cppclass
MiddleNS
for referenced model
rtwdemo_cppclass_refmid
BottomNS
for referenced model
rtwdemo_cppclass_refbot
If you build the model with its default settings, you can examine the generated header
and source files for each model to see where the namespace is emitted. For example, the
Namespace setting for the model
rtwdemo_cppclass_refmid
is shown below, followed by excerpts of the
emitted namespace code in the model header and source files.
42 // Class declaration for model rtwdemo_cppclass_refmid 43 namespace MiddleNS { 44 class MiddleClass { 45 // public data and function members 46 public: 47 // Model entry point functions ... 52 // model step function 53 void StepMethod(const real_T *arg_In1, const real_T &arg_In2, real_T 54 *arg_Out1, real_T *arg_Out2); ... 87 }; 88 }
15 #include "rtwdemo_cppclass_refmid.h" 16 #include "rtwdemo_cppclass_refmid_private.h" 17 18 namespace MiddleNS 19 { 20 // Model step function 21 void MiddleClass::StepMethod(const real_T *arg_In1, const real_T &arg_In2, 22 real_T *arg_Out1, real_T *arg_Out2) 23 { ... 43 } ... 83 }
You can configure C++ class interfaces for right-click builds of nonvirtual subsystems in Simulink models, if the following requirements are met:
The model is configured for the C++
language and
C++ class
code interface packaging.
The subsystem is convertible to a Model block using the function
Simulink.SubSystem.convertToModelReference
. For referenced
model conversion requirements, see the Simulink reference page Simulink.SubSystem.convertToModelReference
.
To configure C++ class interfaces for a subsystem that meets the requirements:
Open the containing model and select the subsystem block.
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++ class interface
dialog sequence that is described in detail in the preceding section, Configure Step Method for Your Model Class. (For more information
about using the MATLAB command, see RTW.configSubsystemBuild
.)
Use the Configure C++ class interface dialog boxes to configure C++ class settings for the subsystem.
Right-click the subsystem and select C/C++ Code > Build This Subsystem.
When the subsystem build completes, you can examine the C++ class interfaces in the generated files and the HTML code generation report.
If you select the Code interface packaging option C++
class
for your model, you can use the C++ class interface control functions
(listed in C++ Class Interface Control Functions) to programmatically configure the
step method for your model class.
Typical uses of these functions include:
Create and validate a new step method interface, starting with default configuration information from your Simulink model
Create a model-specific C++ class interface with
or
obj
= RTW.ModelCPPDefaultClass
,
where obj
= RTW.ModelCPPArgsClass
returns a handle to an newly
created, empty C++ class interface.obj
Attach the C++ class interface to your loaded ERT-based Simulink model using attachToModel
.
Get default C++ class interface configuration information from your model using
getDefaultConf
.
Use the Get
and Set
functions listed in
C++ Class Interface Control Functions to test or reset the model
class name and model step method name. Additionally, if you are using the I/O
arguments style step method, you can test and reset argument names, argument
positions, argument categories, and argument type qualifiers.
Validate the C++ class interface using runValidation
. (If validation fails, use the error
message information thatrunValidation
returns to address the
issues.)
Save your model and then generate code using the rtwbuild
function.
Modify and validate an existing step method interface for a Simulink model
Get the handle to an existing model-specific C++ class interface that is
attached to your loaded ERT-based Simulink model using
,
where obj
= RTW.getClassInterfaceSpecification
(modelName
)
is a character vector
specifying the name of a loaded ERT-based Simulink model, and modelName
returns a
handle to a C++ class interface attached to the specified model. If the model does
not have an attached C++ class interface configuration, the function returns
obj
[]
.
Use the Get
and Set
functions listed in
C++ Class Interface Control Functions to test or reset the model
class name and model step method name. Additionally, if the returned interface uses
the I/O arguments style step method, you can test and reset argument names, argument
positions, argument categories, and argument type qualifiers.
Validate the C++ class interface using runValidation
. (If validation fails, use the error
message information that runValidation
returns to address the
issues.)
Save your model and then generate code using the rtwbuild
function.
You should not use the same model-specific C++ class interface control object across multiple models. If you do, changes that you make to the step method configuration in one model propagate to other models, which is usually not desirable.
C++ Class Interface Control Functions
Function | Description |
---|---|
attachToModel | Attach model-specific C++ class interface to loaded ERT-based Simulink model |
getArgCategory | Get argument category for Simulink model port from model-specific C++ class interface |
getArgName | Get argument name for Simulink model port from model-specific C++ class interface |
getArgPosition | Get argument position for Simulink model port from model-specific C++ class interface |
getArgQualifier | Get argument type qualifier for Simulink model port from model-specific C++ class interface |
getClassName | Get class name from model-specific C++ class interface |
getDefaultConf | Get default configuration information for model-specific C++ class interface from Simulink model to which it is attached |
getNamespace | Get namespace from model-specific C++ class interface |
getNumArgs | Get number of step method arguments from model-specific C++ class interface |
getStepMethodName | Get step method name from model-specific C++ class interface |
getPreview | Get preview of model-specific C++ class interface step function prototype |
RTW.configSubsystemBuild | Open GUI to configure C function prototype or C++ class interface for right-click build of specified subsystem |
RTW.getClassInterfaceSpecification | Get handle to model-specific C++ class interface control object |
runValidation | Validate model-specific C++ class interface against Simulink model to which it is attached |
setArgCategory | Set argument category for Simulink model port in model-specific C++ class interface |
setArgName | Set argument name for Simulink model port in model-specific C++ class interface |
setArgPosition | Set argument position for Simulink model port in model-specific C++ class interface |
setArgQualifier | Set argument type qualifier for Simulink model port in model-specific C++ class interface |
setClassName | Set class name in model-specific C++ class interface |
setNamespace | Set namespace in model-specific C++ class interface |
setStepMethodName | Set step method name in model-specific C++ class interface |
The following sample MATLAB script configures the step method for the rtwdemo_counter
model class, using
the C++ Class 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++ as the target language for the model set_param(gcs,'TargetLang','C++') %% Set required option for I/O arguments style step method (cmd off = GUI on) set_param(gcs,'ZeroExternalMemoryAtStartup','off') %% Create a C++ class interface using an I/O arguments style step method a=RTW.ModelCPPArgsClass %% Attach the C++ class interface to the model attachToModel(a,gcs) %% Get the default C++ class 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
Code generation automatically adds a copy constructor and an assignment operator to C++ class declarations when required to securely handle pointer members. The constructor and operator are added as private member functions when both of the following conditions exist:
Model configuration parameter Use dynamic memory allocation for model block instantiation is set.
The base model contains a Model block. The Model block
is not directly or indirectly within a subsystem for which block parameter
Function packaging is set to Reusable
function
.
Under these conditions, the software generates a private copy constructor and assignment operator to prevent pointer members within the model class from being copied by other code.
To prevent generation of these functions, consider clearing the parameter Use dynamic memory allocation for model block instantiation.
The code excerpt below shows generated
code for a model class that has a
pointer member. (Look for instances of model
.hMiddleClass_ptr
). The copy
constructor and assignment operator declarations are shown in bold.
class MiddleClass; // class forward declaration for <S1>/Bottom model instance typedef MiddleClass* MiddleClass_ptr; ... // Class declaration for model cppclass_top class Top { ... // private data and function members private: // Block signals BlockIO_cppclass_top cppclass_top_B; // Block states D_Work_cppclass_top cppclass_top_DWork; // Real-Time Model RT_MODEL_cppclass_top cppclass_top_M; // private member function(s) for subsystem '<Root>/Subsystem' void cppclass_top_Subsystem_Init(); void cppclass_top_Subsystem_Start(); void cppclass_top_Subsystem(); //Copy Constructor Top(const Top &rhs); //Assignment Operator Top& operator= (const Top &rhs); // model instance variable for '<S1>/Bottom model instance' MiddleClass_ptr Bottom_model_instanceMDLOBJ1; };
If a model has a custom model step function for which a scalar output is passed by
value and is updated in a conditionally executed context, configure the output to be
passed by pointer. In the model Configure C++ class interface dialog box, set
Category to Pointer
. Examples of a
conditionally executed context include:
Output has a slower sample time than the fundamental rate of the model when the model is used as a referenced model.
Conditionally executed subsystem modifies the output.
S-function modifies the output.
Hit Crossing block modifies the output.
Rate Transition block modifies the output.
The C++ class
value for model configuration parameter
Code interface packaging does not support some Simulink model configuration parameters. Selecting C++
class
disables these parameters:
Identifier format control subpane on the Identifiers pane
File customization template parameter on the Templates pane
The code and data templates on the Templates pane are
supported for C++ class
code generation. However, the
following template file features that are supported for other language selections
are not supported for C++ class generated code:
Free-form text outside template sections
Custom tokens
TLC commands (<! >
tokens)
Global data placement subpane on the Code Placement pane
Selecting C++ class
also disables the Code Mappings
editor (see Configure Default C Code Generation for Categories of Data Elements and Functions). You cannot apply
default storage classes, memory sections, or function templates to the model.
Among the data exchange interfaces available on the Interface
pane of the Configuration Parameters dialog box, only the C API interface is supported
for C++ class
code generation. If you select
External mode or ASAP2 interface, code
generation fails with a validation error.
The I/O arguments style of step method specification supports single-rate models and multirate single-tasking models, but not multirate multitasking models.
If you have a Stateflow® license, for a Stateflow chart that resides in a root model configured to use the I/O
arguments step method
function specification, and that uses a model root
inport value or calls a subsystem that uses a model root inport value, do one of the
following to generate code:
In the Stateflow chart, clear the Execute (enter) Chart At Initialization check box.
Insert a Simulink Signal Conversion block immediately after the root inport. In the Signal Conversion block parameters dialog box, select Exclude this block from 'Block reduction' optimization.
If a model root inport value connects to a Simscape™ conversion block, insert a Simulink Signal Conversion block between the root inport and the Simscape conversion block. In the Signal Conversion block parameters dialog box, select Exclude this block from 'Block reduction' optimization.
When building a referenced model that is configured to generate a C++ class interface:
Do not use a C++ class interface in cases when a referenced model cannot have a combined output/update function. Cases include a model that has a continuous sample time or saves states.
Do not use virtual buses as inputs or outputs to the referenced model when the referenced model uses the I/O arguments step method. When bus signals cross referenced model boundaries, either use nonvirtual buses or use the Default step method.
If the C++ encapsulation interface is not the default, the value is ignored for the Pass fixed-size scalar root inputs by value for code generation parameter. For more information, see Pass fixed-size scalar root inputs by value for code generation.
Customize the interface of C++ code that you generate from a model and want to call from C or C++ code. To call the C++ code from C code, you encapsulate the C++ class interface in the generated code.
Configure a Model with Custom C++ Encapsulation Interface
If starting with a new model:
Create a model with at least one inport and at least one outport.
Open the Embedded Coder app.
Set model configuration parameter Language to C++
.
Set model configuration parameter Code interface packaging to C++ class
.
In the Model Configuration Parameters dialog box, on the Code Generation > Interface pane, click Configure C++ Class Interface. Customize the interface as needed. See Customize Generated C++ Class Interfaces.
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. Use the I/O arguments style interface to customize the step method prototype. Use the default step method style interface to achieve the maximized supported feature set including virtual buses across model boundaries and a multitasking interface.
open_system('rtwdemo_cppclass');
Generate Code
Open the example model rtwdemo_cppclass
.
Save a copy of the model to a writable location.
Open the Embedded Coder app.
Generate code.
Examine the generated source code. Minimally, review the customized model step and initialization classes ModelClass::step_method
and ModelClass::initialize
in file rtwdemo_cppclass.cpp
.
View the interface configuration. In the model, double-click the yellow button labeled View Interface Configuration. Alternatively, in the Model Configuration Parameters dialog box on the Code Generation > Interface pane, click Configure C++ Class Interface. Observe the customized arguments for the step function and how they relate to the generated code.
Change to Default Arguments and Rebuild Model
If starting with a new model, populate the initial configuration for the C++ class interface.
In the Model Interface dialog box, click Get Default Configuration.
Close the Model Interface dialog box.
Close the Model Configuration Parameters dialog box.
Generate code.
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
. Observe the default arguments for the step function and how these relate to the generated code.
Apply Custom Arguments and Rebuild Model
In the model, double-click the yellow button labeled View Interface Configuration. Alternatively, Model Configuration Parameters dialog box, in the Code Generation > Interface pane, click Configure C++ Class Interface.
In the Confgiure C++ class interface dialog box, move Port Name Out1
to the top of the order and set its Category to Value
.
Close the Configure C++ class interface dialog box.
Close the Model Configuration Parameters dialog box.
Generate code.
In the generated file rtwdemo_cppclass.cpp
, inspect the customized model step and initialization classes ModelClass::step_method
and ModelClass::initialize
. Observe that 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.
Configure the function specification for the default step method by double-clicking the yellow button labeled Use Default Interface in MultiTasking Mode.
Generate code.
In the generated code, inspect the model step and initialization classes.
Configure the function specification for the I/O arguments step method by double-clicking the yellow button labeled Use I/O Arguments Interface.
Generate code.
In the generated code, inspect the model step and initialization classes.
Apply C++ Class Interface with Referenced Models
The rtwdemo_cppclass_refmid
model is a referenced model in example model rtwdemo_cppclass
. That referenced model shows how to generate a customizable C++ class interface to generated code for a referenced model in a model reference hierarchy.
Open model rtwdemo_cppclass_refmid
.
Open the Embedded Coder app.
Double-click the Subsystem_with_MdlRef_blks
block. Then, double-click the middle_instance_1
block.
View the interface configuration in the rtwdemo_cppclass_refmid
model. Double-click the yellow button labeled View Interface Configuration. Alternatively, in the Model Configuration Parameters dialog box, on the Code Generation > Interface pane, click Configure C++ Class Interface. Observe that the configuration for the class interface for the referenced model differs from the class interface for the top model (for example, the namespace differs).
open_system('rtwdemo_cppclass_refmid');
Apply Mixed C/C++ Interface Styles in Model Reference Hierarchy
The rtwdemo_cppclass_refc
model is a referenced model in the example model rtwdemo_cppclass
. That referenced model shows how to use mixed interface styles within a model reference hierarchy. The model uses the C code interface style (compiled with a C++ compiler), while the other models in the same model hierarchy use C++ class interfaces.
Open model rtwdemo_cppclass
.
Double-click the Subsystem_with_MdlRef_blks
block. Then, double-click the ref_c_instance
block.
Open the Embedded Coder app. A dialog box appears asking whether you want to generate code for the top model rtwdemo_cppclass
or for the active model rtwdemo_cppclass_refc
.
Click the Open active model as top button.
In the Simulink Editor window that opens for model rtwdemo_cppclass_refc
, open the Embedded Coder app.
Check the setting of model configuration parameter Code interface packaging. It is set to Nonreusable function (for a C function prototype). Then, close the Model Configuration Parameters dialog box.
Click the Code Mappings - C tab.
In the Code Mappings editor, click the Functions tab.
Click the Update Code Mappings button.
In the table, view the function prototype preview for the step function.
Close the model.
open_system('rtwdemo_cppclass_refc');
More About
For more detail and limitations, see Customize Generated C++ Class Interfaces in the Embedded Coder® documentation.