Main Content

Configure Class Interface

In this step of the tutorial, you configure the C++ class interface. A customized C++ class interface enables the generated classes to meet specific code standards or interface requirements so that the generated code can compile and integrate into larger architectures requiring minimal post-generation customization.

To customize the class interface of model CppClassWorkflowKeyIgnition:

  • Change the class name to engine_status.

  • Set the class namespace to en1.

  • Configure the inports of the model as public with a set method for access.

  • Configure the outports of the model as public with an aggregate structure-based get method to access all output ports.

  • Update the names of the initialize and terminate entry-point methods.

Configure Model Class Name and Namespace

  1. If the model CppClassWorkflowKeyIgnition is not open, open the model.

    openExample('CppClassWorkflowKeyIgnition.slx')
  2. On the C++ Code tab, click Code Interface and select Class Name & Namespace.

  3. Edit the C++ Class Name field to engine_status.

  4. Enter en1 in the C++ Class Namespace field.

    Set the model class name and namespace for the Configure C++ Class Settings window for CppClassWorkflowKeyIgnition

  5. Click OK. Validation is performed interactively with field warnings that alert you if you enter an invalid name or namespace.

  6. Save the model, and then click Build to build and regenerate the code.

  7. In the Code view pane, search for engine_status to view the change in the generated code.

    Search for new class name in the Code view

Configure Visibility and Access of Class Members

  1. Open the Code Mappings editor.

    To open the Code Mappings editor, on the C++ Code tab, click Code Interface and select Code Mappings.

  2. Click the Data tab to open the Data pane.

    The Code Mappings spreadsheet displays visibility and access information for each category of model element.

  3. Configure Inports.

    • In the Data Visibility column, select public.

    • In the Member Access Method column, select Inlined method.

  4. Configure Outports.

    • In the Data Visibility column, select public.

    • In the Member Access Method column, select Structure-based method.

  5. Save the model, and click Build to build and regenerate the code.

  6. In the model CppClassWorkflowKeyIgnition, click the Inport block keyState.

  7. Place your cursor over the ellipsis menu above the block and click Navigate To Code.

    View of the contextual menu for the keyState inport block

    The Code view highlights code that corresponds to the block.

  8. In the Code view, click the search arrows to locate the public declaration and definition of the set method setkeyState for the root inport keyState.

    View of the code for the setkeyState method

    The get method for the aggregated root outports, getExternalOutputs, is declared directly beneath the inport set method.

  9. Place your cursor over the identifier ExtY in the getExternalOutputs method declaration.

    A traceability dialog box displays definitions that correspond to the code.

    View of the traceability dialog box displaying the ExtY structure

  10. In the traceability dialog box, click struct ExtY to locate the structure.

        // External outputs (root outports fed by signals with default storage)
        struct ExtY {
          real_T engineState[3];           // '<Root>/engineState'
          real_T cycleTime;                // '<Root>/cycleTime'
        };
  11. In the search bar, type getExternalOutputs and press Enter to locate the method definition in CppClassWorkflowKeyIgnition.cpp.

Configure Model Functions

Configure the class method names. When you generate C++ code from a model, model entry-point functions appear as class methods in the generated code. To integrate with external code or interface requirements, you can customize the name of the generated methods.

  1. In the Code Mappings editor, click the Functions tab to view the class methods.

  2. Configure the Initialize function name.

    In the Method Name column, click and edit the spreadsheet to change the name to initIntegrator.

  3. Configure the Terminate function name.

    In the Method Name column, click and edit the spreadsheet to change the name to terminateIntegrator.

  4. Verify the updated names in the Method Preview column.

    View of the CppClassWorkflowKeyIgnition model in Simulink. The toolstrip is at the top. The Simulink model is in the middle. The Code Mappings pane is at the bottom. The Functions tab in the Code Mappings pane is open, displaying updated entry-point function names.

  5. Save the model, and then click Build to build and regenerate code.

  6. In the Code view, search for the updated method names for the entry-point functions to view the generated code.

Next, deploy the C++ generated code.