: _name(""),
_index(0),
_parent(0),
+ _path_cache(0),
_type(NONE),
_tied(false),
_attr(READ|WRITE)
: _name(node._name),
_index(node._index),
_parent(0), // don't copy the parent
+ _path_cache(0),
_type(node._type),
_tied(node._tied),
_attr(node._attr)
*/
SGPropertyNode::SGPropertyNode (const string &name,
int index, SGPropertyNode * parent)
- : _name(name), _index(index), _parent(parent), _type(NONE),
- _tied(false), _attr(READ|WRITE)
+ : _name(name),
+ _index(index),
+ _parent(parent),
+ _path_cache(0),
+ _type(NONE),
+ _tied(false),
+ _attr(READ|WRITE)
{
}
for (int i = 0; i < (int)_children.size(); i++) {
delete _children[i];
}
+ delete _path_cache;
clear_value();
}
SGPropertyNode *
SGPropertyNode::getNode (const string &relative_path, bool create)
+{
+ if (_path_cache == 0)
+ _path_cache = new cache_map;
+
+ SGPropertyNode * result = (*_path_cache)[relative_path];
+ if (result == 0) {
+ vector<PathComponent> components;
+ parse_path(relative_path, components);
+ result = find_node(this, components, 0, create);
+ (*_path_cache)[relative_path] = result;
+ }
+
+ return result;
+}
+
+SGPropertyNode *
+SGPropertyNode::getNode (const string &relative_path, int index, bool create)
{
vector<PathComponent> components;
parse_path(relative_path, components);
+ if (components.size() > 0)
+ components[components.size()-1].index = index;
return find_node(this, components, 0, create);
}
const SGPropertyNode *
SGPropertyNode::getNode (const string &relative_path) const
{
- vector<PathComponent> components;
- parse_path(relative_path, components);
- // FIXME: cast away const
- return find_node((SGPropertyNode *)this, components, 0, false);
+ return ((SGPropertyNode *)this)->getNode(relative_path, false);
}
+const SGPropertyNode *
+SGPropertyNode::getNode (const string &relative_path, int index) const
+{
+ return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
+}
\f
////////////////////////////////////////////////////////////////////////
#include STL_STRING
#include <vector>
+#include <map>
#include STL_IOSTREAM
SG_USING_STD(string);
SG_USING_STD(vector);
+SG_USING_STD(map);
#if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
SG_USING_STD(istream);
SG_USING_STD(ostream);
SGPropertyNode * getNode (const string &relative_path, bool create = false);
+ /**
+ * Get a pointer to another node by relative path.
+ *
+ * This method leaves the index off the last member of the path,
+ * so that the user can specify it separately (and save some
+ * string building). For example, getNode("/bar[1]/foo", 3) is
+ * exactly equivalent to getNode("bar[1]/foo[3]"). The index
+ * provided overrides any given in the path itself for the last
+ * component.
+ */
+ SGPropertyNode * getNode (const string &relative_path, int index,
+ bool create = false);
+
+
/**
* Get a const pointer to another node by relative path.
*/
const SGPropertyNode * getNode (const string &relative_path) const;
+ /**
+ * Get a const pointer to another node by relative path.
+ *
+ * This method leaves the index off the last member of the path,
+ * so that the user can specify it separate.
+ */
+ const SGPropertyNode * getNode (const string &relative_path,
+ int index) const;
+
+
//
// Access Mode.
//
int _index;
SGPropertyNode * _parent;
vector<SGPropertyNode *> _children;
-
-
+ typedef map<const string,SGPropertyNode *> cache_map;
+ cache_map * _path_cache;
Type _type;
bool _tied;
int _attr;