Main Content

Finely Control Data Representation by Writing TLC Code for a Storage Class

If creating your own storage class by using the properties in an Embedded Coder Dictionary or the Custom Storage Class Designer does not meet your requirements for controlling data representation in the generated code, you can write TLC code that explicitly controls the effect that a storage class has on the code. For example, to create a storage class that yields arbitrarily nested structures, you must write TLC code. In the Custom Storage Class Designer, these advanced storage classes have Type set to Other. You cannot create such a storage class in an Embedded Coder Dictionary, so to use the storage class in the Code Mappings editor, you must configure the dictionary of the model to refer to the package that contains the storage class, as described in Refer to Code Generation Definitions in a Package.

For an example, see Generate Code That Dereferences Data from a Literal Memory Address. For general information about TLC code, see Why Use the Target Language Compiler?.

Create Custom Attributes Class for Storage Class

As described in Allow Users of Storage Class to Specify Property Value, instance-specific properties enable users of a storage class to control the effect that the storage class has on each data item. For example, the built-in storage class ExportToFile has several instance-specific properties such as Header file and Definition file.

When you create a storage class with Type set to Other, to add your own instance-specific properties that are not built into the Custom Storage Class Designer, create a custom attributes class for your package. A custom attributes class is a MATLAB® class that you create as a subclass of Simulink.CustomStorageClassAttributes. Each property that you add to your custom attributes class appears to the user of the storage class as an instance-specific property.

To create your custom attributes class, see Define Data Classes.

Write TLC Code for Storage Class

To control the effect of your storage class, write TLC code that specifies the code to generate for each data item.

  1. In your package namespace folder (for example, +myPackage), create a tlc folder.

  2. Copy a TLC template such as TEMPLATE_v2.tlc from the folder matlabroot/toolbox/rtw/targets/ecoder/csc_templates (open) into your tlc folder.

  3. Write your TLC code, following the comments in the template file. The comments describe how to specify, for example, how the generated code declares, defines, and accesses (by value or by reference) each data item.

Create Storage Class by Using Custom Storage Class Designer

To create your storage class in your package, you open the Custom Storage Class Designer in an advanced mode.

  1. At the command prompt, enter:

    cscdesigner -advanced
  2. Select your package and create a storage class.

  3. For the storage class, set Type to Other. In the Other Attributes pane, specify the name of your TLC file and the name of your custom attributes class.

  4. Set the properties on the Other Attributes pane.

    • Is grouped: Select this option if you intend to combine multiple data items into a single entity in the generated code. For example, the built-in storage classes BitField and Struct are grouped because they can aggregate multiple data items into a single structure variable.

    • TLC file name: Enter the name of your TLC file. The Custom Storage Class Designer assumes that the file exists in the package folder tlc, so specify only the name of the file, not the file path.

    • CSC attributes class name: (optional) If you created a custom attributes class, enter the full name of the class, including the package name. For example, specify myPackage.myCustomAttsClass. For more information, see Create Custom Attributes Class for Storage Class.

  5. On the General and Comments panes, specify values for the remaining properties.

To apply the storage class to data objects, you create the data objects from your package and configure the objects to use your storage class. For more information on applying a custom storage class to data objects, see Apply Storage Class to Data Objects. To apply the storage class in a model by using the Code Mappings editor, you must configure the Embedded Coder Dictionary that the model uses to refer to your package. When you refer to the package, the storage class appears in the Embedded Coder Dictionary and becomes available in the Code Mappings editor. For more information, see Refer to Code Generation Definitions in a Package.

Related Topics