From 3dcdc88b14aec626c87f8f480a1d07781a27c069 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 27 Oct 2009 10:10:46 +0200 Subject: Implement schema enumeration to C++ enum mapping in C++/Hybrid --- documentation/cxx/hybrid/guide/index.xhtml | 162 ++++++++++++++++++++++------- documentation/xsde.1 | 3 + documentation/xsde.xhtml | 4 + 3 files changed, 130 insertions(+), 39 deletions(-) (limited to 'documentation') diff --git a/documentation/cxx/hybrid/guide/index.xhtml b/documentation/cxx/hybrid/guide/index.xhtml index 6856e8c..5a4b33a 100644 --- a/documentation/cxx/hybrid/guide/index.xhtml +++ b/documentation/cxx/hybrid/guide/index.xhtml @@ -241,13 +241,14 @@ - - - - - - - + + + + + + + +
4.1Namespaces
4.2Memory Management
4.3Attributes and Elements
4.4Compositors
4.5Accessing the Object Model
4.6Modifying the Object Model
4.7Creating the Object Model from Scratch
4.8Customizing the Object Model
4.9Polymorphic Object Models
4.3Enumerations
4.4Attributes and Elements
4.5Compositors
4.6Accessing the Object Model
4.7Modifying the Object Model
4.8Creating the Object Model from Scratch
4.9Customizing the Object Model
4.10Polymorphic Object Models
@@ -1542,7 +1543,7 @@ $ xsde cxx-hybrid --generate-parser --generate-serializer \

For information on how to use object models with polymorphic types, - refer to Section 4.9, "Polymorphic Object Models".

+ refer to Section 4.10, "Polymorphic Object Models".

@@ -1626,12 +1627,30 @@ $ xsde cxx-hybrid --generate-parser --generate-serializer \
 // gender (fixed-length)
 //
-class gender: public std::string
+class gender
 {
 public:
+  enum value_type
+  {
+    male,
+    female
+  };
+
   gender ();
+  gender (value_type);
   gender (const gender&);
   gender& operator= (const gender&);
+
+  void
+  value (value_type);
+
+  operator value_type () const;
+
+  const char*
+  string () const;
+
+private:
+  ...
 };
 
 // person (fixed-length)
