pugixml icon indicating copy to clipboard operation
pugixml copied to clipboard

Better node visitor interface

Open zeux opened this issue 8 years ago • 1 comments

Assuming node visitor interface is a good idea (is it? not 100% clear), there are several problems with existing xml_tree_walker:

  1. ::begin/::end are pretty pointless - could be called from external code
  2. No way to get callbacks before/after the subtree - this is what begin/end could have been. This was mentioned (if memory serves) in https://code.google.com/p/pugixml/issues/detail?id=219 but this link is now dead.
  3. No way to skip a subtree. This is mentioned in issue #77.

While manually implementing a recursive traversal is pretty trivial, all traversals that pugixml implements are stackless (which is not as simple/concise to write yourself), so there seems to be some value in a generic traversal. There are still several implementation options though:

  1. Interface with virtual functions. This will have to be a new interface to maintain binary compatibility - xml_tree_visitor or something like that.
  2. Pseudo-interface with templated member functions. This can be faster, although in a non-LTO build it's a weird tradeoff - templated implementations can't inline node structure accessors, while an implementation based on virtual dispatch can't inline user code. With LTO or header-only mode the approach based on compile-time polymorphism should be faster though. This can be seen as an extension of xml_node::find_node.
  3. Tree iterator with ability to descend into a subtree or skip it, so that the user code is in control of the flow.

These need to be evaluated for the performance and versatility in various scenarios. 1 and 2 are extensions of existring ::traverse() support while 3 is basically a separate idea.

Finally, there is - as it is - quite a few ways to traverse the nodes in pugixml. Ideally if we do need a new way it has to supplant the old one - that is, we should at the minimum deprecate existing ::traverse in favor of whatever the new interface is.

zeux avatar Jul 16 '16 00:07 zeux

I believe this is a useful feature and I have a custom implementation of options 1+3 at https://github.com/jamsilva/pugixml_tree_visitor. Feel free to use, change and distribute it under the same license as pugixml.

jamsilva avatar May 14 '17 19:05 jamsilva