From 45ac758cc9336168fee1ce24e144347babafeda7 Mon Sep 17 00:00:00 2001 From: James Turner Date: Tue, 27 Dec 2016 11:09:07 +0000 Subject: [PATCH] Expose position on SGPropertyNode. --- simgear/props/props.cxx | 14 ++++++++++++-- simgear/props/props.hxx | 4 ++++ simgear/props/props_test.cxx | 9 +++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/simgear/props/props.cxx b/simgear/props/props.cxx index aa1baf31..49dbdad8 100644 --- a/simgear/props/props.cxx +++ b/simgear/props/props.cxx @@ -1100,7 +1100,7 @@ SGPropertyNode::addChildren( const std::string& name, } /** - * Get a non-const child by index. + * Get a non-const child by position. */ SGPropertyNode * SGPropertyNode::getChild (int position) @@ -1113,7 +1113,7 @@ SGPropertyNode::getChild (int position) /** - * Get a const child by index. + * Get a const child by position. */ const SGPropertyNode * SGPropertyNode::getChild (int position) const @@ -1124,6 +1124,16 @@ SGPropertyNode::getChild (int position) const return 0; } +unsigned int SGPropertyNode::getPosition() const +{ + if (_parent == nullptr) { + return 0; + } + + auto it = std::find(_parent->_children.begin(), _parent->_children.end(), this); + assert(it != _parent->_children.end()); + return std::distance(_parent->_children.begin(), it); +} /** * Get a non-const child by name and index, creating if necessary. diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index dcbd9a24..84552a32 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -848,6 +848,10 @@ public: */ const SGPropertyNode * getParent () const { return _parent; } + /** + * Get node's position in parent (*NOT* index) + */ + unsigned int getPosition() const; // // Children. diff --git a/simgear/props/props_test.cxx b/simgear/props/props_test.cxx index a6f41b8b..0cc63450 100644 --- a/simgear/props/props_test.cxx +++ b/simgear/props/props_test.cxx @@ -310,12 +310,19 @@ test_property_nodes () grandchild->setDoubleValue(100); grandchild = child->getChild("bar", 3, true); grandchild->setDoubleValue(200); + + SG_CHECK_EQUAL(grandchild->getPosition(), 1); + + grandchild = child->getChild("bar", 1, true); grandchild->setDoubleValue(300); grandchild = child->getChild("bar", 2, true); grandchild->setDoubleValue(400); dump_node(&root); + SG_CHECK_EQUAL(child->getPosition(), 1); + SG_CHECK_EQUAL(grandchild->getPosition(), 3); + cout << "Trying path (expect /foo[0]/bar[0])" << endl; grandchild = root.getNode("/hack/../foo/./bar[0]"); cout << "Path is " << grandchild->getPath() << endl; @@ -333,6 +340,8 @@ test_property_nodes () cerr << "** FAILED to create /a/b/c" << endl; dump_node(&root); cout << endl; + + } void test_addChild()