%% Customize Interface to Generated C++ Code That Is Called By C Code
% 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 <docid:ecoder_ug.brre9ro-1 Customize Generated C++ Class Interfaces>.
%
%% About C++ Encapsulation Interfaces
% A generated C++ class encapsulates model data and methods. Using Embedded 
% Coder(R), 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.
%
% <<../cpp_class_arguments_modified.png>>
%
%% 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.
%
% <<../cpp_class_arguments_default.png>>
%
%% 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 <docid:ecoder_ug.brre9ro-1 Customize Generated C++ Class Interfaces> in the
% Embedded Coder(R) documentation.

%% 
% Copyright 2019 The MathWorks, Inc.