From f60df03e3cedb86508645357e17003eb9281f31a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 22 Jan 2010 15:40:12 +0200 Subject: Add support for detaching subtrees in C++/Tree New option: --generate-detach. New test: cxx/tree/detach. --- documentation/cxx/tree/manual/index.xhtml | 89 ++++++++++++++++++++++++++----- documentation/xsd.1 | 7 +++ documentation/xsd.xhtml | 7 +++ 3 files changed, 90 insertions(+), 13 deletions(-) (limited to 'documentation') diff --git a/documentation/cxx/tree/manual/index.xhtml b/documentation/cxx/tree/manual/index.xhtml index c00be9e..695a72e 100644 --- a/documentation/cxx/tree/manual/index.xhtml +++ b/documentation/cxx/tree/manual/index.xhtml @@ -2737,6 +2737,27 @@ public: }; +

In addition, if requested by specifying the --generate-detach + option and only for members of non-fundamental C++ types, the mapping + provides a detach function that returns an automatic pointer to the + member's type, for example:

+ +
+class object: xml_schema::type
+{
+public:
+  ...
+
+  std::auto_ptr<member_type>
+  detach_member ();
+  ...
+
+};
+  
+ +

This function detaches the value from the tree leaving the member + value uninitialized. Accessing such an uninitialized value prior to + re-initializing it results in undefined behavior.

The following code shows how one could use this mapping:

@@ -2746,14 +2767,16 @@ f (object& o) { using xml_schema::string; - string s (o.member ()); // get + string s (o.member ()); // get object::member_type& sr (o.member ()); // get - o.member ("hello"); // set, deep copy - o.member () = "hello"; // set, deep copy + o.member ("hello"); // set, deep copy + o.member () = "hello"; // set, deep copy std::auto_ptr<string> p (new string ("hello")); - o.member (p); // set, assumes ownership + o.member (p); // set, assumes ownership + p = o.detach_member (); // detach, member is uninitialized + o.member (p); // re-attach } @@ -2907,6 +2930,11 @@ public: void set (std::auto_ptr<X>); + // Detach and return the contained value. + // + std::auto_ptr<X> + detach (); + void reset (); }; @@ -2968,6 +2996,9 @@ f (object& o) p = new string ("hello"); o.member ().set (p); // set, assumes ownership + + p = o.member ().detach (); // detach, member is reset + o.member ().set (p); // re-attach } @@ -3042,15 +3073,45 @@ public: sequence interface as defined by the ISO/ANSI Standard for C++ (ISO/IEC 14882:1998, Section 23.1.1, "Sequences"). Practically, this means that you can treat such a sequence - as if it was std::vector. One notable extension - to the standard interface that is available only for - sequences of non-fundamental C++ types is the addition of + as if it was std::vector. Two notable extensions + to the standard interface that are available only for + sequences of non-fundamental C++ types are the addition of the overloaded push_back and insert - member functions which instead of the constant reference - to the element type accept automatic pointer to the element - type. These functions assume ownership of the pointed to - object and resets the passed automatic pointer. -

+ as well as the detach_back and detach + member functions. The additional push_back and + insert functions accept an automatic pointer to the + element type instead of the constant reference. They assume + ownership of the pointed to object and resets the passed + automatic pointer. The detach_back and + detach functions detach the element + value from the sequence container and, by default, remove + the element from the sequence. These additional functions + have the following signatures:

+ +
+template <typename X>
+class sequence
+{
+public:
+  ...
+
+  void
+  push_back (std::auto_ptr<X>)
+
+  iterator
+  insert (iterator position, std::auto_ptr<X>)
+
+  std::auto_ptr<X>
+  detach_back (bool pop = true);
+
+  iterator
+  detach (iterator position,
+          std::auto_ptr<X>& result,
+          bool erase = true)
+
+  ...
+}
+  

The following code shows how one could use this mapping:

@@ -3074,7 +3135,9 @@ f (object& o) s.push_back ("hello"); // deep copy std::auto_ptr<string> p (new string ("hello")); - s.push_back (o); // assumes ownership + s.push_back (p); // assumes ownership + p = s.detach_back (); // detach and pop + s.push_back (p); // re-append // Setting a new container. // diff --git a/documentation/xsd.1 b/documentation/xsd.1 index 762d865..7536bb6 100644 --- a/documentation/xsd.1 +++ b/documentation/xsd.1 @@ -800,6 +800,13 @@ not initialized and accessing them results in undefined behavior. Generate constructors that expect an instance of a base type followed by all required members. +.IP "\fB\--generate-detach\fR" +Generate detach functions for required elements and attributes (detach +functions for optional and sequence cardinalities are provided by the +respective containers). These functions, for example, allow you to move +sub-trees in the object model either within the same tree or between +different trees. + .IP "\fB\--generate-wildcard\fR" Generate accessors and modifiers as well as parsing and serialization code for XML Schema wildcards diff --git a/documentation/xsd.xhtml b/documentation/xsd.xhtml index 46e8c1a..1330509 100644 --- a/documentation/xsd.xhtml +++ b/documentation/xsd.xhtml @@ -711,6 +711,13 @@
Generate constructors that expect an instance of a base type followed by all required members.
+
--generate-detach
+
Generate detach functions for required elements and attributes + (detach functions for optional and sequence cardinalities are + provided by the respective containers). These functions, for + example, allow you to move sub-trees in the object model either + within the same tree or between different trees.
+
--generate-wildcard
Generate accessors and modifiers as well as parsing and serialization code for XML Schema wildcards (any and -- cgit v1.1