Re: [Libxmlplusplus-general] Node ownership



Murray Cumming wrote:
On Tue, 2003-02-04 at 08:21, Stefan Seefeld wrote:

* decide who can create Nodes,


Clearly libxml++ objects need to be able to create nodes. Maybe people
want to create nodes with normal C++ memory management too.

Well, that's not so clear to me. And even if it was, there are multiple
ways to achieve this. I was discussing this with DV, and he suggested to
make something simple, even if it is a bit restrictive. It's more
important to have a non-leaking library (by that I also mean an API
that is clear about the semantics) than something that is flexible but
is an invitation for bugs.

who owns them if they are taken out of a document,


At the moment they are deleted if that happens.


  etc. May be a 'Store' object may help...


So, let's list some possible solutions. For instance,

1. A boolean owned_by_libxml member variable.
  The libxml destroy callback would not destroy the C++ wrapper instance
if owned_by_libxml is false.

but what would it do instead ? The impl object is already destroyed, so
the wrapper is invalid anyways.


  Methods such as Node::add_child_node(Node*) would set the
owned_by_libxml to true and take ownership. This would be documented.
  Methods such as Node::remove_and_detach() would set the
owned_by_libxml and the Node should be deleted if it is never added to
something else with add_child_node().

2. Use of shared-reference-counting smartpointers everywhere. The libxml destroy callback would just unref() the C++ wrapper
instance. It would be destroyed when the last smartpointer instance went
out of scope.
  I like this idea (I liked it when Xerces-C++ used it in the past), but
  - there might be complex circular-references issues, and
  - people might think it's slow or takes extra memory.

3. Implement reference-counting, as above, but make use of smartpointers
optional. Smartpointers would be necessary when copying parts of an XML
tree to another tree.

I'd suggest

4. add a clipboard-like API, i.e. a place where nodes that aren't
   currently part of a document are helt. This could be hidden:

   We don't need an extra flag to specify ownership. If the document
   points to the node (and, the node to the document), it is owned. The
   question arises only if we take the node out of the document.

   So, the question is how to implement that 'remove node from tree'
   method. It could work like this:

   void Node::remove_child(Node *node)
   {
      Clipboard::store(node); // unlink the node from the current doc
   }
   void Node::insert_child(iterator i, Node *node)
   {
      Clipboard::restore(node);       // remove the node from the
                                      // clipboard, if it was stored,
                                      // else unlink it from the old
                                      // document
      xmlAddPrevSibling(node->_impl); // link the node into the new doc
   }


   the Clipboard would be hidden, only accessible by xmlpp objects.
   Oh, and we may add a static method to Node:

   void Node::destroy(Node *node); that really deletes a subtree.

   The advantage here is that nodes are always owned by *exactly* one
   container. Either a document or a clipboard. That's simple to keep
   in mind by users, and it is simple to implement correctly.

Stefan





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]