get template elements by name

This commit is contained in:
David Rose 2004-10-12 19:50:34 +00:00
parent 2ed895f54f
commit b9b7ddec00
3 changed files with 26 additions and 4 deletions

View File

@ -247,5 +247,9 @@ get_element(int n) const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
const XFileDataObject *XFileDataNodeTemplate:: const XFileDataObject *XFileDataNodeTemplate::
get_element(const string &name) const { get_element(const string &name) const {
int child_index = _template->find_child_index(name);
if (child_index >= 0) {
return get_element(child_index);
}
return NULL; return NULL;
} }

View File

@ -57,12 +57,29 @@ find_child(const string &name) const {
ChildrenByName::const_iterator ni; ChildrenByName::const_iterator ni;
ni = _children_by_name.find(name); ni = _children_by_name.find(name);
if (ni != _children_by_name.end()) { if (ni != _children_by_name.end()) {
return (*ni).second; return get_child((*ni).second);
} }
return NULL; return NULL;
} }
////////////////////////////////////////////////////////////////////
// Function: XFileNode::find_child_index
// Access: Public
// Description: Returns the index number of the child with the
// indicated name, if any, or -1 if none.
////////////////////////////////////////////////////////////////////
int XFileNode::
find_child_index(const string &name) const {
ChildrenByName::const_iterator ni;
ni = _children_by_name.find(name);
if (ni != _children_by_name.end()) {
return (*ni).second;
}
return -1;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: XFileNode::find_descendent // Function: XFileNode::find_descendent
// Access: Public // Access: Public
@ -117,13 +134,13 @@ get_guid() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void XFileNode:: void XFileNode::
add_child(XFileNode *node) { add_child(XFileNode *node) {
_children.push_back(node);
if (node->has_name()) { if (node->has_name()) {
_children_by_name[node->get_name()] = node; _children_by_name[node->get_name()] = (int)_children.size();
} }
if (node->has_guid()) { if (node->has_guid()) {
_x_file->_nodes_by_guid[node->get_guid()] = node; _x_file->_nodes_by_guid[node->get_guid()] = node;
} }
_children.push_back(node);
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////

View File

@ -50,6 +50,7 @@ public:
INLINE int get_num_children() const; INLINE int get_num_children() const;
INLINE XFileNode *get_child(int n) const; INLINE XFileNode *get_child(int n) const;
XFileNode *find_child(const string &name) const; XFileNode *find_child(const string &name) const;
int find_child_index(const string &name) const;
XFileNode *find_descendent(const string &name) const; XFileNode *find_descendent(const string &name) const;
virtual bool has_guid() const; virtual bool has_guid() const;
@ -73,7 +74,7 @@ protected:
typedef pvector< PT(XFileNode) > Children; typedef pvector< PT(XFileNode) > Children;
Children _children; Children _children;
typedef pmap<string, XFileNode *> ChildrenByName; typedef pmap<string, int> ChildrenByName;
ChildrenByName _children_by_name; ChildrenByName _children_by_name;
public: public: