wxFormBuilder / ticpp

This project is obsolete. TinyXML-2 offers a very similar C++ interface.
MIT License
92 stars 34 forks source link

Next node function #80

Open davidhesselbom opened 7 years ago

davidhesselbom commented 7 years ago

ticpp has the NextSibling family of functions for getting the next node at the same level of the XML tree, as well as Parent and FirstChild for navigating up and down the XML tree. It also has iterators that will iterate over all siblings at a given level, but, as far as I can tell, not all nodes of the tree.

I need to be able to find the next node in the XML tree, be it a sibling, parent, or child, so I came up with this:

ticpp::Node* NextNode(ticpp::Node* node)
{
    ticpp::Node* result;
    if ((result = node->FirstChild(false)) || (result = node->NextSibling(false)))
        return result;
    else
    {
        // Keep going back up in the tree until we find an orphan (the root of the tree).
        // or an ancestor that has a sibling --- a (great-)*(grand)?uncle, if you will.
        while ((node = node->Parent(false)) && !(result = node->NextSibling(false)))
        { }
        return result; // may be "parent of root", i.e. nullptr
    }
}

The function can then be called repeatedly using the return value from the previous call as the argument:

    while (node && someCondition)
        node = node.NextNode(node);

Aside from the fact that the NextNode function can cause a memory leak when node->Parent() returns the root of the tree (as described in #61), it seems strange to me that a similar function apparently does not exist in ticpp already. Is there any other, better way to go about getting the next node?