The DOM implementation is based on a Tree class backend called NTree.
Currently, the storage of DOM nodes is done in NTree Node.
Each
DOM node has a pointer on its
NTree's node.
Every tree-specific method in
DOM's Node interface use its counterpart
NTree node's method.
So, the memory representation is like:
Because of runtime switchable allocators, the tree must allow node deletion by undefined function.
NTree does support using a
compile-time/template based Deleter it will call when it doesn't need a node anymore.
This
Deleter is currently implemented by calling
virtual DOM::Node::returnToAllocator method.
Every DOM's type (Node, Element, Document, Attribute, etc...) implement correct node returning to the allocator.
It's DOM's node responsibility to return any data it has acquired.
However, it's NTree's node responsibility to delete all children's node.
Because of the multiple creation method, it's very easy to get confused which deletion method to call.
Let's take some usage example:
- You create a node using DOM methods like createDocument, createElement, etc...
You then have to link your DOM nodes with DOM::Node methods like appendChild or insertBefore.
In that case, you'd use DOM::Node::Suicide method on the root node to delete the whole tree
- You want to remove a Node, so you call DOM::Node::removeChild method
In that case, you'd use DOM::Node::Suicide method on the returned DOM::Node to delete this node only
- You want to replace a Node, so you call DOM::Node::replaceChild method
In that case, you'd use DOM::Node::Suicide method on the returned DOM::Node (if any) to delete this node only
- You want to set an attribute of an DOM::Element node, so you call DOM::Element::setAttributeNode method.
In that case, you'd use DOM::Node::Suicide method on the returned DOM::Attr (if any) to delete this attribute
Usually, as long as you use the same allocator for every DOM::Node, you shouldn't get any difficulties creating and/or deleting Node,
provided you remember calling
DOM::Node::Suicide method on unreachable node before you loose track of the Node.