Generate Individual C++ Namespaces for Architectural Data Types - MATLAB & Simulink (original) (raw)
Main Content
You can configure an individual C++ namespace for data types that is independent from the global or model class namespace when using data types that are stored in the Architectural Data section of a data dictionary. For more information regarding architectural data, see What Is Architectural Data?
In this example, you will:
- Specify individual C++ namespaces for architectural data types in the Architectural Data section of a data dictionary.
- Link a data dictionary that contains architectural data types to a model configured for C++ code generation.
- Consume architectural data types on ports in a model configured for C++ code generation by typing the ports with architectural data types.
- Generate C++ code that includes individual C++ namespaces for data types that are consumed in the model.
1. Create a data dictionary by opening the Architectural Data Editor and selecting New. Open the Architectural Data Editor by entering archdataeditor
in the Command Window.
Alternatively, create a data dictionary programmatically.
archdata = Simulink.dictionary.archdata.create("ArchData.sldd");
2. In the Architectural Data Editor, in the Create section of the toolstrip, add an alias data type to the data dictionary by selecting Alias Type.
Alternatively, add an alias type to the data dictionary programmatically.
alias = addAliasType(archdata,"AliasType");
3. Select AliasType
on the Data Types tab, and expand the Code Generation section in the Details pane, and specify the C++ Namespace property as types::ns
.
Alternatively, set the namespace of the alias type programmatically.
set(alias,CppNamespace="types::ns");
4. Save the data dictionary, and open example model CppInterface.
open_system("CppInterface");
5. To link data dictionary ArchData
to the model, on the Modeling tab, in the Design section click Link to Data Dictionary. In the Model Properties dialog box click the Browse button and select data dictionary ArchData
from the File Browser. Click Apply, and then click OK.
6. In the model canvas, open the Block Parameters dialog box of the input
block. Navigate to the Signal Attributes tab and select AliasType
from the Data type list.
7. Save the model, and generate code.
Block parameters UPPER
and LOWER
are members of structure InstP_CppInterface_T
in CppInterface.h
. These parameters are of architectural data type AliasType
which belongs to namespace types::ns
.
class CppInterface final { // public data and function members public: // instance parameters, for system '' struct InstP_CppInterface_T { types::ns::AliasType LOWER; // Variable: LOWER // Referenced by: '/Constant2'
types::ns::AliasType UPPER; // Variable: UPPER
// Referenced by: '<Root>/Constant1'
};
// External inputs (root inport signals with default storage) struct ExtU_CppInterface_T { types::ns::AliasType input; // '/input' };
...
// instance parameters InstP_CppInterface_T CppInterface_InstP{ // Variable: LOWER // Referenced by: '/Constant2'
-10.0,
// Variable: UPPER
// Referenced by: '<Root>/Constant1'
10.0
}; };
...
#endif
Data type AliasType
is defined in CppInterface_types.h
in the associated namespace types::ns
.
... #ifndef CppInterface_types_h_ #define CppInterface_types_h_ #include "rtwtypes.h" #ifndef DEFINED_TYPEDEF_FOR_types__ns__AliasType_ #define DEFINED_TYPEDEF_FOR_types__ns__AliasType_
namespace types { namespace ns { using AliasType = real_T; } }
#endif #endif ...
See Also
Tools
Objects
- Simulink.dictionary.ArchitecturalData | Simulink.dictionary.archdata.AliasType | Simulink.dictionary.archdata.DataInterface | Simulink.dictionary.archdata.EnumType | Simulink.dictionary.archdata.NumericType | Simulink.dictionary.archdata.ServiceInterface | Simulink.dictionary.archdata.StructType