From 3dcdc88b14aec626c87f8f480a1d07781a27c069 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov For information on how to use object models with polymorphic types,
- refer to Section 4.9, "Polymorphic Object Models".
// 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:
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. -
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: + ... +} ++ + +
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:
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:
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:
};
-
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 -
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> -
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> -
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) -
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".
base64Binary
and hexBinary
--suppress-enum
--generate-detach