Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Next node function #80

@davidhesselbom

Description

@davidhesselbom

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions