Generated C++ Get/Set Methods

For C++, methods are generated to assist the user in getting, setting, or querying the choice construct variable. These methods are of the form get_elemName, set_elemName, and is_elemName where elemName would be replaced with the name of the element. The get method will return a pointer to the choice item only if it is the selected item; otherwise it will return null. The is method returns a boolean value of true if the element is the selected element or false otherwise. The set method sets the element to the given value and selects it by setting the t value.

C Example

The following is a common example of a choice construct with a nested sequence. This allows element a or element b or both elements to be present in an XML instance of the type:

   <xsd:complexType name="AOrBOrBothType">
      <xsd:choice>
         <xsd:sequence>
            <xsd:element name="a" type="xsd:string"/>
            <xsd:element name="b" type="xsd:string"
                 minOccurs="0"/>
            </xsd:sequence>
         <xsd:element name="b" type="xsd:string"/>
      </xsd:choice>
   </xsd:complexType>

The generated C code for this type is as follows:

   typedef struct AOrBOrBothType_1 {
      struct {
         unsigned bPresent : 1;
      } m;
      OSXMLSTRING a;
      OSXMLSTRING b;
   } AOrBOrBothType_1;

   /* choice tag constants */
   #define T_AOrBOrBothType__seq1 1
   #define T_AOrBOrBothType_b 2
   
   typedef struct AOrBOrBothType {
      OSUINT16 t;
      union {
         /* t = 1 */
         AOrBOrBothType_1 *_seq1;
         /* t = 2 */
         OSXMLSTRING* b;
      } u;
   } AOrBOrBothType;

In this case, XBinder created the type AOrBOrBothType_1 to represent the inner sequence. It then added the _seq1 element to the main C type using this type. A user populating the structure would use the _seq1 element to specify element a or both and would use the b element to specify choice b.

C++ Example

The C++ code generated for the example schema above is as follows:

        class AOrBOrBothType_1 : public OSXSDComplexType {
        public:
          struct {
             unsigned bPresent : 1;
        } m;

           OSXMLStringClass a;
           OSXMLStringClass b;
           ...
        } ;

        class AOrBOrBothType : public OSXSDComplexType {
        public:
           // tag constants
           enum {
              T__seq1 = 1,
              T_b = 2
           } ;
           OSUINT16 t;
           union {
              /* t = 1 */
              AOrBOrBothType_1 *_seq1;
              /* t = 2 */
              OSXMLStringClass *b;
           } u;

           ...

           inline AOrBOrBothType_1* get__seq1 () {
              return u._seq1;
           }
           inline OSBOOL is__seq1 () {
              return (t == T__seq1);
           }
           void set__seq1 (const AOrBOrBothType_1& value);

           inline OSXMLStringClass* get_b () {
              return u.b;
           }
           inline OSBOOL is_b () {
              return (t == T_b);
           }
           void set_b (const OSXMLStringClass& value);
        } ;

This shows the generated get/set methods as well as the generated member variables in the class. If the user wanted to set the class to the nested sequence value, the set__seq1 method could be used. If the user wanted to determine if the b element was selected in the class and then get the value, the following code snippet could be used (object is assumed to be an instance of the AOrBOrBothType class):

        if (object.is_b()) {
           OSXMLStringClass* value = object.get_b();
        }