From 36ba4a4f1af667682416f48c1698b1167e66e2b1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 14 Oct 2010 16:13:36 +0200 Subject: Omit _present(bool) for variable-length members --- NEWS | 6 + documentation/cxx/hybrid/guide/index.xhtml | 34 +++- examples/cxx/hybrid/compositors/compositors.xsd | 4 +- examples/cxx/hybrid/compositors/driver.cxx | 2 +- .../parser/validation/built-in/any-type/driver.cxx | 4 +- xsde/cxx/hybrid/extraction-source.cxx | 80 ++++++-- xsde/cxx/hybrid/tree-header.cxx | 55 +++--- xsde/cxx/hybrid/tree-inline.cxx | 220 +++++---------------- 8 files changed, 180 insertions(+), 225 deletions(-) diff --git a/NEWS b/NEWS index b76baec..541c6dc 100644 --- a/NEWS +++ b/NEWS @@ -55,6 +55,12 @@ Version 3.2.0 information, see Section 5.14, "Mapping for anyType" in the Embedded C++/Hybrid Mapping Getting Started Guide. + * The object model interface for optional members of variable-length + types now omits the _present modifier function. This is done to help + detect programming errors that result from a type becoming variable- + length due to schema changes. To reset such optional members you can + call the member modifier function with NULL as its argument. + * New configuration parameter, XSDE_STL_ITERATOR, makes iterators provided by the mapping conform to the STL requirements. This feature requires working header and allows you to use the standard diff --git a/documentation/cxx/hybrid/guide/index.xhtml b/documentation/cxx/hybrid/guide/index.xhtml index ebb67fb..601574d 100644 --- a/documentation/cxx/hybrid/guide/index.xhtml +++ b/documentation/cxx/hybrid/guide/index.xhtml @@ -2267,8 +2267,8 @@ class person

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:

