]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/props.cxx
Minor fixes from Cameron Moore.
[simgear.git] / simgear / misc / props.cxx
index 42ce347d79a68fa93cc67b05fef94090588819e1..83a2ef320b3a28b509d5f0dba144e3f408f906ed 100644 (file)
@@ -7,6 +7,7 @@
 // $Id$
 
 #include <simgear/compiler.h>
+#include <simgear/debug/logstream.hxx>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,6 +40,12 @@ public:
 // Convenience macros for value access.
 ////////////////////////////////////////////////////////////////////////
 
+#define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
+#define TEST_WRITE if (!getAttribute(WRITE)) return false
+
+#define DO_TRACE_READ(type) if(getAttribute(TRACE_READ)) trace_read(type)
+#define DO_TRACE_WRITE(type) if (getAttribute(TRACE_WRITE)) trace_write(type)
+
 #define GET_BOOL (_value.bool_val->getValue())
 #define GET_INT (_value.int_val->getValue())
 #define GET_LONG (_value.long_val->getValue())
@@ -281,64 +288,92 @@ find_node (SGPropertyNode * current,
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of SGValue.
+// Implementation of SGPropertyNode.
 ////////////////////////////////////////////////////////////////////////
 
 
 /**
- * Default constructor.
- *
- * The type will be UNKNOWN and the raw value will be "".
+ * Default constructor: always creates a root node.
  */
-SGValue::SGValue ()
-  : _type(UNKNOWN), _tied(false)
+SGPropertyNode::SGPropertyNode ()
+  : _name(""),
+    _index(0),
+    _parent(0),
+    _path_cache(0),
+    _type(NONE),
+    _tied(false),
+    _attr(READ|WRITE)
 {
-  _value.string_val = new SGRawValueInternal<string>;
 }
 
 
 /**
  * Copy constructor.
  */
-SGValue::SGValue (const SGValue &source)
+SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
+  : _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)
 {
-  _type = source._type;
-  _tied = source._tied;
-  switch (source._type) {
+  switch (_type) {
+  case NONE:
+    break;
   case ALIAS:
-                               // FIXME!!!
-    _value.alias = (SGValue *)(source.getAlias());
+    _value.alias = node._value.alias;
     break;
   case BOOL:
-    _value.bool_val = source._value.bool_val->clone();
+    _value.bool_val = node._value.bool_val->clone();
     break;
   case INT:
-    _value.int_val = source._value.int_val->clone();
+    _value.int_val = node._value.int_val->clone();
     break;
   case LONG:
-    _value.long_val = source._value.long_val->clone();
+    _value.long_val = node._value.long_val->clone();
     break;
   case FLOAT:
-    _value.float_val = source._value.float_val->clone();
+    _value.float_val = node._value.float_val->clone();
     break;
   case DOUBLE:
-    _value.double_val = source._value.double_val->clone();
+    _value.double_val = node._value.double_val->clone();
     break;
   case STRING:
-  case UNKNOWN:
-    _value.string_val = source._value.string_val->clone();
+  case UNSPECIFIED:
+    _value.string_val = node._value.string_val->clone();
     break;
   }
 }
 
 
+/**
+ * Convenience constructor.
+ */
+SGPropertyNode::SGPropertyNode (const string &name,
+                               int index, SGPropertyNode * parent)
+  : _name(name),
+    _index(index),
+    _parent(parent),
+    _path_cache(0),
+    _type(NONE),
+    _tied(false),
+    _attr(READ|WRITE)
+{
+}
+
+
 /**
  * Destructor.
  */
-SGValue::~SGValue ()
+SGPropertyNode::~SGPropertyNode ()
 {
-  if (_type != ALIAS)
-    clear_value();
+  for (int i = 0; i < (int)_children.size(); i++) {
+    delete _children[i];
+  }
+  delete _path_cache;
+  clear_value();
 }
 
 
@@ -346,11 +381,13 @@ SGValue::~SGValue ()
  * Delete and clear the current value.
  */
 void
-SGValue::clear_value ()
+SGPropertyNode::clear_value ()
 {
   switch (_type) {
+  case NONE:
   case ALIAS:
-    _value.alias->clear_value();
+    _value.alias = 0;
+    break;
   case BOOL:
     delete _value.bool_val;
     _value.bool_val = 0;
@@ -372,86 +409,258 @@ SGValue::clear_value ()
     _value.double_val = 0;
     break;
   case STRING:
-  case UNKNOWN:
+  case UNSPECIFIED:
     delete _value.string_val;
     _value.string_val = 0;
     break;
   }
+  _type = NONE;
 }
 
 
 /**
- * Get the current type.
- *
- * Does not return a type of ALIAS.
+ * Get the value as a string.
  */
-SGValue::Type
-SGValue::getType () const
+string
+SGPropertyNode::get_string () const
 {
-  if (_type == ALIAS)
-    return _value.alias->getType();
-  else
-    return (Type)_type;
+  TEST_READ("");
+  char buf[128];
+
+  switch (_type) {
+  case ALIAS:
+    return _value.alias->getStringValue();
+  case BOOL:
+    if (GET_BOOL)
+      return "true";
+    else
+      return "false";
+  case INT:
+    sprintf(buf, "%d", GET_INT);
+    return buf;
+  case LONG:
+    sprintf(buf, "%ld", GET_LONG);
+    return buf;
+  case FLOAT:
+    sprintf(buf, "%f", GET_FLOAT);
+    return buf;
+  case DOUBLE:
+    sprintf(buf, "%f", GET_DOUBLE);
+    return buf;
+  case STRING:
+  case UNSPECIFIED:
+    return GET_STRING;
+  case NONE:
+  default:
+    return "";
+  }
 }
 
 
 /**
- * Get the current aliased value.
+ * Trace a read access for a property.
  */
-SGValue *
-SGValue::getAlias ()
+void
+SGPropertyNode::trace_read (SGPropertyNode::Type accessType) const
 {
-  return (_type == ALIAS ? _value.alias : 0);
+  SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath()
+        << ", value \"" << get_string() << '"');
 }
 
 
 /**
- * Get the current aliased value.
+ * Trace a write access for a property.
  */
