From 36ba4a4f1af667682416f48c1698b1167e66e2b1 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov Compared to the one cardinality class, optional adds
- two extra functions for querying and modifying the element's presence
- status. The following example shows how we can use these functions:
person& p = ... @@ -2280,6 +2280,33 @@ if (p.middle_name_present ()) }+
If an optional member is of a variable-length type, then the second
+ _present()
function is omitted. This is done to help
+ detect programming errors that result from a type becoming
+ variable-length due to schema changes. In this situation, before
+ the type becomes variable-length, calling the presence function
+ with true
as its argument and then accessing the
+ member is valid. Once the type becomes variable-length, the
+ same sequence of calls would lead to a runtime error. By
+ omitting the second _present()
function for
+ variable-length types, this kind of errors can be detected
+ at compile time. To reset an optional member of a variable-length
+ type you can call the member modifier function with NULL
+ as its argument. For example, if the middle_name
+ member was of a variable-length type, then the above code fragment
+ would look like this:
+person& p = ... + +if (p.middle_name_present ()) +{ + cout << *p.middle_name () << endl; + p.middle_name (0); // Reset to the "not present" state. +} ++ +
There are two cases in the optional cardinality class that are handled differently. These are optional attributes with default and fixed values. When an optional attribute declaration in XML Schema @@ -3021,9 +3048,6 @@ public: bool sequence_present () const; - void - sequence_present (bool); - const sequence_type& sequence () const; -- cgit v1.1