+ functions for querying and modifying the member'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; diff --git a/examples/cxx/hybrid/compositors/compositors.xsd b/examples/cxx/hybrid/compositors/compositors.xsd index 85f0db7..6a4f98e 100644 --- a/examples/cxx/hybrid/compositors/compositors.xsd +++ b/examples/cxx/hybrid/compositors/compositors.xsd @@ -13,8 +13,8 @@ copyright : not copyrighted - public domain - - + + diff --git a/examples/cxx/hybrid/compositors/driver.cxx b/examples/cxx/hybrid/compositors/driver.cxx index 5d7dd01..63205c9 100644 --- a/examples/cxx/hybrid/compositors/driver.cxx +++ b/examples/cxx/hybrid/compositors/driver.cxx @@ -40,7 +40,7 @@ main (int, char*[]) for (simple_choice::c_iterator i = s.begin (); i != s.end(); ++i) { - *i = !*i; + *i += 10; } break; diff --git a/tests/cxx/parser/validation/built-in/any-type/driver.cxx b/tests/cxx/parser/validation/built-in/any-type/driver.cxx index 3ffffa6..bfdd71d 100644 --- a/tests/cxx/parser/validation/built-in/any-type/driver.cxx +++ b/tests/cxx/parser/validation/built-in/any-type/driver.cxx @@ -117,8 +117,8 @@ struct any_simple_extension_pimpl: any_simple_extension_pskel virtual void post_any_simple_extension () { - char& v = post_any_simple_type (); - cout << "'" << b << "'" << endl; + char* v = post_any_simple_type (); + cout << "'" << v << "'" << endl; delete[] v; } #endif diff --git a/xsde/cxx/hybrid/extraction-source.cxx b/xsde/cxx/hybrid/extraction-source.cxx index 1e2a602..0bcc166 100644 --- a/xsde/cxx/hybrid/extraction-source.cxx +++ b/xsde/cxx/hybrid/extraction-source.cxx @@ -277,14 +277,23 @@ namespace CXX if (!a.default_p ()) { os << "}" - << "else" << endl - << "x." << epresent (a) << " (false);"; + << "else" << endl; + + if (fl) + os << "x." << epresent (a) << " (false);"; + else + os << "x." << name << " (0);"; + } else { os << "}" - << "else" << endl - << "x." << edefault (a) << " (true);"; + << "else" << endl; + + if (fl) + os << "x." << edefault (a) << " (true);"; + else + os << "x." << name << " (0);"; } } @@ -403,9 +412,15 @@ namespace CXX } if (e.min () == 0) + { os << "}" - << "else" << endl - << "x." << epresent (e) << " (false);"; + << "else" << endl; + + if (fl) + os << "x." << epresent (e) << " (false);"; + else + os << "x." << name << " (0);"; + } os << "}"; } @@ -486,9 +501,14 @@ namespace CXX << "return false;"; os << "}" - << "else" << endl - << "x." << present << " (false);" - << "}"; + << "else" << endl; + + if (fl) + os << "x." << present << " (false);"; + else + os << "x." << name << " (0);"; + + os << "}"; } else All::contains (a); @@ -608,9 +628,15 @@ namespace CXX } if (e.min () == 0) + { os << "}" - << "else" << endl - << "x." << epresent (e) << " (false);"; + << "else" << endl; + + if (fl) + os << "x." << epresent (e) << " (false);"; + else + os << "x." << name << " (0);"; + } } os << "break;" @@ -697,9 +723,15 @@ namespace CXX << "return false;"; if (c.min () == 0) + { os << "}" - << "else" << endl - << "x." << epresent (c) << " (false);"; + << "else" << endl; + + if (fl) + os << "x." << epresent (c) << " (false);"; + else + os << "x." << name << " (0);"; + } } os << "break;" @@ -789,9 +821,14 @@ namespace CXX << "return false;"; os << "}" - << "else" << endl - << "x." << present << " (false);" - << "}"; + << "else" << endl; + + if (fl) + os << "x." << present << " (false);"; + else + os << "x." << name << " (0);"; + + os << "}"; } else { @@ -907,9 +944,14 @@ namespace CXX << "return false;"; os << "}" - << "else" << endl - << "x." << present << " (false);" - << "}"; + << "else" << endl; + + if (fl) + os << "x." << present << " (false);"; + else + os << "x." << name << " (0);"; + + os << "}"; } else Sequence::contains (s); diff --git a/xsde/cxx/hybrid/tree-header.cxx b/xsde/cxx/hybrid/tree-header.cxx index 90bdd24..20f0328 100644 --- a/xsde/cxx/hybrid/tree-header.cxx +++ b/xsde/cxx/hybrid/tree-header.cxx @@ -1358,6 +1358,7 @@ namespace CXX String const& name (ename (a)); SemanticGraph::Type& t (a.type ()); + Boolean fl (fixed_length (t)); if (a.optional_p () && !fix) @@ -1368,9 +1369,10 @@ namespace CXX << name << " () const;" << endl; - os << "void" << endl - << name << " (bool);" - << endl; + if (fl) + os << "void" << endl + << name << " (bool);" + << endl; } // const type& @@ -1406,7 +1408,7 @@ namespace CXX // type* // detach () // - if (detach && !fixed_length (t)) + if (detach && !fl) { arg_.dispatch (t); os << endl @@ -1453,6 +1455,7 @@ namespace CXX String const& name (ename (e)); SemanticGraph::Type& t (e.type ()); + Boolean fl (fixed_length (t)); if (e.max () != 1) { @@ -1496,9 +1499,10 @@ namespace CXX << present << " () const;" << endl; - os << "void" << endl - << present << " (bool);" - << endl; + if (fl) + os << "void" << endl + << present << " (bool);" + << endl; } // const type& @@ -1530,7 +1534,7 @@ namespace CXX // type* // detach () // - if (detach && !fixed_length (t)) + if (detach && !fl) { arg_.dispatch (t); os << endl @@ -1667,9 +1671,10 @@ namespace CXX << present << " () const;" << endl; - os << "void" << endl - << present << " (bool);" - << endl; + if (fl) + os << "void" << endl + << present << " (bool);" + << endl; // const type& // name () const @@ -1967,9 +1972,10 @@ namespace CXX << present << " () const;" << endl; - os << "void" << endl - << present << " (bool);" - << endl; + if (fl) + os << "void" << endl + << present << " (bool);" + << endl; // const type& // name () const @@ -2203,9 +2209,10 @@ namespace CXX << present << " () const;" << endl; - os << "void" << endl - << present << " (bool);" - << endl; + if (fl) + os << "void" << endl + << present << " (bool);" + << endl; } // const type& @@ -2410,9 +2417,10 @@ namespace CXX << present << " () const;" << endl; - os << "void" << endl - << present << " (bool);" - << endl; + if (fl) + os << "void" << endl + << present << " (bool);" + << endl; // const type& // name () const @@ -2610,9 +2618,10 @@ namespace CXX << present << " () const;" << endl; - os << "void" << endl - << present << " (bool);" - << endl; + if (fl) + os << "void" << endl + << present << " (bool);" + << endl; } // const type& diff --git a/xsde/cxx/hybrid/tree-inline.cxx b/xsde/cxx/hybrid/tree-inline.cxx index fa55403..b28397c 100644 --- a/xsde/cxx/hybrid/tree-inline.cxx +++ b/xsde/cxx/hybrid/tree-inline.cxx @@ -414,44 +414,25 @@ namespace CXX // void // preset (bool); // - os << inl - << "void " << scope << "::" << endl - << name << " (bool x)" - << "{"; - - if (def) + if (fl) { - os << "if (x)"; + os << inl + << "void " << scope << "::" << endl + << name << " (bool x)" + << "{"; - if (fl) + if (def) { - os << endl - << "this->" << member << " = " << edefault_value (a) << - " ();"; + os << "if (x)" << endl + << "this->" << member << " = " << edefault_value (a) << " ();"; } else { - os << "{"; - delete_.dispatch (t, L"this->" + member); - os << "this->" << member << " = 0;" - << "}"; - } - } - else - { - if (fl) os << "this->" << epresent_member (a) << " = x;"; - else - { - os << "if (!x)" - << "{"; - delete_.dispatch (t, L"this->" + member); - os << "this->" << member << " = 0;" - << "}"; } - } - os << "}"; + os << "}"; + } } // const type& @@ -644,23 +625,13 @@ namespace CXX // void // preset (bool); // - os << inl - << "void " << scope << "::" << endl - << present << " (bool x)" - << "{"; - if (fl) - os << "this->" << epresent_member (e) << " = x;"; - else - { - os << "if (!x)" - << "{"; - delete_.dispatch (t, L"this->" + member); - os << "this->" << member << " = 0;" + os << inl + << "void " << scope << "::" << endl + << present << " (bool x)" + << "{" + << "this->" << epresent_member (e) << " = x;" << "}"; - } - - os << "}"; } // const type& @@ -828,31 +799,20 @@ namespace CXX // void // preset (bool); // - os << inl - << "void " << scope << "::" << endl - << present << " (bool x)" - << "{" - << "if (this->" << arm_member << " != " << tag << ")" << endl - << "this->" << arm << " (" << tag << ");"; - if (fl) { - os << endl - << "this->" << umember << "." << member << - ".data_[sizeof ("; + os << inl + << "void " << scope << "::" << endl + << present << " (bool x)" + << "{" + << "if (this->" << arm_member << " != " << tag << ")" << endl + << "this->" << arm << " (" << tag << ");" + << endl + << "this->" << umember << "." << member << ".data_[sizeof ("; var_.dispatch (t); - os << ")] = x;"; - } - else - { - os << "else if (!x)" - << "{"; - delete_.dispatch (t, L"this->" + umember + L"." + member); - os << "this->" << umember << "." << member << " = 0;" + os << ")] = x;" << "}"; } - - os << "}"; } // const type& @@ -1012,34 +972,13 @@ namespace CXX // void // present (bool); // - os << inl - << "void " << scope << "::" << endl - << present << " (bool x)" - << "{"; - if (fl) - os << "this->" << epresent_member (a) << " = x;"; - else - { - String p (L"this->" + member); - - os << "if (!x)" - << "{"; - - if (!custom_alloc) - os << "delete " << p << ";"; - else - os << "if (" << p << ")" - << "{" - << p << "->~" << type << " ();" - << "::xsde::cxx::free (" << p << ");" - << "}"; - - os << p << " = 0;" + os << inl + << "void " << scope << "::" << endl + << present << " (bool x)" + << "{" + << "this->" << epresent_member (a) << " = x;" << "}"; - } - - os << "}"; // const type& @@ -1216,34 +1155,13 @@ namespace CXX // void // preset (bool); // - os << inl - << "void " << scope << "::" << endl - << present << " (bool x)" - << "{"; - if (fl) - os << "this->" << epresent_member (c) << " = x;"; - else - { - String p (L"this->" + member); - - os << "if (!x)" - << "{"; - - if (!custom_alloc) - os << "delete " << p << ";"; - else - os << "if (" << p << ")" - << "{" - << p << "->~" << type << " ();" - << "::xsde::cxx::free (" << p << ");" - << "}"; - - os << p << " = 0;" + os << inl + << "void " << scope << "::" << endl + << present << " (bool x)" + << "{" + << "this->" << epresent_member (c) << " = x;" << "}"; - } - - os << "}"; // const type& @@ -1406,34 +1324,13 @@ namespace CXX // void // preset (bool); // - os << inl - << "void " << scope << "::" << endl - << present << " (bool x)" - << "{"; - if (fl) - os << "this->" << epresent_member (s) << " = x;"; - else - { - String p (L"this->" + member); - - os << "if (!x)" - << "{"; - - if (!custom_alloc) - os << "delete " << p << ";"; - else - os << "if (" << p << ")" - << "{" - << p << "->~" << type << " ();" - << "::xsde::cxx::free (" << p << ");" - << "}"; - - os << p << " = 0;" + os << inl + << "void " << scope << "::" << endl + << present << " (bool x)" + << "{" + << "this->" << epresent_member (s) << " = x;" << "}"; - } - - os << "}"; // const type& @@ -1610,40 +1507,17 @@ namespace CXX // void // preset (bool); // - os << inl - << "void " << scope << "::" << endl - << present << " (bool x)" - << "{" - << "if (this->" << arm_member << " != " << tag << ")" << endl - << "this->" << arm << " (" << tag << ");"; - if (fl) - { - os << endl + os << inl + << "void " << scope << "::" << endl + << present << " (bool x)" + << "{" + << "if (this->" << arm_member << " != " << tag << ")" << endl + << "this->" << arm << " (" << tag << ");" + << endl << "this->" << umember << "." << member << - ".data_[sizeof (" << type << ")] = x;"; - } - else - { - String p (L"this->" + umember + L"." + member); - - os << "else if (!x)" - << "{"; - - if (!custom_alloc) - os << "delete " << p << ";"; - else - os << "if (" << p << ")" - << "{" - << p << "->~" << type << " ();" - << "::xsde::cxx::free (" << p << ");" - << "}"; - - os << p << " = 0;" + ".data_[sizeof (" << type << ")] = x;" << "}"; - } - - os << "}"; } // const type& -- cgit v1.1