-const SGValue *
-SGValue::getAlias () const
+void
+SGPropertyNode::trace_write (SGPropertyNode::Type accessType) const
 {
-  return (_type == ALIAS ? _value.alias : 0);
+  SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath()
+        << ", value\"" << get_string() << '"');
 }
 
 
 /**
- * Alias to another value.
+ * Alias to another node.
  */
 bool
-SGValue::alias (SGValue * alias)
+SGPropertyNode::alias (SGPropertyNode * target)
 {
-  if (alias == 0 || _type == ALIAS || _tied)
+  if (target == 0 || _type == ALIAS || _tied)
     return false;
   clear_value();
-  _value.alias = alias;
+  _value.alias = target;
   _type = ALIAS;
   return true;
 }
 
 
 /**
- * Unalias from another value.
+ * Alias to another node by path.
+ */
+bool
+SGPropertyNode::alias (const string &path)
+{
+  return alias(getNode(path, true));
+}
+
+
+/**
+ * Remove an alias.
  */
 bool
-SGValue::unalias ()
+SGPropertyNode::unalias ()
 {
-                               // FIXME: keep copy of previous value,
-                               // as with untie()
   if (_type != ALIAS)
     return false;
-  _value.string_val = new SGRawValueInternal<string>;
-  _type = UNKNOWN;
+  _type = NONE;
+  _value.alias = 0;
   return true;
 }
 
 
 /**
- * Get a boolean value.
+ * Get the target of an alias.
  */
-bool
-SGValue::getBoolValue () const
+SGPropertyNode *
+SGPropertyNode::getAliasTarget ()
+{
+  return (_type == ALIAS ? _value.alias : 0);
+}
+
+
+const SGPropertyNode *
+SGPropertyNode::getAliasTarget () const
+{
+  return (_type == ALIAS ? _value.alias : 0);
+}
+
+
+/**
+ * Get a non-const child by index.
+ */
+SGPropertyNode *
+SGPropertyNode::getChild (int position)
+{
+  if (position >= 0 && position < nChildren())
+    return _children[position];
+  else
+    return 0;
+}
+
+
+/**
+ * Get a const child by index.
+ */
+const SGPropertyNode *
+SGPropertyNode::getChild (int position) const
+{
+  if (position >= 0 && position < nChildren())
+    return _children[position];
+  else
+    return 0;
+}
+
+
+/**
+ * Get a non-const child by name and index, creating if necessary.
+ */
+SGPropertyNode *
+SGPropertyNode::getChild (const string &name, int index, bool create)
+{
+  int pos = find_child(name, index, _children);
+  if (pos >= 0) {
+    return _children[pos];
+  } else if (create) {
+    _children.push_back(new SGPropertyNode(name, index, this));
+    return _children[_children.size()-1];
+  } else {
+    return 0;
+  }
+}
+
+
+/**
+ * Get a const child by name and index.
+ */
+const SGPropertyNode *
+SGPropertyNode::getChild (const string &name, int index) const
+{
+  int pos = find_child(name, index, _children);
+  if (pos >= 0)
+    return _children[pos];
+  else
+    return 0;
+}
+
+
+/**
+ * Get all children with the same name (but different indices).
+ */
+vector<SGPropertyNode *>
+SGPropertyNode::getChildren (const string &name)
+{
+  vector<SGPropertyNode *> children;
+  int max = _children.size();
+
+  for (int i = 0; i < max; i++)
+    if (_children[i]->getName() == name)
+      children.push_back(_children[i]);
+
+  sort(children.begin(), children.end(), CompareIndices());
+  return children;
+}
+
+
+/**
+ * Get all children const with the same name (but different indices).
+ */
+vector<const SGPropertyNode *>
+SGPropertyNode::getChildren (const string &name) const
+{
+  vector<const SGPropertyNode *> children;
+  int max = _children.size();
+
+  for (int i = 0; i < max; i++)
+    if (_children[i]->getName() == name)
+      children.push_back(_children[i]);
+
+  sort(children.begin(), children.end(), CompareIndices());
+  return children;
+}
+
+
+string
+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;
+  }
+  return path;
+}
+
+SGPropertyNode::Type
+SGPropertyNode::getType () const
+{
+  if (_type == ALIAS)
+    return _value.alias->getType();
+  else
+    return _type;
+}
+
+
+bool 
+SGPropertyNode::getBoolValue () const
 {
+  DO_TRACE_READ(BOOL);
+  TEST_READ(false);
   switch (_type) {
   case ALIAS:
     return _value.alias->getBoolValue();
@@ -466,20 +675,19 @@ SGValue::getBoolValue () const
   case DOUBLE:
     return GET_DOUBLE == 0.0L ? false : true;
   case STRING:
-  case UNKNOWN:
+  case UNSPECIFIED:
     return (GET_STRING == "true" || getDoubleValue() != 0.0L);
+  case NONE:
+  default:
+    return false;
   }
-
-  return false;
 }
 
-
-/**
- * Get an integer value.
- */
-int
-SGValue::getIntValue () const
+int 
+SGPropertyNode::getIntValue () const
 {
+  DO_TRACE_READ(INT);
+  TEST_READ(0);
   switch (_type) {
   case ALIAS:
     return _value.alias->getIntValue();
@@ -494,20 +702,19 @@ SGValue::getIntValue () const
   case DOUBLE:
     return int(GET_DOUBLE);
   case STRING:
-  case UNKNOWN:
+  case UNSPECIFIED:
     return atoi(GET_STRING.c_str());
+  case NONE:
+  default:
+    return 0;
   }
-
-  return 0;
 }
 
-
-/**
- * Get a long integer value.
- */
-long
-SGValue::getLongValue () const
+long 
+SGPropertyNode::getLongValue () const
 {
+  DO_TRACE_READ(LONG);
+  TEST_READ(0L);
   switch (_type) {
   case ALIAS:
     return _value.alias->getLongValue();
@@ -522,18 +729,19 @@ SGValue::getLongValue () const
   case DOUBLE:
     return long(GET_DOUBLE);
   case STRING:
-  case UNKNOWN:
+  case UNSPECIFIED:
     return strtol(GET_STRING.c_str(), 0, 0);
+  case NONE:
+  default:
+    return 0L;
   }
 }
 
-
-/**
- * Get a float value.
- */
-float
-SGValue::getFloatValue () const
+float 
+SGPropertyNode::getFloatValue () const
 {
+  DO_TRACE_READ(FLOAT);
+  TEST_READ(0.0);
   switch (_type) {
   case ALIAS:
     return _value.alias->getFloatValue();
@@ -548,20 +756,19 @@ SGValue::getFloatValue () const
   case DOUBLE:
     return float(GET_DOUBLE);
   case STRING:
-  case UNKNOWN:
+  case UNSPECIFIED:
     return atof(GET_STRING.c_str());
+  case NONE:
+  default:
+    return 0.0;
   }
-
-  return 0.0;
 }
 
-
-/**
- * Get a double value.
- */
-double
-SGValue::getDoubleValue () const
+double 
+SGPropertyNode::getDoubleValue () const
 {
+  DO_TRACE_READ(DOUBLE);
+  TEST_READ(0.0L);
   switch (_type) {
   case ALIAS:
     return _value.alias->getDoubleValue();
@@ -576,91 +783,70 @@ SGValue::getDoubleValue () const
   case DOUBLE:
     return GET_DOUBLE;
   case STRING:
-  case UNKNOWN:
+  case UNSPECIFIED:
     return strtod(GET_STRING.c_str(), 0);
+  case NONE:
+  default:
+    return 0.0L;
   }
-
-  return 0.0;
 }
 
-
-/**
- * Get a string value.
- */
 string
