Union Type

An xsd:union type is used to specify that one of several simple types can be used for a specific value. This type is mapped to a C structured type that contains an identifier for the selected type and a union of all of the possible types. Atomic types (i.e. those that use a single processor storage unit such as integer or Boolean) are stored as values in the union whereas compound or structured types (such as the structure used to represent a hexBinary type) are stored as pointers.

The general mapping is as follows:

XSD type:

   <xsd:simpleType name="TypeName">
      <xsd:union memberTypes="Type1 … TypeN"/>
   </xsd:simpleType>

Generated C code:

   /* choice tag constants */
   #define T_TypeName_type1   1
   ...
   #define T_TypeName_typeN   N

   typedef struct TypeName {
      OSUINT16 t;
      union {
         /* t = 1 */
         Type1 type1;
         ...
         /* t = N */
         TypeN typeN;
      } u;
   } TypeName;

Generated C++ code:

   class TypeName : public OSRTBaseType {
   public:
      // tag constants
      enum {
         T_type1 = 1,
         ...
         T_typeN = N,
      } ;
      OSUINT16 t;
      union {
         /* t = 1 */
         Type1 type1;
         ...
         /* t = N */
         TypeN typeN;
      } u;
      ...
   } ;

Notes:

  1. Where typename begins with a lowercase letter above (for example, Type1 is shown as type1 in places), it means the actual typename is used with the first letter set to lowercase.

  2. The choice tag constants ( T_TypeName_type) are the identifiers of each of the particular values in the union. The selected value is stored in the t member variable of the generated structure. In the case of C++, the tag values are in the form of an enum construct within the class containing enumerations of the form T_type.

  3. The member variables in the union may be stored as values (if atomic) or as pointers to a value of the item (if structured).

  4. The generated C++ class contains additional methods to get, set, or query the union value. These are in the form of get_type, set_type, and is_type respectively.