//
// $Id$
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
#include <simgear/compiler.h>
+#include <simgear/debug/logstream.hxx>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include "props.hxx"
-#if !defined(FG_HAVE_NATIVE_SGI_COMPILERS)
-FG_USING_STD(cerr);
-FG_USING_STD(endl);
-#endif
-FG_USING_STD(sort);
+SG_USING_STD(sort);
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Local classes.
+////////////////////////////////////////////////////////////////////////
+
+/**
+ * Comparator class for sorting by index.
+ */
+class CompareIndices
+{
+public:
+ int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
+ return (n1->getIndex() < n2->getIndex());
+ }
+};
\f
// 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())
#define GET_FLOAT (_value.float_val->getValue())
#define GET_DOUBLE (_value.double_val->getValue())
#define GET_STRING (_value.string_val->getValue())
#define SET_BOOL(val) (_value.bool_val->setValue(val))
#define SET_INT(val) (_value.int_val->setValue(val))
+#define SET_LONG(val) (_value.long_val->setValue(val))
#define SET_FLOAT(val) (_value.float_val->setValue(val))
#define SET_DOUBLE(val) (_value.double_val->setValue(val))
#define SET_STRING(val) (_value.string_val->setValue(val))
const bool SGRawValue<bool>::DefaultValue = false;
const int SGRawValue<int>::DefaultValue = 0;
+const long SGRawValue<long>::DefaultValue = 0L;
const float SGRawValue<float>::DefaultValue = 0.0;
const double SGRawValue<double>::DefaultValue = 0.0L;
const string SGRawValue<string>::DefaultValue = "";
int position,
bool create)
{
+ // Run off the end of the list
if (current == 0) {
return 0;
}
- else if (position >= components.size()) {
+ // Success! This is the one we want.
+ else if (position >= (int)components.size()) {
return current;
}
+ // Empty component means root.
else if (components[position].name == "") {
return find_node(current->getRootNode(), components, position + 1, create);
}
+ // . means current directory
else if (components[position].name == ".") {
return find_node(current, components, position + 1, create);
}
+ // .. means parent directory
else if (components[position].name == "..") {
SGPropertyNode * parent = current->getParent();
if (parent == 0)
return find_node(parent, components, position + 1, create);
}
+ // Otherwise, a child name
else {
SGPropertyNode * child =
current->getChild(components[position].name,
\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),
+ _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
+ _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:
+ _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 = 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), _type(NONE),
+ _tied(false), _attr(READ|WRITE)
+{
+}
+
+
/**
* Destructor.
*/
-SGValue::~SGValue ()
+SGPropertyNode::~SGPropertyNode ()
{
+ for (int i = 0; i < (int)_children.size(); i++) {
+ delete _children[i];
+ }
clear_value();
}
* Delete and clear the current value.
*/
void
-SGValue::clear_value ()
+SGPropertyNode::clear_value ()
{
switch (_type) {
+ case NONE:
+ case ALIAS:
+ _value.alias = 0;
+ break;
case BOOL:
delete _value.bool_val;
_value.bool_val = 0;
delete _value.int_val;
_value.int_val = 0;
break;
+ case LONG:
+ delete _value.long_val;
+ _value.long_val = 0L;
+ break;
case FLOAT:
delete _value.float_val;
_value.float_val = 0;
_value.double_val = 0;
break;
case STRING:
- case UNKNOWN:
+ case UNSPECIFIED:
delete _value.string_val;
_value.string_val = 0;
break;
}
+ _type = NONE;
}
/**
- * Get a boolean value.
+ * Get the value as a string.
*/
-bool
-SGValue::getBoolValue () const
+string
+SGPropertyNode::get_string () const
{
+ TEST_READ("");
+ char buf[128];
+
switch (_type) {
+ case ALIAS:
+ return _value.alias->getStringValue();
case BOOL:
- return GET_BOOL;
+ if (GET_BOOL)
+ return "true";
+ else
+ return "false";
case INT:
- return GET_INT == 0 ? false : true;
+ sprintf(buf, "%d", GET_INT);
+ return buf;
+ case LONG:
+ sprintf(buf, "%ld", GET_LONG);
+ return buf;
case FLOAT:
- return GET_FLOAT == 0.0 ? false : true;
+ sprintf(buf, "%f", GET_FLOAT);
+ return buf;
case DOUBLE:
- return GET_DOUBLE == 0.0L ? false : true;
+ sprintf(buf, "%f", GET_DOUBLE);
+ return buf;
case STRING:
- case UNKNOWN:
- return (GET_STRING == "true" || getDoubleValue() != 0.0L);
+ case UNSPECIFIED:
+ return GET_STRING;
}
+
+ return ""; // if NONE
}
/**
- * Get an integer value.
+ * Trace a read access for a property.
*/
-int
-SGValue::getIntValue () const
+void
+SGPropertyNode::trace_read (SGPropertyNode::Type accessType) const
{
- switch (_type) {
- case BOOL:
- return (int)GET_BOOL;
- case INT:
- return GET_INT;
- case FLOAT:
- return (int)GET_FLOAT;
- case DOUBLE:
- return (int)GET_DOUBLE;
- case STRING:
- case UNKNOWN:
- return atoi(GET_STRING.c_str());
- }
+ SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath()
+ << ", value \"" << get_string() << '"');
}
/**
- * Get a float value.
+ * Trace a write access for a property.
*/
-float
-SGValue::getFloatValue () const
+void
+SGPropertyNode::trace_write (SGPropertyNode::Type accessType) const
{
- switch (_type) {
- case BOOL:
- return (float)GET_BOOL;
- case INT:
- return (float)GET_INT;
- case FLOAT:
- return GET_FLOAT;
- case DOUBLE:
- return GET_DOUBLE;
- case STRING:
- case UNKNOWN:
- return atof(GET_STRING.c_str());
- }
+ SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath()
+ << ", value\"" << get_string() << '"');
}
/**
- * Get a double value.
+ * Alias to another node.
*/
-double
-SGValue::getDoubleValue () const
+bool
+SGPropertyNode::alias (SGPropertyNode * target)
{
- switch (_type) {
- case BOOL:
- return (double)GET_BOOL;
- case INT:
- return (double)GET_INT;
- case FLOAT:
- return (double)GET_FLOAT;
- case DOUBLE:
- return GET_DOUBLE;
- case STRING:
- case UNKNOWN:
- return (double)atof(GET_STRING.c_str());
- }
+ if (target == 0 || _type == ALIAS || _tied)
+ return false;
+ clear_value();
+ _value.alias = target;
+ _type = ALIAS;
+ return true;
}
/**
- * Get a string value.
+ * Alias to another node by path.
*/
-string
-SGValue::getStringValue () const
+bool
+SGPropertyNode::alias (const string &path)
{
- char buf[128];
-
- switch (_type) {
- case BOOL:
- if (GET_BOOL)
- return "true";
- else
- return "false";
- case INT:
- sprintf(buf, "%d", GET_INT);
- return buf;
- case FLOAT:
- sprintf(buf, "%f", GET_FLOAT);
- return buf;
- case DOUBLE:
- sprintf(buf, "%lf", GET_DOUBLE);
- return buf;
- case STRING:
- case UNKNOWN:
- return GET_STRING;
- }
+ return alias(getNode(path, true));
}
/**
- * Set a bool value.
+ * Remove an alias.
*/
bool
-SGValue::setBoolValue (bool value)
+SGPropertyNode::unalias ()
{
- if (_type == UNKNOWN) {
- clear_value();
- _value.bool_val = new SGRawValueInternal<bool>;
- _type = BOOL;
- }
-
- switch (_type) {
- case BOOL:
- return SET_BOOL(value);
- case INT:
- return SET_INT((int)value);
- case FLOAT:
- return SET_FLOAT((float)value);
- case DOUBLE:
- return SET_DOUBLE((double)value);
- case STRING:
- return SET_STRING(value ? "true" : "false");
- }
-
- return false;
+ if (_type != ALIAS)
+ return false;
+ _type = NONE;
+ _value.alias = 0;
+ return true;
}
/**
- * Set an int value.
+ * Get the target of an alias.
*/
-bool
-SGValue::setIntValue (int value)
+SGPropertyNode *
+SGPropertyNode::getAliasTarget ()
{
- if (_type == UNKNOWN) {
- clear_value();
- _value.int_val = new SGRawValueInternal<int>;
- _type = INT;
- }
+ return (_type == ALIAS ? _value.alias : 0);
+}
- switch (_type) {
- case BOOL:
- return SET_BOOL(value == 0 ? false : true);
- case INT:
- return SET_INT(value);
- case FLOAT:
- return SET_FLOAT((float)value);
- case DOUBLE:
- return SET_DOUBLE((double)value);
- case STRING: {
- char buf[128];
- sprintf(buf, "%d", value);
- return SET_STRING(buf);
- }
- }
- return false;
+const SGPropertyNode *
+SGPropertyNode::getAliasTarget () const
+{
+ return (_type == ALIAS ? _value.alias : 0);
}
/**
- * Set a float value.
+ * Get a non-const child by index.
*/
-bool
-SGValue::setFloatValue (float value)
+SGPropertyNode *
+SGPropertyNode::getChild (int position)
{
- if (_type == UNKNOWN) {
- clear_value();
- _value.float_val = new SGRawValueInternal<float>;
- _type = FLOAT;
- }
-
- switch (_type) {
- case BOOL:
- return SET_BOOL(value == 0.0 ? false : true);
- case INT:
- return SET_INT((int)value);
- case FLOAT:
- return SET_FLOAT(value);
- case DOUBLE:
- return SET_DOUBLE((double)value);
- case STRING: {
- char buf[128];
- sprintf(buf, "%f", value);
- return SET_STRING(buf);
- }
- }
-
- return false;
+ if (position >= 0 && position < nChildren())
+ return _children[position];
+ else
+ return 0;
}
/**
- * Set a double value.
+ * Get a const child by index.
*/
-bool
-SGValue::setDoubleValue (double value)
+const SGPropertyNode *
+SGPropertyNode::getChild (int position) const
{
- if (_type == UNKNOWN) {
- clear_value();
- _value.double_val = new SGRawValueInternal<double>;
- _type = DOUBLE;
- }
-
- switch (_type) {
- case BOOL:
- return SET_BOOL(value == 0.0L ? false : true);
- case INT:
- return SET_INT((int)value);
- case FLOAT:
- return SET_FLOAT((float)value);
- case DOUBLE:
- return SET_DOUBLE(value);
- case STRING: {
- char buf[128];
- sprintf(buf, "%lf", value);
- return SET_STRING(buf);
- }
- }
-
- return false;
+ if (position >= 0 && position < nChildren())
+ return _children[position];
+ else
+ return 0;
}
/**
- * Set a string value.
+ * Get a non-const child by name and index, creating if necessary.
*/
-bool
-SGValue::setStringValue (string value)
+SGPropertyNode *
+SGPropertyNode::getChild (const string &name, int index, bool create)
{
- if (_type == UNKNOWN) {
- clear_value();
- _value.string_val = new SGRawValueInternal<string>;
- _type = STRING;
- }
-
- switch (_type) {
- case BOOL:
- return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
- case INT:
- return SET_INT(atoi(value.c_str()));
- case FLOAT:
- return SET_FLOAT(atof(value.c_str()));
- case DOUBLE:
- return SET_DOUBLE((double)atof(value.c_str()));
- case STRING:
- return SET_STRING(value);
+ 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;
}
-
- return false;
}
/**
- * Set a value of unknown type (stored as a string).
+ * Get a const child by name and index.
*/
-bool
-SGValue::setUnknownValue (string value)
+const SGPropertyNode *
+SGPropertyNode::getChild (const string &name, int index) const
{
- switch (_type) {
- case BOOL:
- return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
- case INT:
- return SET_INT(atoi(value.c_str()));
- case FLOAT:
- return SET_FLOAT(atof(value.c_str()));
- case DOUBLE:
- return SET_DOUBLE((double)atof(value.c_str()));
- case STRING:
- case UNKNOWN:
- return SET_STRING(value);
- }
-
- return false;
+ int pos = find_child(name, index, _children);
+ if (pos >= 0)
+ return _children[pos];
+ else
+ return 0;
}
/**
- * Tie a bool value.
+ * Get all children with the same name (but different indices).
*/
-bool
-SGValue::tie (const SGRawValue<bool> &value, bool use_default)
+vector<SGPropertyNode *>
+SGPropertyNode::getChildren (const string &name)
{
- if (_tied)
- return false;
-
- bool old_val;
- if (use_default)
- old_val = getBoolValue();
-
- clear_value();
- _type = BOOL;
- _tied = true;
- _value.bool_val = value.clone();
+ vector<SGPropertyNode *> children;
+ int max = _children.size();
- if (use_default)
- setBoolValue(old_val);
+ for (int i = 0; i < max; i++)
+ if (_children[i]->getName() == name)
+ children.push_back(_children[i]);
- return true;
+ sort(children.begin(), children.end(), CompareIndices());
+ return children;
}
/**
- * Tie an int value.
+ * Get all children const with the same name (but different indices).
*/
-bool
-SGValue::tie (const SGRawValue<int> &value, bool use_default)
+vector<const SGPropertyNode *>
+SGPropertyNode::getChildren (const string &name) const
{
- if (_tied)
- return false;
-
- int old_val;
- if (use_default)
- old_val = getIntValue();
-
- clear_value();
- _type = INT;
- _tied = true;
- _value.int_val = value.clone();
+ vector<const SGPropertyNode *> children;
+ int max = _children.size();
- if (use_default)
- setIntValue(old_val);
+ for (int i = 0; i < max; i++)
+ if (_children[i]->getName() == name)
+ children.push_back(_children[i]);
- return true;
+ sort(children.begin(), children.end(), CompareIndices());
+ return children;
}
-/**
- * Tie a float value.
- */
-bool
-SGValue::tie (const SGRawValue<float> &value, bool use_default)
+string
+SGPropertyNode::getPath (bool simplify) const
{
- if (_tied)
- return false;
-
- float old_val;
- if (use_default)
- old_val = getFloatValue();
-
- clear_value();
- _type = FLOAT;
- _tied = true;
- _value.float_val = value.clone();
+ if (_parent == 0)
+ return "";
- if (use_default)
- setFloatValue(old_val);
+ string path = _parent->getPath(simplify);
+ path += '/';
+ path += _name;
+ if (_index != 0 || !simplify) {
+ char buffer[128];
+ sprintf(buffer, "[%d]", _index);
+ path += buffer;
+ }
+ return path;
+}
- return true;
+SGPropertyNode::Type
+SGPropertyNode::getType () const
+{
+ if (_type == ALIAS)
+ return _value.alias->getType();
+ else
+ return _type;
}
-/**
- * Tie a double value.
- */
-bool
-SGValue::tie (const SGRawValue<double> &value, bool use_default)
+bool
+SGPropertyNode::getBoolValue () const
{
- if (_tied)
- return false;
-
- double old_val;
- if (use_default)
- old_val = getDoubleValue();
+ DO_TRACE_READ(BOOL);
+ TEST_READ(false);
+ switch (_type) {
+ case ALIAS:
+ return _value.alias->getBoolValue();
+ case BOOL:
+ return GET_BOOL;
+ case INT:
+ return GET_INT == 0 ? false : true;
+ case LONG:
+ return GET_LONG == 0L ? false : true;
+ case FLOAT:
+ return GET_FLOAT == 0.0 ? false : true;
+ case DOUBLE:
+ return GET_DOUBLE == 0.0L ? false : true;
+ case STRING:
+ case UNSPECIFIED:
+ return (GET_STRING == "true" || getDoubleValue() != 0.0L);
+ }
- clear_value();
- _type = DOUBLE;
- _tied = true;
- _value.double_val = value.clone();
+ return false; // if NONE
+}
- if (use_default)
- setDoubleValue(old_val);
+int
+SGPropertyNode::getIntValue () const
+{
+ DO_TRACE_READ(INT);
+ TEST_READ(0);
+ switch (_type) {
+ case ALIAS:
+ return _value.alias->getIntValue();
+ case BOOL:
+ return int(GET_BOOL);
+ case INT:
+ return GET_INT;
+ case LONG:
+ return int(GET_LONG);
+ case FLOAT:
+ return int(GET_FLOAT);
+ case DOUBLE:
+ return int(GET_DOUBLE);
+ case STRING:
+ case UNSPECIFIED:
+ return atoi(GET_STRING.c_str());
+ }
- return true;
+ return 0; // if NONE
}
-
-/**
- * Tie a string value.
- */
-bool
-SGValue::tie (const SGRawValue<string> &value, bool use_default)
+long
+SGPropertyNode::getLongValue () const
{
- if (_tied)
- return false;
+ DO_TRACE_READ(LONG);
+ TEST_READ(0L);
+ switch (_type) {
+ case ALIAS:
+ return _value.alias->getLongValue();
+ case BOOL:
+ return long(GET_BOOL);
+ case INT:
+ return long(GET_INT);
+ case LONG:
+ return GET_LONG;
+ case FLOAT:
+ return long(GET_FLOAT);
+ case DOUBLE:
+ return long(GET_DOUBLE);
+ case STRING:
+ case UNSPECIFIED:
+ return strtol(GET_STRING.c_str(), 0, 0);
+ }
- string old_val;
- if (use_default)
- old_val = getStringValue();
+ return 0L; // if NONE
+}
- clear_value();
- _type = STRING;
- _tied = true;
- _value.string_val = value.clone();
+float
+SGPropertyNode::getFloatValue () const
+{
+ DO_TRACE_READ(FLOAT);
+ TEST_READ(0.0);
+ switch (_type) {
+ case ALIAS:
+ return _value.alias->getFloatValue();
+ case BOOL:
+ return float(GET_BOOL);
+ case INT:
+ return float(GET_INT);
+ case LONG:
+ return float(GET_LONG);
+ case FLOAT:
+ return GET_FLOAT;
+ case DOUBLE:
+ return float(GET_DOUBLE);
+ case STRING:
+ case UNSPECIFIED:
+ return atof(GET_STRING.c_str());
+ }
- if (use_default)
- setStringValue(old_val);
+ return 0.0; // if NONE
+}
- return true;
+double
+SGPropertyNode::getDoubleValue () const
+{
+ DO_TRACE_READ(DOUBLE);
+ TEST_READ(0.0L);
+ switch (_type) {
+ case ALIAS:
+ return _value.alias->getDoubleValue();
+ case BOOL:
+ return double(GET_BOOL);
+ case INT:
+ return double(GET_INT);
+ case LONG:
+ return double(GET_LONG);
+ case FLOAT:
+ return double(GET_FLOAT);
+ case DOUBLE:
+ return GET_DOUBLE;
+ case STRING:
+ case UNSPECIFIED:
+ return strtod(GET_STRING.c_str(), 0);
+ }
+
+ return 0.0L; // if NONE
}
+string
+SGPropertyNode::getStringValue () const
+{
+ DO_TRACE_READ(STRING);
+ return get_string();
+}
-/**
- * Untie a value.
- */
bool
-SGValue::untie ()
+SGPropertyNode::setBoolValue (bool value)
{
- if (!_tied)
- return false;
-
- switch (_type) {
- case BOOL: {
- bool val = getBoolValue();
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE || _type == UNSPECIFIED) {
clear_value();
_value.bool_val = new SGRawValueInternal<bool>;
- SET_BOOL(val);
+ _type = BOOL;
+ }
+
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setBoolValue(value);
+ break;
+ case BOOL:
+ result = SET_BOOL(value);
+ break;
+ case INT:
+ result = SET_INT(int(value));
+ break;
+ case LONG:
+ result = SET_LONG(long(value));
+ break;
+ case FLOAT:
+ result = SET_FLOAT(float(value));
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(double(value));
+ break;
+ case STRING:
+ result = SET_STRING(value ? "true" : "false");
break;
}
- case INT: {
- int val = getIntValue();
+
+ DO_TRACE_WRITE(BOOL);
+ return result;
+}
+
+bool
+SGPropertyNode::setIntValue (int value)
+{
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE || _type == UNSPECIFIED) {
clear_value();
_value.int_val = new SGRawValueInternal<int>;
- SET_INT(val);
- break;
+ _type = INT;
}
- case FLOAT: {
- float val = getFloatValue();
- clear_value();
- _value.float_val = new SGRawValueInternal<float>;
- SET_FLOAT(val);
+
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setIntValue(value);
break;
- }
- case DOUBLE: {
- double val = getDoubleValue();
- clear_value();
- _value.double_val = new SGRawValueInternal<double>;
- SET_DOUBLE(val);
+ case BOOL:
+ result = SET_BOOL(value == 0 ? false : true);
+ break;
+ case INT:
+ result = SET_INT(value);
+ break;
+ case LONG:
+ result = SET_LONG(long(value));
+ break;
+ case FLOAT:
+ result = SET_FLOAT(float(value));
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(double(value));
break;
- }
case STRING: {
- string val = getStringValue();
- clear_value();
- _value.string_val = new SGRawValueInternal<string>;
- SET_STRING(val);
+ char buf[128];
+ sprintf(buf, "%d", value);
+ result = SET_STRING(buf);
break;
}
}
- _tied = false;
- return true;
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of SGPropertyNode.
-////////////////////////////////////////////////////////////////////////
-
-
-/**
- * Default constructor: always creates a root node.
- */
-SGPropertyNode::SGPropertyNode ()
- : _value(0), _name(""), _index(0), _parent(0)
-{
+ DO_TRACE_WRITE(INT);
+ return result;
}
-
-/**
- * Convenience constructor.
- */
-SGPropertyNode::SGPropertyNode (const string &name,
- int index, SGPropertyNode * parent)
- : _value(0), _name(name), _index(index), _parent(parent)
+bool
+SGPropertyNode::setLongValue (long value)
{
-}
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE || _type == UNSPECIFIED) {
+ clear_value();
+ _value.long_val = new SGRawValueInternal<long>;
+ _type = LONG;
+ }
-SGPropertyNode::~SGPropertyNode ()
-{
- delete _value;
- for (int i = 0; i < _children.size(); i++)
- delete _children[i];
-}
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setLongValue(value);
+ break;
+ case BOOL:
+ result = SET_BOOL(value == 0L ? false : true);
+ break;
+ case INT:
+ result = SET_INT(int(value));
+ break;
+ case LONG:
+ result = SET_LONG(value);
+ break;
+ case FLOAT:
+ result = SET_FLOAT(float(value));
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(double(value));
+ break;
+ case STRING: {
+ char buf[128];
+ sprintf(buf, "%d", value);
+ result = SET_STRING(buf);
+ break;
+ }
+ }
-SGPropertyNode *
-SGPropertyNode::getChild (int position)
-{
- if (position >= 0 && position < nChildren())
- return _children[position];
- else
- return 0;
+ DO_TRACE_WRITE(LONG);
+ return result;
}
-const SGPropertyNode *
-SGPropertyNode::getChild (int position) const
+bool
+SGPropertyNode::setFloatValue (float value)
{
- if (position >= 0 && position < nChildren())
- return _children[position];
- else
- return 0;
-}
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE || _type == UNSPECIFIED) {
+ clear_value();
+ _value.float_val = new SGRawValueInternal<float>;
+ _type = FLOAT;
+ }
-SGPropertyNode *
-SGPropertyNode::getChild (const string &name, int index, bool create)
-{
- int pos = find_child(name, index, _children);
- if (pos >= 0) {
- return getChild(pos);
- } else if (create) {
- _children.push_back(new SGPropertyNode(name, index, this));
- return _children[_children.size()-1];
- } else {
- return 0;
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setFloatValue(value);
+ break;
+ case BOOL:
+ result = SET_BOOL(value == 0.0 ? false : true);
+ break;
+ case INT:
+ result = SET_INT(int(value));
+ break;
+ case LONG:
+ result = SET_LONG(long(value));
+ break;
+ case FLOAT:
+ result = SET_FLOAT(value);
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(double(value));
+ break;
+ case STRING: {
+ char buf[128];
+ sprintf(buf, "%f", value);
+ result = SET_STRING(buf);
+ break;
+ }
}
-}
-const SGPropertyNode *
-SGPropertyNode::getChild (const string &name, int index) const
-{
- int pos = find_child(name, index, _children);
- if (pos >= 0)
- _children[_children.size()-1];
- else
- return 0;
+ DO_TRACE_WRITE(FLOAT);
+ return result;
}
-
-class CompareIndices
+bool
+SGPropertyNode::setDoubleValue (double value)
{
-public:
- int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
- return (n1->getIndex() < n2->getIndex());
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE || _type == UNSPECIFIED) {
+ clear_value();
+ _value.double_val = new SGRawValueInternal<double>;
+ _type = DOUBLE;
}
-};
-
-/**
- * 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]);
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setDoubleValue(value);
+ break;
+ case BOOL:
+ result = SET_BOOL(value == 0.0L ? false : true);
+ break;
+ case INT:
+ result = SET_INT(int(value));
+ break;
+ case LONG:
+ result = SET_LONG(long(value));
+ break;
+ case FLOAT:
+ result = SET_FLOAT(float(value));
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(value);
+ break;
+ case STRING: {
+ char buf[128];
+ sprintf(buf, "%lf", value);
+ result = SET_STRING(buf);
+ break;
+ }
+ }
- sort(children.begin(), children.end(), CompareIndices());
- return children;
+ DO_TRACE_WRITE(DOUBLE);
+ return result;
}
-
-/**
- * Get all children with the same name (but different indices).
- */
-vector<const SGPropertyNode *>
-SGPropertyNode::getChildren (const string &name) const
+bool
+SGPropertyNode::setStringValue (string value)
{
- vector<const SGPropertyNode *> children;
- int max = _children.size();
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE || _type == UNSPECIFIED) {
+ clear_value();
+ _value.string_val = new SGRawValueInternal<string>;
+ _type = STRING;
+ }
- for (int i = 0; i < max; i++)
- if (_children[i]->getName() == name)
- children.push_back(_children[i]);
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setStringValue(value);
+ break;
+ case BOOL:
+ result = SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+ break;
+ case INT:
+ result = SET_INT(atoi(value.c_str()));
+ break;
+ case LONG:
+ result = SET_LONG(strtol(value.c_str(), 0, 0));
+ break;
+ case FLOAT:
+ result = SET_FLOAT(atof(value.c_str()));
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(strtod(value.c_str(), 0));
+ break;
+ case STRING:
+ result = SET_STRING(value);
+ break;
+ }
- sort(children.begin(), children.end(), CompareIndices());
- return children;
+ DO_TRACE_WRITE(STRING);
+ return result;
}
-
-string
-SGPropertyNode::getPath (bool simplify) const
+bool
+SGPropertyNode::setUnspecifiedValue (string value)
{
- if (_parent == 0)
- return "";
+ bool result = false;
+ TEST_WRITE;
+ if (_type == NONE) {
+ clear_value();
+ _value.string_val = new SGRawValueInternal<string>;
+ _type = UNSPECIFIED;
+ }
- string path = _parent->getPath(simplify);
- path += '/';
- path += _name;
- if (_index != 0 || !simplify) {
- char buffer[128];
- sprintf(buffer, "[%d]", _index);
- path += buffer;
+ switch (_type) {
+ case ALIAS:
+ result = _value.alias->setUnspecifiedValue(value);
+ break;
+ case BOOL:
+ result = SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+ break;
+ case INT:
+ result = SET_INT(atoi(value.c_str()));
+ break;
+ case LONG:
+ result = SET_LONG(strtol(value.c_str(), 0, 0));
+ break;
+ case FLOAT:
+ result = SET_FLOAT(atof(value.c_str()));
+ break;
+ case DOUBLE:
+ result = SET_DOUBLE(strtod(value.c_str(), 0));
+ break;
+ case STRING:
+ case UNSPECIFIED:
+ result = SET_STRING(value);
+ break;
}
- return path;
-}
-SGValue::Type
-SGPropertyNode::getType () const
-{
- if (_value != 0)
- return _value->getType();
- else
- return SGValue::UNKNOWN;
+ DO_TRACE_WRITE(UNSPECIFIED);
+ return result;
}
-bool
-SGPropertyNode::getBoolValue () const
+bool
+SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
{
- return (_value == 0 ? SGRawValue<bool>::DefaultValue
- : _value->getBoolValue());
-}
+ if (_type == ALIAS || _tied)
+ return false;
-int
-SGPropertyNode::getIntValue () const
-{
- return (_value == 0 ? SGRawValue<int>::DefaultValue
- : _value->getIntValue());
-}
+ bool old_val = false;
+ if (useDefault)
+ old_val = getBoolValue();
-float
-SGPropertyNode::getFloatValue () const
-{
- return (_value == 0 ? SGRawValue<float>::DefaultValue
- : _value->getFloatValue());
-}
+ clear_value();
+ _type = BOOL;
+ _tied = true;
+ _value.bool_val = rawValue.clone();
-double
-SGPropertyNode::getDoubleValue () const
-{
- return (_value == 0 ? SGRawValue<double>::DefaultValue
- : _value->getDoubleValue());
-}
+ if (useDefault)
+ setBoolValue(old_val);
-string
-SGPropertyNode::getStringValue () const
-{
- return (_value == 0 ? SGRawValue<string>::DefaultValue
- : _value->getStringValue());
+ return true;
}
bool
-SGPropertyNode::setBoolValue (bool val)
+SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
{
- if (_value == 0)
- _value = new SGValue();
- return _value->setBoolValue(val);
-}
+ if (_type == ALIAS || _tied)
+ return false;
-bool
-SGPropertyNode::setIntValue (int val)
-{
- if (_value == 0)
- _value = new SGValue();
- return _value->setIntValue(val);
-}
+ int old_val = 0;
+ if (useDefault)
+ old_val = getIntValue();
-bool
-SGPropertyNode::setFloatValue (float val)
-{
- if (_value == 0)
- _value = new SGValue();
- return _value->setFloatValue(val);
-}
+ clear_value();
+ _type = INT;
+ _tied = true;
+ _value.int_val = rawValue.clone();
-bool
-SGPropertyNode::setDoubleValue (double val)
-{
- if (_value == 0)
- _value = new SGValue();
- return _value->setDoubleValue(val);
-}
+ if (useDefault)
+ setIntValue(old_val);
-bool
-SGPropertyNode::setStringValue (string val)
-{
- if (_value == 0)
- _value = new SGValue();
- return _value->setStringValue(val);
+ return true;
}
bool
-SGPropertyNode::setUnknownValue (string val)
+SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
{
- if (_value == 0)
- _value = new SGValue();
- return _value->setUnknownValue(val);
-}
+ if (_type == ALIAS || _tied)
+ return false;
-bool
-SGPropertyNode::isTied () const
-{
- return (_value == 0 ? false : _value->isTied());
-}
+ long old_val;
+ if (useDefault)
+ old_val = getLongValue();
-bool
-SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
-{
- if (_value == 0)
- _value = new SGValue();
- return _value->tie(rawValue, useDefault);
-}
+ clear_value();
+ _type = LONG;
+ _tied = true;
+ _value.long_val = rawValue.clone();
-bool
-SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
-{
- if (_value == 0)
- _value = new SGValue();
- return _value->tie(rawValue, useDefault);
+ if (useDefault)
+ setLongValue(old_val);
+
+ return true;
}
bool
SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
{
- if (_value == 0)
- _value = new SGValue();
- return _value->tie(rawValue, useDefault);
+ if (_type == ALIAS || _tied)
+ return false;
+
+ float old_val = 0.0;
+ if (useDefault)
+ old_val = getFloatValue();
+
+ clear_value();
+ _type = FLOAT;
+ _tied = true;
+ _value.float_val = rawValue.clone();
+
+ if (useDefault)
+ setFloatValue(old_val);
+
+ return true;
}
bool
SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
{
- if (_value == 0)
- _value = new SGValue();
- return _value->tie(rawValue, useDefault);
+ if (_type == ALIAS || _tied)
+ return false;
+
+ double old_val = 0.0;
+ if (useDefault)
+ old_val = getDoubleValue();
+
+ clear_value();
+ _type = DOUBLE;
+ _tied = true;
+ _value.double_val = rawValue.clone();
+
+ if (useDefault)
+ setDoubleValue(old_val);
+
+ return true;
+
}
bool
SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
{
- if (_value == 0)
- _value = new SGValue();
- return _value->tie(rawValue, useDefault);
+ if (_type == ALIAS || _tied)
+ return false;
+
+ string old_val;
+ if (useDefault)
+ old_val = getStringValue();
+
+ clear_value();
+ _type = STRING;
+ _tied = true;
+ _value.string_val = rawValue.clone();
+
+ if (useDefault)
+ setStringValue(old_val);
+
+ return true;
}
bool
SGPropertyNode::untie ()
{
- return (_value == 0 ? false : _value->untie());
+ if (!_tied)
+ return false;
+
+ switch (_type) {
+ case BOOL: {
+ bool val = getBoolValue();
+ clear_value();
+ _type = BOOL;
+ _value.bool_val = new SGRawValueInternal<bool>;
+ SET_BOOL(val);
+ break;
+ }
+ case INT: {
+ int val = getIntValue();
+ clear_value();
+ _type = INT;
+ _value.int_val = new SGRawValueInternal<int>;
+ SET_INT(val);
+ break;
+ }
+ case LONG: {
+ long val = getLongValue();
+ clear_value();
+ _type = LONG;
+ _value.long_val = new SGRawValueInternal<long>;
+ SET_LONG(val);
+ break;
+ }
+ case FLOAT: {
+ float val = getFloatValue();
+ clear_value();
+ _type = FLOAT;
+ _value.float_val = new SGRawValueInternal<float>;
+ SET_FLOAT(val);
+ break;
+ }
+ case DOUBLE: {
+ double val = getDoubleValue();
+ clear_value();
+ _type = DOUBLE;
+ _value.double_val = new SGRawValueInternal<double>;
+ SET_DOUBLE(val);
+ break;
+ }
+ case STRING: {
+ string val = getStringValue();
+ clear_value();
+ _type = STRING;
+ _value.string_val = new SGRawValueInternal<string>;
+ SET_STRING(val);
+ break;
+ }
+ }
+
+ _tied = false;
+ return true;
}
SGPropertyNode *
}
-/**
- * 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());
-}
-
-
-/**
- * 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()));
}
}
+/**
+ * Get a long value for another node.
+ */
+long
+SGPropertyNode::getLongValue (const string &relative_path,
+ long defaultValue) const
+{
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? defaultValue : node->getLongValue());
+}
+
+
/**
* Get a float value for another node.
*/
}
+/**
+ * Set a long value for another node.
+ */
+bool
+SGPropertyNode::setLongValue (const string &relative_path, long value)
+{
+ return getNode(relative_path, true)->setLongValue(value);
+}
+
+
/**
* Set a float value for another node.
*/
* 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);
}
}
+/**
+ * Tie a node reached by a relative path, creating it if necessary.
+ */
+bool
+SGPropertyNode::tie (const string &relative_path,
+ const SGRawValue<long> &rawValue,
+ bool useDefault)
+{
+ return getNode(relative_path, true)->tie(rawValue, useDefault);
+}
+
+
/**
* Tie a node reached by a relative path, creating it if necessary.
*/