-SGValue::getStringValue () const
+SGPropertyNode::getStringValue () const
 {
-  char buf[128];
+  DO_TRACE_READ(STRING);
+  return get_string();
+}
+
+bool
+SGPropertyNode::setBoolValue (bool value)
+{
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE || _type == UNSPECIFIED) {
+    clear_value();
+    _value.bool_val = new SGRawValueInternal<bool>;
+    _type = BOOL;
+  }
 
   switch (_type) {
   case ALIAS:
-    return _value.alias->getStringValue();
+    result = _value.alias->setBoolValue(value);
+    break;
   case BOOL:
-    if (GET_BOOL)
-      return "true";
-    else
-      return "false";
-  case INT:
-    sprintf(buf, "%d", GET_INT);
-    return buf;
-  case LONG:
-    sprintf(buf, "%ld", GET_LONG);
-    return buf;
-  case FLOAT:
-    sprintf(buf, "%f", GET_FLOAT);
-    return buf;
-  case DOUBLE:
-    sprintf(buf, "%f", GET_DOUBLE);
-    return buf;
-  case STRING:
-  case UNKNOWN:
-    return GET_STRING;
-  }
-
-  return "";
-}
-
-
-/**
- * Set a bool value.
- */
-bool
-SGValue::setBoolValue (bool value)
-{
-  if (_type == UNKNOWN) {
-    clear_value();
-    _value.bool_val = new SGRawValueInternal<bool>;
-    _type = BOOL;
-  }
-
-  switch (_type) {
-  case ALIAS:
-    return _value.alias->setBoolValue(value);
-  case BOOL:
-    return SET_BOOL(value);
+    result = SET_BOOL(value);
+    break;
   case INT:
-    return SET_INT(int(value));
+    result = SET_INT(int(value));
+    break;
   case LONG:
-    return SET_LONG(long(value));
+    result = SET_LONG(long(value));
+    break;
   case FLOAT:
-    return SET_FLOAT(float(value));
+    result = SET_FLOAT(float(value));
+    break;
   case DOUBLE:
-    return SET_DOUBLE(double(value));
+    result = SET_DOUBLE(double(value));
+    break;
   case STRING:
-    return SET_STRING(value ? "true" : "false");
+  case UNSPECIFIED:
+    result = SET_STRING(value ? "true" : "false");
+    break;
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(BOOL);
+  return result;
 }
 
-
-/**
- * Set an int value.
- */
 bool
-SGValue::setIntValue (int value)
+SGPropertyNode::setIntValue (int value)
 {
-  if (_type == UNKNOWN) {
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE || _type == UNSPECIFIED) {
     clear_value();
     _value.int_val = new SGRawValueInternal<int>;
     _type = INT;
@@ -668,35 +854,45 @@ SGValue::setIntValue (int value)
 
   switch (_type) {
   case ALIAS:
-    return _value.alias->setIntValue(value);
+    result = _value.alias->setIntValue(value);
+    break;
   case BOOL:
-    return SET_BOOL(value == 0 ? false : true);
+    result = SET_BOOL(value == 0 ? false : true);
+    break;
   case INT:
-    return SET_INT(value);
+    result = SET_INT(value);
+    break;
   case LONG:
-    return SET_LONG(long(value));
+    result = SET_LONG(long(value));
+    break;
   case FLOAT:
-    return SET_FLOAT(float(value));
+    result = SET_FLOAT(float(value));
+    break;
   case DOUBLE:
-    return SET_DOUBLE(double(value));
-  case STRING: {
+    result = SET_DOUBLE(double(value));
+    break;
+  case STRING:
+  case UNSPECIFIED: {
     char buf[128];
     sprintf(buf, "%d", value);
-    return SET_STRING(buf);
+    result = SET_STRING(buf);
+    break;
   }
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(INT);
+  return result;
 }
 
-
-/**
- * Set a long value.
- */
 bool
-SGValue::setLongValue (long value)
+SGPropertyNode::setLongValue (long value)
 {
-  if (_type == UNKNOWN) {
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE || _type == UNSPECIFIED) {
     clear_value();
     _value.long_val = new SGRawValueInternal<long>;
     _type = LONG;
@@ -704,35 +900,45 @@ SGValue::setLongValue (long value)
 
   switch (_type) {
   case ALIAS:
-    return _value.alias->setLongValue(value);
+    result = _value.alias->setLongValue(value);
+    break;
   case BOOL:
-    return SET_BOOL(value == 0L ? false : true);
+    result = SET_BOOL(value == 0L ? false : true);
+    break;
   case INT:
-    return SET_INT(int(value));
+    result = SET_INT(int(value));
+    break;
   case LONG:
-    return SET_LONG(value);
+    result = SET_LONG(value);
+    break;
   case FLOAT:
-    return SET_FLOAT(float(value));
+    result = SET_FLOAT(float(value));
+    break;
   case DOUBLE:
-    return SET_DOUBLE(double(value));
-  case STRING: {
+    result = SET_DOUBLE(double(value));
+    break;
+  case STRING:
+  case UNSPECIFIED: {
     char buf[128];
-    sprintf(buf, "%d", value);
-    return SET_STRING(buf);
+    sprintf(buf, "%ld", value);
+    result = SET_STRING(buf);
+    break;
   }
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(LONG);
+  return result;
 }
 
-
-/**
- * Set a float value.
- */
 bool
-SGValue::setFloatValue (float value)
+SGPropertyNode::setFloatValue (float value)
 {
-  if (_type == UNKNOWN) {
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE || _type == UNSPECIFIED) {
     clear_value();
     _value.float_val = new SGRawValueInternal<float>;
     _type = FLOAT;
@@ -740,35 +946,45 @@ SGValue::setFloatValue (float value)
 
   switch (_type) {
   case ALIAS:
-    return _value.alias->setFloatValue(value);
+    result = _value.alias->setFloatValue(value);
+    break;
   case BOOL:
-    return SET_BOOL(value == 0.0 ? false : true);
+    result = SET_BOOL(value == 0.0 ? false : true);
+    break;
   case INT:
-    return SET_INT(int(value));
+    result = SET_INT(int(value));
+    break;
   case LONG:
-    return SET_LONG(long(value));
+    result = SET_LONG(long(value));
+    break;
   case FLOAT:
-    return SET_FLOAT(value);
+    result = SET_FLOAT(value);
+    break;
   case DOUBLE:
-    return SET_DOUBLE(double(value));
-  case STRING: {
+    result = SET_DOUBLE(double(value));
+    break;
+  case STRING:
+  case UNSPECIFIED: {
     char buf[128];
     sprintf(buf, "%f", value);
-    return SET_STRING(buf);
+    result = SET_STRING(buf);
+    break;
   }
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(FLOAT);
+  return result;
 }
 
-
-/**
- * Set a double value.
- */
 bool
-SGValue::setDoubleValue (double value)
+SGPropertyNode::setDoubleValue (double value)
 {
-  if (_type == UNKNOWN) {
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE || _type == UNSPECIFIED) {
     clear_value();
     _value.double_val = new SGRawValueInternal<double>;
     _type = DOUBLE;
@@ -776,35 +992,45 @@ SGValue::setDoubleValue (double value)
 
   switch (_type) {
   case ALIAS:
-    return _value.alias->setDoubleValue(value);
+    result = _value.alias->setDoubleValue(value);
+    break;
   case BOOL:
-    return SET_BOOL(value == 0.0L ? false : true);
+    result = SET_BOOL(value == 0.0L ? false : true);
+    break;
   case INT:
-    return SET_INT(int(value));
+    result = SET_INT(int(value));
+    break;
   case LONG:
-    return SET_LONG(long(value));
+    result = SET_LONG(long(value));
+    break;
   case FLOAT:
-    return SET_FLOAT(float(value));
+    result = SET_FLOAT(float(value));
+    break;
   case DOUBLE:
-    return SET_DOUBLE(value);
-  case STRING: {
+    result = SET_DOUBLE(value);
+    break;
+  case STRING:
+  case UNSPECIFIED: {
     char buf[128];
-    sprintf(buf, "%lf", value);
-    return SET_STRING(buf);
+    sprintf(buf, "%f", value);
+    result = SET_STRING(buf);
+    break;
   }
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(DOUBLE);
+  return result;
 }
 
-
-/**
- * Set a string value.
- */
 bool
-SGValue::setStringValue (string value)
+SGPropertyNode::setStringValue (string value)
 {
-  if (_type == UNKNOWN) {
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE || _type == UNSPECIFIED) {
     clear_value();
     _value.string_val = new SGRawValueInternal<string>;
     _type = STRING;
@@ -812,231 +1038,217 @@ SGValue::setStringValue (string value)
 
   switch (_type) {
   case ALIAS:
-    return _value.alias->setStringValue(value);
+    result = _value.alias->setStringValue(value);
+    break;
   case BOOL:
-    return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+    result = SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+    break;
   case INT:
-    return SET_INT(atoi(value.c_str()));
+    result = SET_INT(atoi(value.c_str()));
+    break;
   case LONG:
-    return SET_LONG(strtol(value.c_str(), 0, 0));
+    result = SET_LONG(strtol(value.c_str(), 0, 0));
+    break;
   case FLOAT:
-    return SET_FLOAT(atof(value.c_str()));
+    result = SET_FLOAT(atof(value.c_str()));
+    break;
   case DOUBLE:
-    return SET_DOUBLE(strtod(value.c_str(), 0));
+    result = SET_DOUBLE(strtod(value.c_str(), 0));
+    break;
   case STRING:
-    return SET_STRING(value);
+  case UNSPECIFIED:
+    result = SET_STRING(value);
+    break;
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(STRING);
+  return result;
 }
 
-
-/**
- * Set a value of unknown type (stored as a string).
- */
 bool
-SGValue::setUnknownValue (string value)
+SGPropertyNode::setUnspecifiedValue (string value)
 {
+  bool result = false;
+  TEST_WRITE;
+  if (_type == NONE) {
+    clear_value();
+    _value.string_val = new SGRawValueInternal<string>;
+    _type = UNSPECIFIED;
+  }
+
   switch (_type) {
   case ALIAS:
-    return _value.alias->setUnknownValue(value);
+    result = _value.alias->setUnspecifiedValue(value);
+    break;
   case BOOL:
-    return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+    result = SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+    break;
   case INT:
-    return SET_INT(atoi(value.c_str()));
+    result = SET_INT(atoi(value.c_str()));
+    break;
   case LONG:
-    return SET_LONG(strtol(value.c_str(), 0, 0));
+    result = SET_LONG(strtol(value.c_str(), 0, 0));
+    break;
   case FLOAT:
-    return SET_FLOAT(atof(value.c_str()));
+    result = SET_FLOAT(atof(value.c_str()));
+    break;
   case DOUBLE:
-    return SET_DOUBLE(strtod(value.c_str(), 0));
+    result = SET_DOUBLE(strtod(value.c_str(), 0));
+    break;
   case STRING:
-  case UNKNOWN:
-    return SET_STRING(value);
+  case UNSPECIFIED:
+    result = SET_STRING(value);
+    break;
+  case NONE:
+  default:
+    break;
   }
 
-  return false;
+  DO_TRACE_WRITE(UNSPECIFIED);
+  return result;
 }
 
-
-/**
- * Tie a bool value.
- */
 bool
-SGValue::tie (const SGRawValue<bool> &value, bool use_default)
+SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
 {
-  if (_type == ALIAS)
-    return _value.alias->tie(value, use_default);
-  else if (_tied)
+  if (_type == ALIAS || _tied)
     return false;
 
   bool old_val = false;
-  if (use_default)
+  if (useDefault)
     old_val = getBoolValue();
 
   clear_value();
   _type = BOOL;
   _tied = true;
-  _value.bool_val = value.clone();
+  _value.bool_val = rawValue.clone();
 
-  if (use_default)
+  if (useDefault)
     setBoolValue(old_val);
 
   return true;
 }
 
-
-/**
- * Tie an int value.
- */
 bool
-SGValue::tie (const SGRawValue<int> &value, bool use_default)
+SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
 {
-  if (_type == ALIAS)
-    return _value.alias->tie(value, use_default);
-  else if (_tied)
+  if (_type == ALIAS || _tied)
     return false;
 
   int old_val = 0;
-  if (use_default)
+  if (useDefault)
     old_val = getIntValue();
 
   clear_value();
   _type = INT;
   _tied = true;
-  _value.int_val = value.clone();
+  _value.int_val = rawValue.clone();
 
-  if (use_default)
+  if (useDefault)
     setIntValue(old_val);
 
   return true;
 }
 
-
-/**
- * Tie a long value.
- */
 bool
-SGValue::tie (const SGRawValue<long> &value, bool use_default)
+SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
 {
-  if (_type == ALIAS)
-    return _value.alias->tie(value, use_default);
-  else if (_tied)
+  if (_type == ALIAS || _tied)
     return false;
 
   long old_val;
-  if (use_default)
+  if (useDefault)
     old_val = getLongValue();
 
   clear_value();
   _type = LONG;
   _tied = true;
-  _value.long_val = value.clone();
+  _value.long_val = rawValue.clone();
 
-  if (use_default)
+  if (useDefault)
     setLongValue(old_val);
 
   return true;
 }
 
-
-/**
- * Tie a float value.
- */
 bool
-SGValue::tie (const SGRawValue<float> &value, bool use_default)
+SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
 {
-  if (_type == ALIAS)
-    return _value.alias->tie(value, use_default);
-  else if (_tied)
+  if (_type == ALIAS || _tied)
     return false;
 
   float old_val = 0.0;
-  if (use_default)
+  if (useDefault)
     old_val = getFloatValue();
 
   clear_value();
   _type = FLOAT;
   _tied = true;
-  _value.float_val = value.clone();
+  _value.float_val = rawValue.clone();
 
-  if (use_default)
+  if (useDefault)
     setFloatValue(old_val);
 
   return true;
 }
 
-
-/**
- * Tie a double value.
- */
 bool
-SGValue::tie (const SGRawValue<double> &value, bool use_default)
+SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
 {
-  if (_type == ALIAS)
-    return _value.alias->tie(value, use_default);
-  else if (_tied)
+  if (_type == ALIAS || _tied)
     return false;
 
   double old_val = 0.0;
-  if (use_default)
+  if (useDefault)
     old_val = getDoubleValue();
 
   clear_value();
   _type = DOUBLE;
   _tied = true;
-  _value.double_val = value.clone();
+  _value.double_val = rawValue.clone();
 
-  if (use_default)
+  if (useDefault)
     setDoubleValue(old_val);
 
   return true;
-}
 
+}
 
-/**
- * Tie a string value.
- */
 bool
-SGValue::tie (const SGRawValue<string> &value, bool use_default)
+SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
 {
-  if (_type == ALIAS)
-    return _value.alias->tie(value, use_default);
-  else if (_tied)
+  if (_type == ALIAS || _tied)
     return false;
 
   string old_val;
-  if (use_default)
+  if (useDefault)
     old_val = getStringValue();
 
   clear_value();
   _type = STRING;
   _tied = true;
-  _value.string_val = value.clone();
+  _value.string_val = rawValue.clone();
 
-  if (use_default)
+  if (useDefault)
     setStringValue(old_val);
 
   return true;
 }
 
-
-/**
- * Untie a value.
- */
 bool
-SGValue::untie ()
+SGPropertyNode::untie ()
 {
   if (!_tied)
     return false;
 
   switch (_type) {
-  case ALIAS: {
-    return _value.alias->untie();
-  }
   case BOOL: {
     bool val = getBoolValue();
     clear_value();
+    _type = BOOL;
     _value.bool_val = new SGRawValueInternal<bool>;
     SET_BOOL(val);
     break;
@@ -1044,6 +1256,7 @@ SGValue::untie ()
   case INT: {
     int val = getIntValue();
     clear_value();
+    _type = INT;
     _value.int_val = new SGRawValueInternal<int>;
     SET_INT(val);
     break;
@@ -1051,6 +1264,7 @@ SGValue::untie ()
   case LONG: {
     long val = getLongValue();
     clear_value();
+    _type = LONG;
     _value.long_val = new SGRawValueInternal<long>;
     SET_LONG(val);
     break;
@@ -1058,6 +1272,7 @@ SGValue::untie ()
   case FLOAT: {
     float val = getFloatValue();
     clear_value();
+    _type = FLOAT;
     _value.float_val = new SGRawValueInternal<float>;
     SET_FLOAT(val);
     break;
@@ -1065,451 +1280,29 @@ SGValue::untie ()
   case DOUBLE: {
     double val = getDoubleValue();
     clear_value();
+    _type = DOUBLE;
     _value.double_val = new SGRawValueInternal<double>;
     SET_DOUBLE(val);
     break;
   }
-  case STRING: {
+  case STRING:
+  case UNSPECIFIED: {
     string val = getStringValue();
     clear_value();
+    _type = STRING;
     _value.string_val = new SGRawValueInternal<string>;
     SET_STRING(val);
     break;
   }
+  case NONE:
+  default:
+    break;
   }
 
   _tied = false;
   return true;
 }
 
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of SGPropertyNode.
-////////////////////////////////////////////////////////////////////////
-
-
-/**
- * Utility function: given a value, find the property node.
- */
-static SGPropertyNode *
-find_node_by_value (SGPropertyNode * start_node, const SGValue * value)
-{
-  if (start_node->getValue() == value) {
-    return start_node;
-  } else for (int i = 0; i < start_node->nChildren(); i++) {
-    SGPropertyNode * child =
-      find_node_by_value(start_node->getChild(i), value);
-    if (child != 0)
-      return child;
-  }
-  return 0;
-}
-
-
-/**
- * Default constructor: always creates a root node.
- */
-SGPropertyNode::SGPropertyNode ()
-  : _value(0), _name(""), _index(0), _parent(0), _target(0)
-{
-}
-
-
-/**
- * Convenience constructor.
- */
-SGPropertyNode::SGPropertyNode (const string &name,
-                               int index, SGPropertyNode * parent)
-  : _value(0), _name(name), _index(index), _parent(parent), _target(0)
-{
-}
-
-
-/**
- * Destructor.
- */
-SGPropertyNode::~SGPropertyNode ()
-{
-  delete _value;
-  for (int i = 0; i < (int)_children.size(); i++)
-    delete _children[i];
-}
-
-
-/**
- * Get a value, optionally creating it if not present.
- */
-SGValue *
-SGPropertyNode::getValue (bool create)
-{
-  if (_value == 0 && create)
-    _value = new SGValue();
-  return _value;
-}
-
-
-/**
- * Alias to another node.
- */
-bool
-SGPropertyNode::alias (SGPropertyNode * target)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  _target = target;
-  return _value->alias(target->getValue(true));
-}
-
-
-/**
- * Alias to another node by path.
- */
-bool
-SGPropertyNode::alias (const string &path)
-{
-  return alias(getNode(path, true));
-}
-
-
-/**
- * Remove an alias.
- */
-bool
-SGPropertyNode::unalias ()
-{
-  _target = 0;
-  return (_value != 0 ? _value->unalias() : false);
-}
-
-
-/**
- * Test whether this node is aliased.
- */
-bool
-SGPropertyNode::isAlias () const
-{
-  return (_value != 0 ? _value->isAlias() : false);
-}
-
-
-/**
- * Get the target of an alias.
- *
- * This is tricky, because it is actually the value that is aliased,
- * and someone could realias or unalias the value directly without
- * going through the property node.  The node caches its best guess,
- * but it may have to search the whole property tree.
- *
- * @return The target node for the alias, or 0 if the node is not aliased.
- */
-SGPropertyNode *
-SGPropertyNode::getAliasTarget ()
-{
-  if (_value == 0 || !_value->isAlias()) {
-    return 0;
-  } else if (_target != 0 && _target->getValue() == _value->getAlias()) {
-    return _target;
-  } else {
-    _target = find_node_by_value(getRootNode(), _value->getAlias());
-  }
-}
-
-
-const SGPropertyNode *
-SGPropertyNode::getAliasTarget () const
-{
-  if (_value == 0 || !_value->isAlias()) {
-    return 0;
-  } else if (_target != 0 && _target->getValue() == _value->getAlias()) {
-    return _target;
-  } else {
-                               // FIXME: const cast
-    _target =
-      find_node_by_value((SGPropertyNode *)getRootNode(), _value->getAlias());
-  }
-}
-
-
-/**
- * Get a non-const child by index.
- */
-SGPropertyNode *
-SGPropertyNode::getChild (int position)
-{
-  if (position >= 0 && position < nChildren())
-    return _children[position];
-  else
-    return 0;
-}
-
-
-/**
- * Get a const child by index.
- */
-const SGPropertyNode *
-SGPropertyNode::getChild (int position) const
-{
-  if (position >= 0 && position < nChildren())
-    return _children[position];
-  else
-    return 0;
-}
-
-
-/**
- * Get a non-const child by name and index, creating if necessary.
- */
-SGPropertyNode *
-SGPropertyNode::getChild (const string &name, int index, bool create)
-{
-  int pos = find_child(name, index, _children);
-  if (pos >= 0) {
-    return _children[pos];
-  } else if (create) {
-    _children.push_back(new SGPropertyNode(name, index, this));
-    return _children[_children.size()-1];
-  } else {
-    return 0;
-  }
-}
-
-
-/**
- * Get a const child by name and index.
- */
-const SGPropertyNode *
-SGPropertyNode::getChild (const string &name, int index) const
-{
-  int pos = find_child(name, index, _children);
-  if (pos >= 0)
-    return _children[pos];
-  else
-    return 0;
-}
-
-
-/**
- * Get all children with the same name (but different indices).
- */
-vector<SGPropertyNode *>
-SGPropertyNode::getChildren (const string &name)
-{
-  vector<SGPropertyNode *> children;
-  int max = _children.size();
-
-  for (int i = 0; i < max; i++)
-    if (_children[i]->getName() == name)
-      children.push_back(_children[i]);
-
-  sort(children.begin(), children.end(), CompareIndices());
-  return children;
-}
-
-
-/**
- * Get all children const with the same name (but different indices).
- */
-vector<const SGPropertyNode *>
-SGPropertyNode::getChildren (const string &name) const
-{
-  vector<const SGPropertyNode *> children;
-  int max = _children.size();
-
-  for (int i = 0; i < max; i++)
-    if (_children[i]->getName() == name)
-      children.push_back(_children[i]);
-
-  sort(children.begin(), children.end(), CompareIndices());
-  return children;
-}
-
-
-string
-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;
-  }
-  return path;
-}
-
-SGValue::Type
-SGPropertyNode::getType () const
-{
-  if (_value != 0)
-    return _value->getType();
-  else
-    return SGValue::UNKNOWN;
-}
-
-
-bool 
-SGPropertyNode::getBoolValue () const
-{
-  return (_value == 0 ? SGRawValue<bool>::DefaultValue
-         : _value->getBoolValue());
-}
-
-int 
-SGPropertyNode::getIntValue () const
-{
-  return (_value == 0 ? SGRawValue<int>::DefaultValue
-         : _value->getIntValue());
-}
-
-long 
-SGPropertyNode::getLongValue () const
-{
-  return (_value == 0 ? SGRawValue<long>::DefaultValue
-         : _value->getLongValue());
-}
-
-float 
-SGPropertyNode::getFloatValue () const
-{
-  return (_value == 0 ? SGRawValue<float>::DefaultValue
-         : _value->getFloatValue());
-}
-
-double 
-SGPropertyNode::getDoubleValue () const
-{
-  return (_value == 0 ? SGRawValue<double>::DefaultValue
-         : _value->getDoubleValue());
-}
-
-string
-SGPropertyNode::getStringValue () const
-{
-  return (_value == 0 ? SGRawValue<string>::DefaultValue
-         : _value->getStringValue());
-}
-
-bool
-SGPropertyNode::setBoolValue (bool val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setBoolValue(val);
-}
-
-bool
-SGPropertyNode::setIntValue (int val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setIntValue(val);
-}
-
-bool
-SGPropertyNode::setLongValue (long val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setLongValue(val);
-}
-
-bool
-SGPropertyNode::setFloatValue (float val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setFloatValue(val);
-}
-
-bool
-SGPropertyNode::setDoubleValue (double val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setDoubleValue(val);
-}
-
-bool
-SGPropertyNode::setStringValue (string val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setStringValue(val);
-}
-
-bool
-SGPropertyNode::setUnknownValue (string val)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->setUnknownValue(val);
-}
-
-bool
-SGPropertyNode::isTied () const
-{
-  return (_value == 0 ? false : _value->isTied());
-}
-
-bool
-SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->tie(rawValue, useDefault);
-}
-
-bool
-SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->tie(rawValue, useDefault);
-}
-
-bool
-SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->tie(rawValue, useDefault);
-}
-
-bool
-SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->tie(rawValue, useDefault);
-}
-
-bool
-SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->tie(rawValue, useDefault);
-}
-
-bool
-SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
-{
-  if (_value == 0)
-    _value = new SGValue();
-  return _value->tie(rawValue, useDefault);
-}
-
-bool
-SGPropertyNode::untie ()
-{
-  return (_value == 0 ? false : _value->untie());
-}
-
 SGPropertyNode *
 SGPropertyNode::getRootNode ()
 {
@@ -1530,21 +1323,42 @@ SGPropertyNode::getRootNode () const
 
 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
 ////////////////////////////////////////////////////////////////////////
@@ -1563,38 +1377,14 @@ SGPropertyNode::hasValue (const string &relative_path) const
 }
 
 
-/**
- * Get the value for another node.
- */
-SGValue *
-SGPropertyNode::getValue (const string &relative_path, bool create)
-{
-  SGPropertyNode * node = getNode(relative_path, create);
-  if (node != 0 && !node->hasValue())
-    node->setUnknownValue("");
-  return (node == 0 ? 0 : node->getValue(create));
-}
-
-
-/**
- * Get the value for another node.
- */
-const SGValue *
-SGPropertyNode::getValue (const string &relative_path) const
-{
-  const SGPropertyNode * node = getNode(relative_path);
-  return (node == 0 ? 0 : node->getValue());
-}
-
-
 /**
  * Get the value type for another node.
  */
-SGValue::Type
+SGPropertyNode::Type
 SGPropertyNode::getType (const string &relative_path) const
 {
   const SGPropertyNode * node = getNode(relative_path);
-  return (node == 0 ? SGValue::UNKNOWN : node->getType());
+  return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
 }
 
 
@@ -1734,9 +1524,9 @@ SGPropertyNode::setStringValue (const string &relative_path, string value)
  * Set an unknown value for another node.
  */
 bool
-SGPropertyNode::setUnknownValue (const string &relative_path, string value)
+SGPropertyNode::setUnspecifiedValue (const string &relative_path, string value)
 {
-  return getNode(relative_path, true)->setUnknownValue(value);
+  return getNode(relative_path, true)->setUnspecifiedValue(value);
 }