From: ehofman Date: Sun, 23 Oct 2005 11:55:48 +0000 (+0000) Subject: Melchior FRANZ: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=debd066edd2bd23249e867354b1f41df88cdae80;p=simgear.git Melchior FRANZ: The attached patch makes remove_child() available as removeChild(pos, keep). That's consistent with getChild. Only renamed remove_child to removeChild and added a check for validity of the pos argument. removeChildren() will be used in input.cxx, and a lot in the upcoming dynamic new_gui dialogs, which are aiming at replacing the hard-coded dialogs. I'll discuss them on the list once the infrastructure is in place. (The gui property was one part of it.) --- diff --git a/simgear/props/props.cxx b/simgear/props/props.cxx index e7dac3f4..dc7ccd6b 100644 --- a/simgear/props/props.cxx +++ b/simgear/props/props.cxx @@ -911,6 +911,32 @@ SGPropertyNode::getChildren (const char * name) const } +/** + * Revove child by position. + */ +SGPropertyNode_ptr +SGPropertyNode::removeChild (int pos, bool keep) +{ + SGPropertyNode_ptr node; + if (pos < 0 || pos > _children.size() - 1) + return node; + + vector::iterator it = _children.begin(); + it += pos; + node = _children[pos]; + _children.erase(it); + if (keep) { + _removedChildren.push_back(node); + } + if (_path_cache) + _path_cache->erase(node->getName()); // EMH - TODO: Take "index" into account! + node->setAttribute(REMOVED, true); + node->clearValue(); + fireChildRemoved(node); + return node; +} + + /** * Remove a child node */ @@ -919,25 +945,29 @@ SGPropertyNode::removeChild (const char * name, int index, bool keep) { SGPropertyNode_ptr ret; int pos = find_child(name, index, _children); - if (pos >= 0) { - vector::iterator it = _children.begin(); - it += pos; - SGPropertyNode_ptr node = _children[pos]; - _children.erase(it); - if (keep) { - _removedChildren.push_back(node); - } - if (_path_cache) - _path_cache->erase(name); // EMH - TODO: Take "index" into account! - node->setAttribute(REMOVED, true); - node->clearValue(); - ret = node; - fireChildRemoved(node); - } + if (pos >= 0) + ret = removeChild(pos, keep); return ret; } +/** + * Remove all children with the specified name. + */ +vector +SGPropertyNode::removeChildren (const char * name, bool keep) +{ + vector children; + + for (int pos = _children.size() - 1; pos >= 0; pos--) + if (compare_strings(_children[pos]->getName(), name)) + children.push_back(removeChild(pos, keep)); + + sort(children.begin(), children.end(), CompareIndices()); + return children; +} + + const char * SGPropertyNode::getDisplayName (bool simplify) const { diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index deb4f14d..cea6d639 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -706,12 +706,24 @@ public: vector getChildren (const char * name) const; + /** + * Remove child by position. + */ + SGPropertyNode_ptr removeChild (int pos, bool keep = true); + + /** * Remove a child node */ SGPropertyNode_ptr removeChild (const char * name, int index = 0, bool keep = true); + /** + * Remove all children with the specified name. + */ + vector removeChildren (const char * name, + bool keep = true); + // // Alias support.