Conditionals
Structure definitions support two forms of conditions: switch and if statements.
NOTE: The support for conditionals in Structure definitions differ from Structure Functions.
The switch statement allow users to define conditional structure elements based on another structure variable or expression. The general form is:
switch (<expression>)
{
case <expression>:
<<DATA TYPES>>
break;
case <expression>:
<<DATA TYPES>>
break;
default:
<<DATA TYPES>>
break;
};
#include "standard-types.hsl
struct conditionalExample
{
WORD type ;
WORD len ;
switch (type)
{
case 1:
WORD
value ;
__verify(len == 2);
break;
case 2:
DWORD
value ;
__verify(len == 4);
break;
default:
blob value[len];
break;
};
};
The variable or expression used in the switch statement must resolve to an integer (8, 16, 32, or 64 bit).
Each case statement must be followed by break statement.
The following is not
supported:
...
case 1:
case 2:
<<DATA>>
break
;
...
If the structure references switch statement data elements
further down in the structure or from a parent structures, the switch
data element must be declared in all case branches -- including the default
branch.
In the example below, all branches define a "length" structure
element which is referenced in the declaration of "data". If
the "default" case is omitted, the structure may fail to parse
or be applied.
struct example {
UWord len_type;
switch (len_type) {
case 1:
UByte length; break;
case 2:
UWord length; break;
default:
UDWord length; break;
}
blob data[length];
};
The statements allows users to define conditional structure elements using if, else if, else statements and resembles the C/C++ language. Each branch of the if statement must include an open and closing brace and the last branch must included a semicolon.
if (<expression>)
{
} ;
if (<expression>)
{
}
else if (<expression>)
{
}
else
{
} ;
Definition Overview, Data Types, Enumerated Types, Arrays, Strings, Bitfields, Expressions, Conditionals, Functions, Verification, Library Settings, Structure Settings, and Reserved Words and Symbols.