: _name(copy_string("")),
_index(0),
_parent(0),
+ _path(0),
_path_cache(0),
_type(NONE),
_tied(false),
SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
: _index(node._index),
_parent(0), // don't copy the parent
+ _path(0),
_path_cache(0),
_type(node._type),
_tied(node._tied),
SGPropertyNode * parent)
: _index(index),
_parent(parent),
+ _path(0),
_path_cache(0),
_type(NONE),
_tied(false),
SGPropertyNode::~SGPropertyNode ()
{
delete [] _name;
+ delete _path;
delete _path_cache;
clear_value();
delete _listeners;
const char *
SGPropertyNode::getPath (bool simplify) const
{
- if (_parent == 0)
- return "";
-
- string path = _parent->getPath(simplify);
- path += '/';
- path += _name;
- if (_index != 0 || !simplify) {
- char buffer[128];
- sprintf(buffer, "[%d]", _index);
- path += buffer;
+ // Calculate the complete path only once.
+ if (_path == 0) {
+ string path;
+ if (_parent == 0) {
+ path = "";
+ } else {
+ path = _parent->getPath(simplify);
+ path += '/';
+ path += _name;
+ if (_index != 0 || !simplify) {
+ char buffer[64];
+ sprintf(buffer, "[%d]", _index);
+ path += buffer;
+ }
+ }
+ _path = copy_string(path.c_str());
}
- strncpy(_buffer, path.c_str(), MAX_STRING_LEN);
- return _buffer;
+
+ return _path;
}
SGPropertyNode::Type
const SGPropertyNode * getChild (int position) const;
+ /**
+ * Test whether a named child exists.
+ */
+ bool hasChild (const char * name, int index = 0) const
+ {
+ return (getChild(name, index) != 0);
+ }
+
+
/**
* Get a child node by name and index.
*/
SGPropertyNode * _parent;
vector<SGPropertyNode_ptr> _children;
vector<SGPropertyNode_ptr> _removedChildren;
+ mutable char * _path;
hash_table * _path_cache;
Type _type;
bool _tied;