Configure C++ Class Interface for Export-Function Models
This example shows how to configure a C++ class interface for an export-function model. Export-function modeling enables you to generate code for event-triggered functions that can be integrated with an external environment or a scheduler. Functions are defined using Function-Call Subsystem, function-call Model, Simulink Function, and S-Function blocks. This example shows you how to customize the generated class name, namespace, class members, and class methods for an example model. This example, CppClassExportFunction
, provides a set of functions you might customize in a typical export-function model.
Interface Goals and Requirements
A primary goal for configuring the generated C++ code from an export-function model is to be able to customize event-driven behavior. For this example, the goal is to update the name and namespace of the class to more meaningful values, customize the export functions, and to simplify and encapsulate the remaining data and functions in the interface.
To better reflect the event-driven behavior in the model, update the class name to
triggered_events
.To prevent clashes with other symbols in a project and to indicate that the generated code is from Simulink, update the class namespace to
sl
.To simplify and encapsulate the data, set the model elements to private with a get/set method.
To customize the functions, customize the names of the export-functions.
To make the other entry-point names more meaningful, update the names of the initialize and terminate entry-point methods.
Interactive Example
Configure Model Class Name and Namespace
1. On the C++ Code tab, click Code Interface and select Class Name & Namespace.
2. To configure the class name, in the C++ Class Name field, update the name to triggered_events
.
3. To configure the class namespace, in the C++ Class Namespace field, update the name to sl
.
4. Click OK.
Configure Model Data Elements as Class Members
1. Open the Code Mappings editor. On the C++ Code tab, click Code Interface and select Code Mappings.
2. Configure the data visibility. For the model element categories:
Set the Inports and Outports to
private
andMethod
.Set the Model parameters to
private
andMethod
.Set the Model parameter arguments to
private
andMethod
.Set the Signals, states, and internal data to
private
andMethod
.
Configure Model Functions as Class Methods
1. Open the Functions pane. In the Code Mappings editor, click the Functions tab.
2. Configure the export function names.
For the export function,
Export Function: event 1
, from the Method Name column, click and edit the spreadsheet to update the name tosubsystemEvent1
.For the export function,
Export Function: event 2
, from the Method Name column, click and edit the spreadsheet to update the name tosubsystemEvent2
.
3. Configure the Initialize function name. In the Method Name column, click and edit the spreadsheet to change the name to initIntegrator
.
4. Configure the Terminate function name. In the Method Name column, click and edit the spreadsheet to change the name to terminateReadIntegrator
.
5. Verify the method prototypes for all entry-point functions in the Method Preview column.
Generate the Interface
1. Generate code. To generate a C++ class interface, on the C++ Code tab, click Build.
2. View code. To view the generated code, on the tab, click View Code. The generated code appears beside the model in the model workspace.
Programmatic Example
To programmatically configure a C++ class interface for the CppClassExportFunction
model, use this workflow:
Open the example model
model ='CppClassExportFunction';
open_system(model);
% Get the C++ mapping object
cm = coder.mapping.api.get(model);
Configure Model Class Name and Namespace
% Configure the C++ class name setClassName(cm,'event_triggered'); % Configure the enclosing namespace setClassNamespace(cm,'sl');
Configure Model Data Elements as Class Members
% Configure the inports and outports for the model setData(cm, 'Inports', 'DataVisibility', 'private'); setData(cm, 'Outports', 'DataVisibility', 'private'); setData(cm, 'Inports', 'MemberAccessMethod', 'Method'); setData(cm, 'Outports', 'MemberAccessMethod', 'Method'); % Configure the model parameters % static model parameters (shared across all instances) setData(cm, 'ModelParameters','DataVisibility','private'); setData(cm,'ModelParameters', 'MemberAccessMethod','Method'); % Configure the model parameter agruments setData(cm, 'ModelParameterArguments', 'DataVisibility', 'private'); setData(cm, 'ModelParameterArguments', 'MemberAccessMethod', 'Method'); % Configure the internal data setData(cm, 'InternalData', 'DataVisibility', 'private'); setData(cm, 'InternalData', 'MemberAccessMethod', 'Method');
Configure Model Functions as Class Methods
% To configure the export functions, use the find function to % retrieve all the export functions for the model exported_functions = find(cm, 'ExportedFunctions'); % Configure the Exported Function names setFunction(cm, exported_functions{1}, 'MethodName', 'subsystemEvent1'); setFunction(cm, exported_functions{2}, 'MethodName', 'subsystemEvent2'); % Configure the initialize and terminate function names setFunction(cm,'Initialize','MethodName', 'initIntegrator'); setFunction(cm,'Terminate', 'MethodName', 'terminateReadIntegrator');
Generate The Interface
Build the application model to generate the C++ class interface.
See Also
Code Mappings – C++
Editor | coder.mapping.api.CodeMappingCPP