Re: [libxml++] Re: [Libxmlplusplus-general] Node ownership



Murray Cumming wrote:
On Thu, 2003-02-06 at 18:23, Stefan Seefeld wrote:

Murray Cumming wrote:
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.


But that's what you've been telling us that you want.

Then I wasn't very clear. What I want is to be able to have
nodes that are not part of any document. Who ownes them is
a different story.

[...]

That is the intention at the moment. Nodes own nodes.

but what about root nodes ? I.e. nodes that don't have a parent
(neither a parent node nor a document) ?

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.


True. Does libxml provide any way to detach a node from it's parent? If
not then we probably can't provide functionality via a wrapper that's
not in the libxml itself.

yes, you can 'unlink' nodes. See below...

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.


This doesn't show how we would detach the node in C. If there is a way
to do that then 1. would also seem to be possible, and simpler.

just unlink it: xmlUnlinkNode(_impl);

The things I dislike about 1) are:

1) you suggest an additional member: 'owned_by_libxml', which isn't
   necessary as you can determine by inspecting the _impl whether it
   is linked into a tree.

2) you need an accessible destructor, so you can delete the node
   explicitely, after having checked that you are allowed to

   if (!node->is_owned_by_libxml()) delete node;

   or equivalent. That doesn't look very nice.

Of course, we do want a way to explicitely delete a node. So why not
just passing by a 'friend' method/object ('Document' is already friend
of 'Node'):

  Document::destroy(node);

would be implemented such that it

* looks whether the node is part of a tree, and if so, unlinks it
* calls 'xmlFreeNode' on the _impl member, which will trigger the
  node itself to be deleted.

Stefan





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