PreviousNext

Encapsulated Unions

To define an encapsulated union, use the following syntax:

union [tag] switch (disc_type_spec discriminator) [union_name]

{

case

...

[default_case]

}

If a tag is supplied, it can be used in a type_specifier of the form shown in Unions.

The disc_type_spec indicates the type of the discriminator, which can be an integer, a character, a boolean, or an enumeration.

The union_name specifies a name to be used in C code generated by the IDL compiler. When the IDL compiler generates C code to represent an IDL union, it embeds the union and its discriminator in a C structure. The name of the IDL union becomes the name of the C structure. If you supply a union_name in your type declaration, the compiler assigns this name to the embedded C union; otherwise, the compiler assigns the generic name tagged_union.

A case contains one or more labels and may contain a member declaration:

case constant:

...

[union_member];

Each label in a case specifies a constant. The constant can take any of the forms accepted in an integer, character, or Boolean constant declaration, each of which is described earlier in this topic.

A default_case can be coded anywhere in the list of cases:

default:
[union_member];

A union_member takes the following form:

[[union_member_attribute, ...]] type_specifier declarator;

A union_member_attribute can be any of the following:

· ptr: An attribute indicating that the pointer member being declared is a full pointer: it can be NULL and can be an alias.

· string: An attribute indicating that the character array member being declared is a string.

In any union, the type of the discriminator and the type of all constants in all case labels must resolve to the same type. At the time the union is used, the value of the discriminator selects a member, as follows:

· If the value of the discriminator matches the constant in any label, the member associated with the label is selected.

· If there is no label whose constant matches the value of the discriminator and there is a default case, the default member is selected.

· If there is no label whose constant matches the value of the discriminator and there is no default case, no member is selected and the exception rpc_x_invalid_tag is raised.

Note that IDL prohibits duplicate constant label values.

A union_member can contain only one declarator. If no union_member is supplied, the member is NULL; if that member is selected when the union is used, no data is passed. Note, however, that the tag is always passed.

A union cannot contain a pipe, a conformant array, a varying array, or any structure that contains a conformant or varying array. A union also cannot contain a ref or unique pointer or any structure that contains a ref or unique pointer.

The following is an example of an encapsulated union.

/* IDL construct /*

typedef

union fred switch (long a) ralph {

case 1: float b;

case 2: long c;

} bill;

/* becomes in the generated header file /*

typedef

struct fred {

long a;

union {

float b;

long c;

} ralph;

} bill;