Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Enumerated Type Safe Casting

When code generated for a Simulink Data Type Conversion block or a Stateflow block casts data to an enumerated type, and the block's Saturate on integer overflow option is selected, the cast uses a safe-cast function. The following code shows a safe-cast function for BasicColors:

   static int32_T ET08_safe_cast_to_BasicColors(int32_T input)
   {
     int32_T output;
     /* Initialize output value to default value for BasicColors (Red) */
     output = 0;
     if ((input >= 0) && (input <= 2)) {
       /* Set output value to input value if it is a member of BasicColors */
       output = input;
     }
     return output;
   }

The cast fails if the value to be cast does not correspond to one of the enumerated values in the enumerated type. When a safe cast fails, the value returned is the underlying integer of the enumerated type's default value. The above code reflects this default for BasicColors. See Specifying a Default Enumerated Value in the Simulink documentation for more information.

When a block's Saturate on integer overflow option is cleared, and the block casts to an enumerated type, the resulting code does not use safe casting. The code is therefore more efficient, but is more vulnerable to runtime errors. No warning about the lack of safe casting appears during code generation.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS