TOC PREV NEXT INDEX


Populating Generated Choice Structures



Populating generated CHOICE structures is more complex then for other generated types due to the use of pointers within the union construct. As previously mentioned, the use of pointers with C can be prevented by using the -static command line option. If this is done, the elements within the union construct will be standard inline variable declaration and can be populated directly. Otherwise, the methods listed below can be used to populate the variables.

The recommended way to populate the pointer elements is to declare variables of the embedded type to be used on the stack prior to populating the CHOICE structure. The embedded variable would then be populated with the data to be encoded and then the address of this variable would be plugged into the CHOICE union pointer field.

Consider the following definitions:

AsciiString  ::= [PRIVATE 28] OCTET STRING
 
EBCDICString ::= [PRIVATE 29] OCTET STRING
 
String ::= CHOICE { AsciiString, EBCDICString }
 

This would result in the following type definitions:

typedef ASN1DynOctStr AsciiString;
 
typedef ASN1DynOctStr EBCDICString;
 

typedef struct String {
 
   int t;
 
   union {
 
      /* t = 1 */
 
      AsciiString *asciiString;
 
      /* t = 2 */
 
      EBCDICString *eBCDICString;
 
   } u;
 
} String;
 

To set the AsciiString choice value, one would first declare an AsciiString variable, populate it, and then plug the address into a variable of type String structure as follows:

AsciiString asciiString;
 
String      string;
 

 
asciiString = "Hello!";
 
string.t = T_String_AsciiString;
 
string.u.asciiString = &asciiString;
 

It is also possible to allocate dynamic memory for the CHOICE union option variable; but one must be careful to release this memory when done with the structure.



Objective Systems, Inc.

102 Pickering Way, Suite #506
Exton, Pennsylvania 19341
http://www.obj-sys.com
Phone: (484) 875-9841
Toll-free: (877) 307-6855 (US only)
Fax: (484) 875-9830
info@obj-sys.com
TOC PREV NEXT INDEX