@@ -1807,7 +1826,7 @@ private:
     
  • it is recursive (that is, one of its elements contains a reference, directly or indirectly, to the type itself)
  • -
  • it is polymorphic (see Section 4.9, "Polymorphic +
  • it is polymorphic (see Section 4.10, "Polymorphic Object Models" for details)
  • @@ -1827,10 +1846,9 @@ private: variable-length because it contains a sequence of person elements (maxOccurs="unbounded"). If we recompile the people.xsd schema with the --no-stl - option, the first two types will also become variable-length - since gender inherits from and person - contains elements of the string built-in type. And - when STL is disabled, string is variable-length.

    + option, the person type will also become variable-length + since it contains elements of the string built-in type. + And when STL is disabled, string is variable-length.

    The object model uses different methods for storing and passing around fixed-length and variable-length types. Instances of @@ -1932,7 +1950,81 @@ s->permanent (per) // Assumes ownership or per. s->contract (con) // Assumes ownership or con.

    -

    4.3 Attributes and Elements

    +

    4.3 Enumerations

    + +

    By default, string-based types that use XML Schema restriction by + enumeration are mapped to C++ classes with semantics similar to + C++ enum (you can suppress this mapping and instead get the plain + inheritance by specifying the --suppress-enum compiler + option). The following code fragment again shows the C++ class that + was generated for the gender XML Schema type presented + at the beginning of this chapter:

    + +
    +// gender (fixed-length)
    +//
    +class gender
    +{
    +public:
    +  enum value_type
    +  {
    +    male,
    +    female
    +  };
    +
    +  gender ();
    +  gender (value_type);
    +  gender (const gender&);
    +  gender& operator= (const gender&);
    +
    +  void
    +  value (value_type);
    +
    +  operator value_type () const;
    +
    +  const char*
    +  string () const;
    +
    +private:
    +  value_type v_;
    +};
    +
    + +

    The gender class defines the underlying C++ enum type + (value_type) with enumerators corresponding to the + enumeration elements in XML Schema. The class also + defines the default constructor, copy constructor, constructor + with the underlying enum type as its argument, and the assignment + operator. The gender class also supports the implicit + conversion to the underlying enum type and the explicit conversion + to string via the string() function. Finally, it + provides the value() modifier function which allows you + to set the underlying enum value explicitly. Note also that such an + enumeration class is always fixed-length since it only contains the + C++ enum value. The following example shows how we can use the + gender class:

    + +
    +gender g = gender::male;
    +g = gender::female;
    +g.value (gender::female); // Same as above.
    +
    +cerr << g.string () << endl;
    +
    +if (g != gender::male)
    +  ...
    +
    +switch (g)
    +{
    +case gender::male:
    +  ...
    +case gender::female:
    +  ...
    +}
    +  
    + + +

    4.4 Attributes and Elements

    As we have seen before, XSD/e generates a different set of member functions for elements with different cardinalities. @@ -1945,7 +2037,7 @@ s->contract (con) // Assumes ownership or con. example, the first-name, last-name, gender, and age elements as well as the id attribute belong to this cardinality class. - The following code fragment shows again the accessor and modifier + The following code fragment again shows the accessor and modifier functions that are generated for the first-name element in the person class:

    @@ -1975,7 +2067,7 @@ class person

    The optional cardinality class covers all elements that can occur zero or one time as well as optional attributes. In our example, the middle-name element belongs to this - cardinality class. The following code fragment shows again the + cardinality class. The following code fragment again shows the accessor and modifier functions that are generated for this element in the person class:

    @@ -2664,13 +2756,13 @@ namespace xml_schema } -

    4.4 Compositors

    +

    4.5 Compositors

    The XML Schema language provides three compositor constructs that are used to group elements: all, sequence, and choice. If a compositor has an optional - or sequence cardinality class (see Section - 4.3, "Attributes and Elements") or if a compositor is + or sequence cardinality class (see Section + 4.4, "Attributes and Elements") or if a compositor is inside choice, then the C++/Hybrid mapping generates a nested class for such a compositor as well as a set of accessor and modifier functions similar to the ones defined for elements @@ -3082,7 +3174,7 @@ private: }; -

    4.5 Accessing the Object Model

    +

    4.6 Accessing the Object Model

    In this section we will examine how to get to the information stored in the object model for the person records vocabulary @@ -3131,7 +3223,7 @@ main () // Print gender, age, and id which are all required. // - cout << "gender: " << p.gender () << endl + cout << "gender: " << p.gender ().string () << endl << "age: " << p.age () << endl << "id: " << p.id () << endl << endl; @@ -3174,7 +3266,7 @@ id: 2 -

    4.6 Modifying the Object Model

    +

    4.7 Modifying the Object Model

    In this section we will examine how to modify the information stored in the object model for our person records vocabulary. @@ -3284,7 +3376,7 @@ main () </people> -

    4.7 Creating the Object Model from Scratch

    +

    4.8 Creating the Object Model from Scratch

    In this section we will examine how to create a new object model for our person records vocabulary. The following application @@ -3311,13 +3403,10 @@ main () person p; p.first_name ("John"); p.last_name ("Doe"); + p.gender (gender::male); p.age (32); p.id (1); - gender g; - g.assign ("male"); - p.gender (g); - ps.push_back (p); } @@ -3328,13 +3417,10 @@ main () p.first_name ("Jane"); p.middle_name ("Mary"); p.last_name ("Doe"); + p.gender (gender::female); p.age (28); p.id (2); - gender g; - g.assign ("male"); - p.gender (g); - ps.push_back (p); } @@ -3385,7 +3471,7 @@ main () </people> -

    4.8 Customizing the Object Model

    +

    4.9 Customizing the Object Model

    Sometimes it is desirable to add extra, application-specific data or functionality to some object model classes or @@ -3809,7 +3895,7 @@ for (people::person_iterator i = ps.begin (); i != ps.end (); ++i) -

    4.9 Polymorphic Object Models

    +

    4.10 Polymorphic Object Models

    When generating polymorphism-aware code (see Section 3.7, "Support for Polymorphism"), some objects @@ -4673,7 +4759,7 @@ namespace xml_schema

    The NMTOKENS and IDREFS built-in XML Schema types are mapped to the string sequence type which - is discussed in Section 4.3, "Attributes and + is discussed in Section 4.4, "Attributes and Elements".

    5.3 Mapping for base64Binary and hexBinary

    @@ -5783,7 +5869,7 @@ main (int argc, char* argv[]) { cerr << "first: " << i->first_name () << endl << "last: " << i->last_name () << endl - << "gender: " << i->gender () << endl + << "gender: " << i->gender ().string () << endl << "age: " << i->age () << endl << endl; } @@ -5795,9 +5881,7 @@ main (int argc, char* argv[]) // Initialize the filter. // - gender g; - g.assign ("female"); - root_s.gender_filter (g); + root_s.gender_filter (gender::female); xml_schema::document_simpl doc_s (root_s, people_s.root_name ()); diff --git a/documentation/xsde.1 b/documentation/xsde.1 index 5d4d5cf..90637e0 100644 --- a/documentation/xsde.1 +++ b/documentation/xsde.1 @@ -948,6 +948,9 @@ Suppress the generation of validation code in serializer. Omit attributes with default and fixed values from serialized XML documents. +.IP "\fB\--suppress-enum\fR" +Suppress the generation of the XML Schema enumeration to C++ enum mapping. + .IP "\fB\--generate-detach\fR" Generate detach functions for elements and attributes of variable-length types. These functions, for example, allow you to move sub-trees in the diff --git a/documentation/xsde.xhtml b/documentation/xsde.xhtml index c4c8995..11a90f5 100644 --- a/documentation/xsde.xhtml +++ b/documentation/xsde.xhtml @@ -820,6 +820,10 @@
    Omit attributes with default and fixed values from serialized XML documents.
    +
    --suppress-enum
    +
    Suppress the generation of the XML Schema enumeration to C++ + enum mapping.
    +
    --generate-detach
    Generate detach functions for elements and attributes of variable-length types. These functions, for example, allow -- cgit v1.1