From: Thomas Geymayer Date: Wed, 5 Mar 2014 23:29:55 +0000 (+0100) Subject: SGPropertyNode: do not keep list of deleted children. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5560479665121e086f9b4125bbba35eef59adf07;p=simgear.git SGPropertyNode: do not keep list of deleted children. Get rid of the possibilty to keep delted children. This was not used actively, required unnecessary memory for each property node and lead to subtile bugs, as creating a node after a node with the same path has been deleted resulted in getting the old node back instead of a cleanly initialized one. --- diff --git a/simgear/props/PropertyBasedElement.cxx b/simgear/props/PropertyBasedElement.cxx index 734520a2..44ae0128 100644 --- a/simgear/props/PropertyBasedElement.cxx +++ b/simgear/props/PropertyBasedElement.cxx @@ -50,7 +50,7 @@ namespace simgear // TODO check if really not in use anymore if( _node->getParent() ) _node->getParent() - ->removeChild(_node->getName(), _node->getIndex(), false); + ->removeChild(_node->getName(), _node->getIndex()); } //---------------------------------------------------------------------------- diff --git a/simgear/props/props.cxx b/simgear/props/props.cxx index 882d0432..d9c90a33 100644 --- a/simgear/props/props.cxx +++ b/simgear/props/props.cxx @@ -237,33 +237,19 @@ first_unused_index( const char * name, template inline SGPropertyNode* -SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create) +SGPropertyNode::getExistingChild (Itr begin, Itr end, int index) { int pos = find_child(begin, end, index, _children); - if (pos >= 0) { + if (pos >= 0) return _children[pos]; - } else if (create) { - SGPropertyNode_ptr node; - pos = find_child(begin, end, index, _removedChildren); - if (pos >= 0) { - PropertyList::iterator it = _removedChildren.begin(); - it += pos; - node = _removedChildren[pos]; - _removedChildren.erase(it); - node->setAttribute(REMOVED, false); - _children.push_back(node); - fireChildAdded(node); - return node; - } - } return 0; } - + template SGPropertyNode * SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create) { - SGPropertyNode* node = getExistingChild(begin, end, index, create); + SGPropertyNode* node = getExistingChild(begin, end, index); if (node) { return node; @@ -775,8 +761,6 @@ SGPropertyNode::~SGPropertyNode () // zero out all parent pointers, else they might be dangling for (unsigned i = 0; i < _children.size(); ++i) _children[i]->_parent = 0; - for (unsigned i = 0; i < _removedChildren.size(); ++i) - _removedChildren[i]->_parent = 0; clearValue(); if (_listeners) { @@ -974,8 +958,7 @@ SGPropertyNode::getChild (const char * name, int index, bool create) SGPropertyNode * SGPropertyNode::getChild (const string& name, int index, bool create) { - SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index, - create); + SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index); if (node) { return node; } else if (create) { @@ -1024,7 +1007,7 @@ SGPropertyNode::getChildren (const char * name) const * Remove child by position. */ SGPropertyNode_ptr -SGPropertyNode::removeChild (int pos, bool keep) +SGPropertyNode::removeChild(int pos) { SGPropertyNode_ptr node; if (pos < 0 || pos >= (int)_children.size()) @@ -1034,9 +1017,6 @@ SGPropertyNode::removeChild (int pos, bool keep) it += pos; node = _children[pos]; _children.erase(it); - if (keep) { - _removedChildren.push_back(node); - } node->setAttribute(REMOVED, true); node->clearValue(); @@ -1049,12 +1029,12 @@ SGPropertyNode::removeChild (int pos, bool keep) * Remove a child node */ SGPropertyNode_ptr -SGPropertyNode::removeChild (const char * name, int index, bool keep) +SGPropertyNode::removeChild(const char * name, int index) { SGPropertyNode_ptr ret; int pos = find_child(name, name + strlen(name), index, _children); if (pos >= 0) - ret = removeChild(pos, keep); + ret = removeChild(pos); return ret; } @@ -1063,13 +1043,13 @@ SGPropertyNode::removeChild (const char * name, int index, bool keep) * Remove all children with the specified name. */ PropertyList -SGPropertyNode::removeChildren (const char * name, bool keep) +SGPropertyNode::removeChildren(const char * name) { PropertyList children; for (int pos = static_cast(_children.size() - 1); pos >= 0; pos--) if (compare_strings(_children[pos]->getName(), name)) - children.push_back(removeChild(pos, keep)); + children.push_back(removeChild(pos)); sort(children.begin(), children.end(), CompareIndices()); return children; diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index a43bd2e1..c9e7c05d 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -915,33 +915,30 @@ public: /** * Remove child by position. */ - SGPropertyNode_ptr removeChild (int pos, bool keep = true); + SGPropertyNode_ptr removeChild(int pos); /** * Remove a child node */ - SGPropertyNode_ptr removeChild (const char * name, int index = 0, - bool keep = true); + SGPropertyNode_ptr removeChild(const char * name, int index = 0); /** * Remove a child node */ - SGPropertyNode_ptr removeChild (const std::string& name, int index = 0, - bool keep = true) - { return removeChild(name.c_str(), index, keep); } + SGPropertyNode_ptr removeChild(const std::string& name, int index = 0) + { return removeChild(name.c_str(), index); } /** * Remove all children with the specified name. */ - simgear::PropertyList removeChildren (const char * name, bool keep = true); + simgear::PropertyList removeChildren(const char * name); /** * Remove all children with the specified name. */ - simgear::PropertyList removeChildren (const std::string& name, - bool keep = true) - { return removeChildren(name.c_str(), keep); } + simgear::PropertyList removeChildren(const std::string& name) + { return removeChildren(name.c_str()); } /** * Remove all children (does not change the value of the node) @@ -1754,7 +1751,6 @@ private: /// counted pointer SGPropertyNode * _parent; simgear::PropertyList _children; - simgear::PropertyList _removedChildren; mutable std::string _buffer; simgear::props::Type _type; bool _tied; @@ -1782,7 +1778,7 @@ private: SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false); // very internal method template - SGPropertyNode* getExistingChild (Itr begin, Itr end, int index, bool create); + SGPropertyNode* getExistingChild (Itr begin, Itr end, int index); // very internal path parsing function template friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr, diff --git a/simgear/props/props_io.cxx b/simgear/props/props_io.cxx index a0202b3b..f29779a4 100644 --- a/simgear/props/props_io.cxx +++ b/simgear/props/props_io.cxx @@ -382,7 +382,7 @@ PropsVisitor::endElement (const char * name) SGPropertyNode *dst = parent.node->getChild(name, index, true); copyProperties(src, dst); } - parent.node->removeChild(st.node->getName(), st.node->getIndex(), false); + parent.node->removeChild(st.node->getName(), st.node->getIndex()); } pop_state(); } diff --git a/simgear/scene/model/animation_test.cxx b/simgear/scene/model/animation_test.cxx index 19327a7d..9c6ade02 100644 --- a/simgear/scene/model/animation_test.cxx +++ b/simgear/scene/model/animation_test.cxx @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) VERIFY_CLOSE(anim.center, (v1 + v2) * 0.5) VERIFY_CLOSE(anim.axis, normalize(v2 - v1)) - config->removeChild("axis", 0, false); + config->removeChild("axis", 0); config->setDoubleValue("center/x-m", v1.x()); config->setDoubleValue("center/y-m", v1.y()); config->setDoubleValue("center/z-m", v1.z()); diff --git a/simgear/structure/SGBinding.cxx b/simgear/structure/SGBinding.cxx index 62d1b3c5..28e6f816 100644 --- a/simgear/structure/SGBinding.cxx +++ b/simgear/structure/SGBinding.cxx @@ -44,7 +44,7 @@ SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root) SGBinding::~SGBinding() { if(_arg && _arg->getParent()) - _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false); + _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex()); } void