Expose position on SGPropertyNode.

This commit is contained in:
James Turner
2016-12-27 11:09:07 +00:00
parent f0e6402fff
commit 45ac758cc9
3 changed files with 25 additions and 2 deletions

View File

@@ -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.

View File

@@ -848,6 +848,10 @@ public:
*/
const SGPropertyNode * getParent () const { return _parent; }
/**
* Get node's position in parent (*NOT* index)
*/
unsigned int getPosition() const;
//
// Children.

View File

@@ -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()