//
// $Id$
+#ifdef HAVE_CONFIG_H
+# include <simgear_config.h>
+#endif
+
#include "props.hxx"
#include <algorithm>
#include <sstream>
#include <iomanip>
+#include <iterator>
#include <stdio.h>
#include <string.h>
+#include <boost/algorithm/string/find_iterator.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/algorithm/string/classification.hpp>
+#include <boost/bind.hpp>
+#include <boost/functional/hash.hpp>
+#include <boost/range.hpp>
+
#include <simgear/math/SGMath.hxx>
#if PROPS_STANDALONE
using std::stringstream;
using namespace simgear;
-using namespace simgear::props;
\f
////////////////////////////////////////////////////////////////////////
#define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
#define TEST_WRITE if (!getAttribute(WRITE)) return false
\f
-////////////////////////////////////////////////////////////////////////
-// Default values for every type.
-////////////////////////////////////////////////////////////////////////
-template<> const bool SGRawValue<bool>::DefaultValue = false;
-template<> const int SGRawValue<int>::DefaultValue = 0;
-template<> const long SGRawValue<long>::DefaultValue = 0L;
-template<> const float SGRawValue<float>::DefaultValue = 0.0;
-template<> const double SGRawValue<double>::DefaultValue = 0.0L;
-template<> const char * const SGRawValue<const char *>::DefaultValue = "";
-template<> const SGVec3d SGRawValue<SGVec3d>::DefaultValue = SGVec3d();
-template<> const SGVec4d SGRawValue<SGVec4d>::DefaultValue = SGVec4d();
-\f
////////////////////////////////////////////////////////////////////////
// Local path normalization code.
////////////////////////////////////////////////////////////////////////
-/**
- * A component in a path.
- */
-struct PathComponent
-{
- string name;
- int index;
-};
-
/**
* Parse the name for a path component.
*
* Name: [_a-zA-Z][-._a-zA-Z0-9]*
*/
-static inline const string
-parse_name (const string &path, int &i)
+
+template<typename Range>
+inline Range
+parse_name (const SGPropertyNode *node, const Range &path)
{
- string name = "";
- int max = path.size();
+ typename Range::iterator i = path.begin();
+ typename Range::iterator max = path.end();
- if (path[i] == '.') {
+ if (*i == '.') {
i++;
- if (i < max && path[i] == '.') {
+ if (i != path.end() && *i == '.') {
i++;
- name = "..";
- } else {
- name = ".";
}
- if (i < max && path[i] != '/')
- throw string("illegal character after " + name);
- }
-
- else if (isalpha(path[i]) || path[i] == '_') {
- name += path[i];
+ if (i != max && *i != '/')
+ throw string("illegal character after . or ..");
+ } else if (isalpha(*i) || *i == '_') {
i++;
// The rules inside a name are a little
// less restrictive.
- while (i < max) {
- if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
- path[i] == '-' || path[i] == '.') {
- name += path[i];
- } else if (path[i] == '[' || path[i] == '/') {
+ while (i != max) {
+ if (isalpha(*i) || isdigit(*i) || *i == '_' ||
+ *i == '-' || *i == '.') {
+ // name += path[i];
+ } else if (*i == '[' || *i == '/') {
break;
} else {
- throw string("name may contain only ._- and alphanumeric characters");
+ string err = "'";
+ err.push_back(*i);
+ err.append("' found in propertyname after '"+node->getNameString()+"'");
+ err.append("\nname may contain only ._- and alphanumeric characters");
+ throw err;
}
i++;
}
}
else {
- if (name.size() == 0)
- throw string("name must begin with alpha or '_'");
- }
-
- return name;
-}
-
-
-/**
- * Parse the optional integer index for a path component.
- *
- * Index: "[" [0-9]+ "]"
- */
-static inline int
-parse_index (const string &path, int &i)
-{
- int index = 0;
-
- if (path[i] != '[')
- return 0;
- else
- i++;
-
- for (int max = path.size(); i < max; i++) {
- if (isdigit(path[i])) {
- index = (index * 10) + (path[i] - '0');
- } else if (path[i] == ']') {
- i++;
- return index;
- } else {
- break;
+ if (path.begin() == i) {
+ string err = "'";
+ err.push_back(*i);
+ err.append("' found in propertyname after '"+node->getNameString()+"'");
+ err.append("\nname must begin with alpha or '_'");
+ throw err;
}
}
-
- throw string("unterminated index (looking for ']')");
+ return Range(path.begin(), i);
}
-
-/**
- * Parse a single path component.
- *
- * Component: Name Index?
- */
-static inline PathComponent
-parse_component (const string &path, int &i)
+// Validate the name of a single node
+inline bool validateName(const string& name)
{
- PathComponent component;
- component.name = parse_name(path, i);
- if (component.name[0] != '.')
- component.index = parse_index(path, i);
- else
- component.index = -1;
- return component;
-}
-
-
-/**
- * Parse a path into its components.
- */
-static void
-parse_path (const string &path, vector<PathComponent> &components)
-{
- int pos = 0;
- int max = path.size();
-
- // Check for initial '/'
- if (path[pos] == '/') {
- PathComponent root;
- root.name = "";
- root.index = -1;
- components.push_back(root);
- pos++;
- while (pos < max && path[pos] == '/')
- pos++;
- }
-
- while (pos < max) {
- components.push_back(parse_component(path, pos));
- while (pos < max && path[pos] == '/')
- pos++;
- }
+ using namespace boost;
+ if (name.empty())
+ return false;
+ if (!isalpha(name[0]) && name[0] != '_')
+ return false;
+ return all(make_iterator_range(name.begin(), name.end()),
+ is_alnum() || is_any_of("_-."));
}
-
\f
////////////////////////////////////////////////////////////////////////
// Other static utility functions.
/**
* Locate a child node by name and index.
*/
+template<typename Itr>
static int
-find_child (const char * name, int index, const vector<SGPropertyNode_ptr>& nodes)
+find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
{
int nNodes = nodes.size();
+ boost::iterator_range<Itr> name(begin, end);
for (int i = 0; i < nNodes; i++) {
SGPropertyNode * node = nodes[i];
// searching for a mathing index is a lot less time consuming than
// comparing two strings so do that first.
- if (node->getIndex() == index && compare_strings(node->getName(), name))
+ if (node->getIndex() == index && boost::equals(node->getName(), name))
return i;
}
return -1;
* Locate the child node with the highest index of the same name
*/
static int
-find_last_child (const char * name, const vector<SGPropertyNode_ptr>& nodes)
+find_last_child (const char * name, const PropertyList& nodes)
{
int nNodes = nodes.size();
int index = 0;
return index;
}
-
-/**
- * Locate another node, given a relative path.
- */
-static SGPropertyNode *
-find_node (SGPropertyNode * current,
- const vector<PathComponent> &components,
- int position,
- bool create)
+template<typename Itr>
+inline SGPropertyNode*
+SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
{
- // Run off the end of the list
- if (current == 0) {
- return 0;
- }
-
- // Success! This is the one we want.
- else if (position >= (int)components.size()) {
- return (current->getAttribute(SGPropertyNode::REMOVED) ? 0 : current);
+ int pos = find_child(begin, end, index, _children);
+ if (pos >= 0) {
+ return _children[pos];
+ } else if (create) {
+ SGPropertyNode_ptr node;
+ pos = find_child(begin, end, index, _removedChildren);
+ if (pos >= 0) {
+ PropertyList::iterator it = _removedChildren.begin();
+ it += pos;
+ node = _removedChildren[pos];
+ _removedChildren.erase(it);
+ node->setAttribute(REMOVED, false);
+ _children.push_back(node);
+ fireChildAdded(node);
+ return node;
+ }
}
+ return 0;
+}
+
+template<typename Itr>
+SGPropertyNode *
+SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
+{
+ SGPropertyNode* node = getExistingChild(begin, end, index, create);
- // Empty component means root.
- else if (components[position].name == "") {
- return find_node(current->getRootNode(), components, position + 1, create);
- }
+ if (node) {
+ return node;
+ } else if (create) {
+ node = new SGPropertyNode(begin, end, index, this);
+ _children.push_back(node);
+ fireChildAdded(node);
+ return node;
+ } else {
+ return 0;
+ }
+}
- // . means current directory
- else if (components[position].name == ".") {
- return find_node(current, components, position + 1, create);
+template<typename SplitItr>
+SGPropertyNode*
+find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
+ int last_index)
+{
+ typedef typename SplitItr::value_type Range;
+ // Run off the end of the list
+ if (current == 0) {
+ return 0;
}
- // .. means parent directory
- else if (components[position].name == "..") {
+ // Success! This is the one we want.
+ if (itr.eof())
+ return current;
+ Range token = *itr;
+ // Empty name at this point is empty, not root.
+ if (token.empty())
+ return find_node_aux(current, ++itr, create, last_index);
+ Range name = parse_name(current, token);
+ if (equals(name, "."))
+ return find_node_aux(current, ++itr, create, last_index);
+ if (equals(name, "..")) {
SGPropertyNode * parent = current->getParent();
if (parent == 0)
throw string("attempt to move past root with '..'");
- else
- return find_node(parent, components, position + 1, create);
- }
-
- // Otherwise, a child name
- else {
- SGPropertyNode * child =
- current->getChild(components[position].name.c_str(),
- components[position].index,
- create);
- return find_node(child, components, position + 1, create);
+ return find_node_aux(parent, ++itr, create, last_index);
+ }
+ int index = -1;
+ if (last_index >= 0) {
+ // If we are at the last token and last_index is valid, use
+ // last_index as the index value
+ bool lastTok = true;
+ while (!(++itr).eof()) {
+ if (!itr->empty()) {
+ lastTok = false;
+ break;
+ }
+ }
+ if (lastTok)
+ index = last_index;
+ } else {
+ ++itr;
+ }
+
+ if (index < 0) {
+ index = 0;
+ if (name.end() != token.end()) {
+ if (*name.end() == '[') {
+ typename Range::iterator i = name.end() + 1, end = token.end();
+ for (;i != end; ++i) {
+ if (isdigit(*i)) {
+ index = (index * 10) + (*i - '0');
+ } else {
+ break;
+ }
+ }
+ if (i == token.end() || *i != ']')
+ throw string("unterminated index (looking for ']')");
+ } else {
+ throw string("illegal characters in token: ")
+ + string(name.begin(), name.end());
+ }
+ }
}
+ return find_node_aux(current->getChildImpl(name.begin(), name.end(),
+ index, create), itr, create,
+ last_index);
}
+// Internal function for parsing property paths. last_index provides
+// and index value for the last node name token, if supplied.
+template<typename Range>
+SGPropertyNode*
+find_node (SGPropertyNode * current,
+ const Range& path,
+ bool create,
+ int last_index = -1)
+{
+ using namespace boost;
+ typedef split_iterator<typename range_result_iterator<Range>::type>
+ PathSplitIterator;
+
+ PathSplitIterator itr
+ = make_split_iterator(path, first_finder("/", is_equal()));
+ if (*path.begin() == '/')
+ return find_node_aux(current->getRootNode(), itr, create, last_index);
+ else
+ return find_node_aux(current, itr, create, last_index);
+}
\f
////////////////////////////////////////////////////////////////////////
void
SGPropertyNode::clearValue ()
{
- if (_type == ALIAS) {
+ if (_type == props::ALIAS) {
put(_value.alias);
_value.alias = 0;
- } else if (_type != NONE) {
+ } else if (_type != props::NONE) {
switch (_type) {
- case BOOL:
- _local_val.bool_val = SGRawValue<bool>::DefaultValue;
+ case props::BOOL:
+ _local_val.bool_val = SGRawValue<bool>::DefaultValue();
break;
- case INT:
- _local_val.int_val = SGRawValue<int>::DefaultValue;
+ case props::INT:
+ _local_val.int_val = SGRawValue<int>::DefaultValue();
break;
- case LONG:
- _local_val.long_val = SGRawValue<long>::DefaultValue;
+ case props::LONG:
+ _local_val.long_val = SGRawValue<long>::DefaultValue();
break;
- case FLOAT:
- _local_val.float_val = SGRawValue<float>::DefaultValue;
+ case props::FLOAT:
+ _local_val.float_val = SGRawValue<float>::DefaultValue();
break;
- case DOUBLE:
- _local_val.double_val = SGRawValue<double>::DefaultValue;
+ case props::DOUBLE:
+ _local_val.double_val = SGRawValue<double>::DefaultValue();
break;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
if (!_tied) {
delete [] _local_val.string_val;
}
_local_val.string_val = 0;
break;
+ default: // avoid compiler warning
+ break;
}
delete _value.val;
_value.val = 0;
}
_tied = false;
- _type = NONE;
+ _type = props::NONE;
}
if (!getAttribute(READ))
return "";
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->getStringValue();
- case BOOL:
+ case props::BOOL:
return get_bool() ? "true" : "false";
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
return get_string();
- case NONE:
+ case props::NONE:
return "";
default:
break;
}
stringstream sstr;
switch (_type) {
- case INT:
+ case props::INT:
sstr << get_int();
break;
- case LONG:
+ case props::LONG:
sstr << get_long();
break;
- case FLOAT:
+ case props::FLOAT:
sstr << get_float();
break;
- case DOUBLE:
+ case props::DOUBLE:
sstr << std::setprecision(10) << get_double();
break;
- case EXTENDED:
+ case props::EXTENDED:
{
- Type realType = _value.val->getType();
+ props::Type realType = _value.val->getType();
// Perhaps this should be done for all types?
- if (realType == VEC3D || realType == VEC4D)
+ if (realType == props::VEC3D || realType == props::VEC4D)
sstr.precision(10);
static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
}
: _index(0),
_parent(0),
_path_cache(0),
- _type(NONE),
+ _type(props::NONE),
_tied(false),
_attr(READ|WRITE),
_listeners(0)
{
_local_val.string_val = 0;
_value.val = 0;
- if (_type == NONE)
+ if (_type == props::NONE)
return;
- if (_type == ALIAS) {
+ if (_type == props::ALIAS) {
_value.alias = node._value.alias;
get(_value.alias);
_tied = false;
return;
}
- if (_tied || _type == EXTENDED) {
+ if (_tied || _type == props::EXTENDED) {
_value.val = node._value.val->clone();
return;
}
switch (_type) {
- case BOOL:
+ case props::BOOL:
set_bool(node.get_bool());
break;
- case INT:
+ case props::INT:
set_int(node.get_int());
break;
- case LONG:
+ case props::LONG:
set_long(node.get_long());
break;
- case FLOAT:
+ case props::FLOAT:
set_float(node.get_float());
break;
- case DOUBLE:
+ case props::DOUBLE:
set_double(node.get_double());
break;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
set_string(node.get_string());
break;
default:
/**
* Convenience constructor.
*/
-SGPropertyNode::SGPropertyNode (const char * name,
+template<typename Itr>
+SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
int index,
SGPropertyNode * parent)
: _index(index),
+ _name(begin, end),
_parent(parent),
_path_cache(0),
- _type(NONE),
+ _type(props::NONE),
_tied(false),
_attr(READ|WRITE),
_listeners(0)
{
- int i = 0;
_local_val.string_val = 0;
_value.val = 0;
- _name = parse_name(name, i);
- if (i != int(strlen(name)) || name[0] == '.')
- throw string("plain name expected instead of '") + name + '\'';
+ if (!validateName(_name))
+ throw string("plain name expected instead of '") + _name + '\'';
}
+SGPropertyNode::SGPropertyNode (const string& name,
+ int index,
+ SGPropertyNode * parent)
+ : _index(index),
+ _name(name),
+ _parent(parent),
+ _path_cache(0),
+ _type(props::NONE),
+ _tied(false),
+ _attr(READ|WRITE),
+ _listeners(0)
+{
+ _local_val.string_val = 0;
+ _value.val = 0;
+ if (!validateName(name))
+ throw string("plain name expected instead of '") + _name + '\'';
+}
/**
* Destructor.
bool
SGPropertyNode::alias (SGPropertyNode * target)
{
- if (target == 0 || _type == ALIAS || _tied)
+ if (target == 0 || _type == props::ALIAS || _tied)
return false;
clearValue();
get(target);
_value.alias = target;
- _type = ALIAS;
+ _type = props::ALIAS;
return true;
}
bool
SGPropertyNode::unalias ()
{
- if (_type != ALIAS)
+ if (_type != props::ALIAS)
return false;
clearValue();
return true;
SGPropertyNode *
SGPropertyNode::getAliasTarget ()
{
- return (_type == ALIAS ? _value.alias : 0);
+ return (_type == props::ALIAS ? _value.alias : 0);
}
const SGPropertyNode *
SGPropertyNode::getAliasTarget () const
{
- return (_type == ALIAS ? _value.alias : 0);
+ return (_type == props::ALIAS ? _value.alias : 0);
}
/**
int pos = find_last_child(name, _children)+1;
SGPropertyNode_ptr node;
- node = new SGPropertyNode(name, pos, this);
+ node = new SGPropertyNode(name, name + strlen(name), pos, this);
_children.push_back(node);
fireChildAdded(node);
return node;
/**
* Get a non-const child by name and index, creating if necessary.
*/
+
SGPropertyNode *
SGPropertyNode::getChild (const char * name, int index, bool create)
{
- int pos = find_child(name, index, _children);
- if (pos >= 0) {
- return _children[pos];
- } else if (create) {
- SGPropertyNode_ptr node;
- pos = find_child(name, index, _removedChildren);
- if (pos >= 0) {
- vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
- it += pos;
- node = _removedChildren[pos];
- _removedChildren.erase(it);
- node->setAttribute(REMOVED, false);
- } else {
+ return getChildImpl(name, name + strlen(name), index, create);
+}
+
+SGPropertyNode *
+SGPropertyNode::getChild (const string& name, int index, bool create)
+{
+ SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
+ create);
+ if (node) {
+ return node;
+ } else if (create) {
node = new SGPropertyNode(name, index, this);
+ _children.push_back(node);
+ fireChildAdded(node);
+ return node;
+ } else {
+ return 0;
}
- _children.push_back(node);
- fireChildAdded(node);
- return node;
- } else {
- return 0;
- }
}
-
/**
* Get a const child by name and index.
*/
const SGPropertyNode *
SGPropertyNode::getChild (const char * name, int index) const
{
- int pos = find_child(name, index, _children);
+ int pos = find_child(name, name + strlen(name), index, _children);
if (pos >= 0)
return _children[pos];
else
/**
* Get all children with the same name (but different indices).
*/
-vector<SGPropertyNode_ptr>
+PropertyList
SGPropertyNode::getChildren (const char * name) const
{
- vector<SGPropertyNode_ptr> children;
+ PropertyList children;
int max = _children.size();
for (int i = 0; i < max; i++)
if (pos < 0 || pos >= (int)_children.size())
return node;
- vector<SGPropertyNode_ptr>::iterator it = _children.begin();
+ PropertyList::iterator it = _children.begin();
it += pos;
node = _children[pos];
_children.erase(it);
SGPropertyNode::removeChild (const char * name, int index, bool keep)
{
SGPropertyNode_ptr ret;
- int pos = find_child(name, index, _children);
+ int pos = find_child(name, name + strlen(name), index, _children);
if (pos >= 0)
ret = removeChild(pos, keep);
return ret;
/**
* Remove all children with the specified name.
*/
-vector<SGPropertyNode_ptr>
+PropertyList
SGPropertyNode::removeChildren (const char * name, bool keep)
{
- vector<SGPropertyNode_ptr> children;
+ PropertyList children;
for (int pos = _children.size() - 1; pos >= 0; pos--)
if (compare_strings(_children[pos]->getName(), name))
}
-const char *
+string
SGPropertyNode::getPath (bool simplify) const
{
- // Calculate the complete path only once.
- if (_parent != 0 && _path.empty()) {
- _path = _parent->getPath(simplify);
- _path += '/';
- _path += getDisplayName(simplify);
+ typedef std::vector<SGConstPropertyNode_ptr> PList;
+ PList pathList;
+ for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
+ pathList.push_back(node);
+ string result;
+ for (PList::reverse_iterator itr = pathList.rbegin(),
+ rend = pathList.rend();
+ itr != rend;
+ ++itr) {
+ result += '/';
+ result += (*itr)->getDisplayName(simplify);
}
-
- return _path.c_str();
+ return result;
}
-Type
+props::Type
SGPropertyNode::getType () const
{
- if (_type == ALIAS)
+ if (_type == props::ALIAS)
return _value.alias->getType();
- else if (_type == EXTENDED)
+ else if (_type == props::EXTENDED)
return _value.val->getType();
else
return _type;
SGPropertyNode::getBoolValue () const
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == BOOL)
+ if (_attr == (READ|WRITE) && _type == props::BOOL)
return get_bool();
if (getAttribute(TRACE_READ))
trace_read();
if (!getAttribute(READ))
- return SGRawValue<bool>::DefaultValue;
+ return SGRawValue<bool>::DefaultValue();
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->getBoolValue();
- case BOOL:
+ case props::BOOL:
return get_bool();
- case INT:
+ case props::INT:
return get_int() == 0 ? false : true;
- case LONG:
+ case props::LONG:
return get_long() == 0L ? false : true;
- case FLOAT:
+ case props::FLOAT:
return get_float() == 0.0 ? false : true;
- case DOUBLE:
+ case props::DOUBLE:
return get_double() == 0.0L ? false : true;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
- case NONE:
+ case props::NONE:
default:
- return SGRawValue<bool>::DefaultValue;
+ return SGRawValue<bool>::DefaultValue();
}
}
SGPropertyNode::getIntValue () const
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == INT)
+ if (_attr == (READ|WRITE) && _type == props::INT)
return get_int();
if (getAttribute(TRACE_READ))
trace_read();
if (!getAttribute(READ))
- return SGRawValue<int>::DefaultValue;
+ return SGRawValue<int>::DefaultValue();
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->getIntValue();
- case BOOL:
+ case props::BOOL:
return int(get_bool());
- case INT:
+ case props::INT:
return get_int();
- case LONG:
+ case props::LONG:
return int(get_long());
- case FLOAT:
+ case props::FLOAT:
return int(get_float());
- case DOUBLE:
+ case props::DOUBLE:
return int(get_double());
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
return atoi(get_string());
- case NONE:
+ case props::NONE:
default:
- return SGRawValue<int>::DefaultValue;
+ return SGRawValue<int>::DefaultValue();
}
}
SGPropertyNode::getLongValue () const
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == LONG)
+ if (_attr == (READ|WRITE) && _type == props::LONG)
return get_long();
if (getAttribute(TRACE_READ))
trace_read();
if (!getAttribute(READ))
- return SGRawValue<long>::DefaultValue;
+ return SGRawValue<long>::DefaultValue();
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->getLongValue();
- case BOOL:
+ case props::BOOL:
return long(get_bool());
- case INT:
+ case props::INT:
return long(get_int());
- case LONG:
+ case props::LONG:
return get_long();
- case FLOAT:
+ case props::FLOAT:
return long(get_float());
- case DOUBLE:
+ case props::DOUBLE:
return long(get_double());
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
return strtol(get_string(), 0, 0);
- case NONE:
+ case props::NONE:
default:
- return SGRawValue<long>::DefaultValue;
+ return SGRawValue<long>::DefaultValue();
}
}
SGPropertyNode::getFloatValue () const
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == FLOAT)
+ if (_attr == (READ|WRITE) && _type == props::FLOAT)
return get_float();
if (getAttribute(TRACE_READ))
trace_read();
if (!getAttribute(READ))
- return SGRawValue<float>::DefaultValue;
+ return SGRawValue<float>::DefaultValue();
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->getFloatValue();
- case BOOL:
+ case props::BOOL:
return float(get_bool());
- case INT:
+ case props::INT:
return float(get_int());
- case LONG:
+ case props::LONG:
return float(get_long());
- case FLOAT:
+ case props::FLOAT:
return get_float();
- case DOUBLE:
+ case props::DOUBLE:
return float(get_double());
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
return atof(get_string());
- case NONE:
+ case props::NONE:
default:
- return SGRawValue<float>::DefaultValue;
+ return SGRawValue<float>::DefaultValue();
}
}
SGPropertyNode::getDoubleValue () const
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == DOUBLE)
+ if (_attr == (READ|WRITE) && _type == props::DOUBLE)
return get_double();
if (getAttribute(TRACE_READ))
trace_read();
if (!getAttribute(READ))
- return SGRawValue<double>::DefaultValue;
+ return SGRawValue<double>::DefaultValue();
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->getDoubleValue();
- case BOOL:
+ case props::BOOL:
return double(get_bool());
- case INT:
+ case props::INT:
return double(get_int());
- case LONG:
+ case props::LONG:
return double(get_long());
- case FLOAT:
+ case props::FLOAT:
return double(get_float());
- case DOUBLE:
+ case props::DOUBLE:
return get_double();
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
return strtod(get_string(), 0);
- case NONE:
+ case props::NONE:
default:
- return SGRawValue<double>::DefaultValue;
+ return SGRawValue<double>::DefaultValue();
}
}
SGPropertyNode::getStringValue () const
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == STRING)
+ if (_attr == (READ|WRITE) && _type == props::STRING)
return get_string();
if (getAttribute(TRACE_READ))
trace_read();
if (!getAttribute(READ))
- return SGRawValue<const char *>::DefaultValue;
+ return SGRawValue<const char *>::DefaultValue();
return make_string();
}
SGPropertyNode::setBoolValue (bool value)
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == BOOL)
+ if (_attr == (READ|WRITE) && _type == props::BOOL)
return set_bool(value);
bool result = false;
TEST_WRITE;
- if (_type == NONE || _type == UNSPECIFIED) {
+ if (_type == props::NONE || _type == props::UNSPECIFIED) {
clearValue();
_tied = false;
- _type = BOOL;
+ _type = props::BOOL;
}
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setBoolValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool(value);
break;
- case INT:
+ case props::INT:
result = set_int(int(value));
break;
- case LONG:
+ case props::LONG:
result = set_long(long(value));
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(float(value));
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(double(value));
break;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
result = set_string(value ? "true" : "false");
break;
- case NONE:
+ case props::NONE:
default:
break;
}
SGPropertyNode::setIntValue (int value)
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == INT)
+ if (_attr == (READ|WRITE) && _type == props::INT)
return set_int(value);
bool result = false;
TEST_WRITE;
- if (_type == NONE || _type == UNSPECIFIED) {
+ if (_type == props::NONE || _type == props::UNSPECIFIED) {
clearValue();
- _type = INT;
+ _type = props::INT;
_local_val.int_val = 0;
}
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setIntValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool(value == 0 ? false : true);
break;
- case INT:
+ case props::INT:
result = set_int(value);
break;
- case LONG:
+ case props::LONG:
result = set_long(long(value));
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(float(value));
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(double(value));
break;
- case STRING:
- case UNSPECIFIED: {
+ case props::STRING:
+ case props::UNSPECIFIED: {
char buf[128];
sprintf(buf, "%d", value);
result = set_string(buf);
break;
}
- case NONE:
+ case props::NONE:
default:
break;
}
SGPropertyNode::setLongValue (long value)
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == LONG)
+ if (_attr == (READ|WRITE) && _type == props::LONG)
return set_long(value);
bool result = false;
TEST_WRITE;
- if (_type == NONE || _type == UNSPECIFIED) {
+ if (_type == props::NONE || _type == props::UNSPECIFIED) {
clearValue();
- _type = LONG;
+ _type = props::LONG;
_local_val.long_val = 0L;
}
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setLongValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool(value == 0L ? false : true);
break;
- case INT:
+ case props::INT:
result = set_int(int(value));
break;
- case LONG:
+ case props::LONG:
result = set_long(value);
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(float(value));
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(double(value));
break;
- case STRING:
- case UNSPECIFIED: {
+ case props::STRING:
+ case props::UNSPECIFIED: {
char buf[128];
sprintf(buf, "%ld", value);
result = set_string(buf);
break;
}
- case NONE:
+ case props::NONE:
default:
break;
}
SGPropertyNode::setFloatValue (float value)
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == FLOAT)
+ if (_attr == (READ|WRITE) && _type == props::FLOAT)
return set_float(value);
bool result = false;
TEST_WRITE;
- if (_type == NONE || _type == UNSPECIFIED) {
+ if (_type == props::NONE || _type == props::UNSPECIFIED) {
clearValue();
- _type = FLOAT;
+ _type = props::FLOAT;
_local_val.float_val = 0;
}
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setFloatValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool(value == 0.0 ? false : true);
break;
- case INT:
+ case props::INT:
result = set_int(int(value));
break;
- case LONG:
+ case props::LONG:
result = set_long(long(value));
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(value);
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(double(value));
break;
- case STRING:
- case UNSPECIFIED: {
+ case props::STRING:
+ case props::UNSPECIFIED: {
char buf[128];
sprintf(buf, "%f", value);
result = set_string(buf);
break;
}
- case NONE:
+ case props::NONE:
default:
break;
}
SGPropertyNode::setDoubleValue (double value)
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == DOUBLE)
+ if (_attr == (READ|WRITE) && _type == props::DOUBLE)
return set_double(value);
bool result = false;
TEST_WRITE;
- if (_type == NONE || _type == UNSPECIFIED) {
+ if (_type == props::NONE || _type == props::UNSPECIFIED) {
clearValue();
_local_val.double_val = value;
- _type = DOUBLE;
+ _type = props::DOUBLE;
}
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setDoubleValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool(value == 0.0L ? false : true);
break;
- case INT:
+ case props::INT:
result = set_int(int(value));
break;
- case LONG:
+ case props::LONG:
result = set_long(long(value));
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(float(value));
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(value);
break;
- case STRING:
- case UNSPECIFIED: {
+ case props::STRING:
+ case props::UNSPECIFIED: {
char buf[128];
sprintf(buf, "%f", value);
result = set_string(buf);
break;
}
- case NONE:
+ case props::NONE:
default:
break;
}
SGPropertyNode::setStringValue (const char * value)
{
// Shortcut for common case
- if (_attr == (READ|WRITE) && _type == STRING)
+ if (_attr == (READ|WRITE) && _type == props::STRING)
return set_string(value);
bool result = false;
TEST_WRITE;
- if (_type == NONE || _type == UNSPECIFIED) {
+ if (_type == props::NONE || _type == props::UNSPECIFIED) {
clearValue();
- _type = STRING;
+ _type = props::STRING;
}
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setStringValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool((compare_strings(value, "true")
|| atoi(value)) ? true : false);
break;
- case INT:
+ case props::INT:
result = set_int(atoi(value));
break;
- case LONG:
+ case props::LONG:
result = set_long(strtol(value, 0, 0));
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(atof(value));
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(strtod(value, 0));
break;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
result = set_string(value);
break;
- case EXTENDED:
+ case props::EXTENDED:
{
stringstream sstr(value);
static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
}
break;
- case NONE:
+ case props::NONE:
default:
break;
}
{
bool result = false;
TEST_WRITE;
- if (_type == NONE) {
+ if (_type == props::NONE) {
clearValue();
- _type = UNSPECIFIED;
+ _type = props::UNSPECIFIED;
}
- Type type = _type;
- if (type == EXTENDED)
+ props::Type type = _type;
+ if (type == props::EXTENDED)
type = _value.val->getType();
switch (type) {
- case ALIAS:
+ case props::ALIAS:
result = _value.alias->setUnspecifiedValue(value);
break;
- case BOOL:
+ case props::BOOL:
result = set_bool((compare_strings(value, "true")
|| atoi(value)) ? true : false);
break;
- case INT:
+ case props::INT:
result = set_int(atoi(value));
break;
- case LONG:
+ case props::LONG:
result = set_long(strtol(value, 0, 0));
break;
- case FLOAT:
+ case props::FLOAT:
result = set_float(atof(value));
break;
- case DOUBLE:
+ case props::DOUBLE:
result = set_double(strtod(value, 0));
break;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
result = set_string(value);
break;
- case VEC3D:
+ case props::VEC3D:
result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
break;
- case VEC4D:
+ case props::VEC4D:
result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
break;
- case NONE:
+ case props::NONE:
default:
break;
}
if (!getAttribute(READ))
return stream;
switch (_type) {
- case ALIAS:
+ case props::ALIAS:
return _value.alias->printOn(stream);
- case BOOL:
+ case props::BOOL:
stream << (get_bool() ? "true" : "false");
break;
- case INT:
+ case props::INT:
stream << get_int();
break;
- case LONG:
+ case props::LONG:
stream << get_long();
break;
- case FLOAT:
+ case props::FLOAT:
stream << get_float();
break;
- case DOUBLE:
+ case props::DOUBLE:
stream << get_double();
break;
- case STRING:
- case UNSPECIFIED:
+ case props::STRING:
+ case props::UNSPECIFIED:
stream << get_string();
break;
- case EXTENDED:
+ case props::EXTENDED:
static_cast<SGRawExtended*>(_value.val)->printOn(stream);
break;
- case NONE:
+ case props::NONE:
+ break;
+ default: // avoid compiler warning
break;
}
return stream;
bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
bool useDefault)
{
- if (_type == ALIAS || _tied)
+ if (_type == props::ALIAS || _tied)
return false;
useDefault = useDefault && hasValue();
if (useDefault)
old_val = getStringValue();
clearValue();
- _type = STRING;
+ _type = props::STRING;
_tied = true;
_value.val = rawValue.clone();
return false;
switch (_type) {
- case BOOL: {
+ case props::BOOL: {
bool val = getBoolValue();
clearValue();
- _type = BOOL;
+ _type = props::BOOL;
_local_val.bool_val = val;
break;
}
- case INT: {
+ case props::INT: {
int val = getIntValue();
clearValue();
- _type = INT;
+ _type = props::INT;
_local_val.int_val = val;
break;
}
- case LONG: {
+ case props::LONG: {
long val = getLongValue();
clearValue();
- _type = LONG;
+ _type = props::LONG;
_local_val.long_val = val;
break;
}
- case FLOAT: {
+ case props::FLOAT: {
float val = getFloatValue();
clearValue();
- _type = FLOAT;
+ _type = props::FLOAT;
_local_val.float_val = val;
break;
}
- case DOUBLE: {
+ case props::DOUBLE: {
double val = getDoubleValue();
clearValue();
- _type = DOUBLE;
+ _type = props::DOUBLE;
_local_val.double_val = val;
break;
}
- case STRING:
- case UNSPECIFIED: {
+ case props::STRING:
+ case props::UNSPECIFIED: {
string val = getStringValue();
clearValue();
- _type = STRING;
+ _type = props::STRING;
_local_val.string_val = copy_string(val.c_str());
break;
}
- case EXTENDED: {
+ case props::EXTENDED: {
SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
_value.val = 0; // Prevent clearValue() from deleting
clearValue();
- _type = EXTENDED;
+ _type = props::EXTENDED;
_value.val = val->makeContainer();
delete val;
break;
}
- case NONE:
+ case props::NONE:
default:
break;
}
SGPropertyNode *
SGPropertyNode::getNode (const char * relative_path, bool create)
{
+ using namespace boost;
if (_path_cache == 0)
_path_cache = new hash_table;
SGPropertyNode * result = _path_cache->get(relative_path);
if (result == 0) {
- vector<PathComponent> components;
- parse_path(relative_path, components);
- result = find_node(this, components, 0, create);
+ result = find_node(this,
+ make_iterator_range(relative_path, relative_path
+ + strlen(relative_path)),
+ create);
if (result != 0)
_path_cache->put(relative_path, result);
}
SGPropertyNode *
SGPropertyNode::getNode (const char * relative_path, int index, bool create)
{
- vector<PathComponent> components;
- parse_path(relative_path, components);
- if (components.size() > 0)
- components.back().index = index;
- return find_node(this, components, 0, create);
+ using namespace boost;
+ return find_node(this, make_iterator_range(relative_path, relative_path
+ + strlen(relative_path)),
+ create, index);
}
const SGPropertyNode *
/**
* Get the value type for another node.
*/
-Type
+props::Type
SGPropertyNode::getType (const char * relative_path) const
{
const SGPropertyNode * node = getNode(relative_path);
- return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
+ return (node == 0 ? props::UNSPECIFIED : node->getType());
}
_properties.erase(it);
}
-namespace simgear
-{
template<>
std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
{
return stream;
}
+namespace simgear
+{
template<>
std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
{
}
return stream;
}
-
+}
template<>
std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
{
return stream;
}
+namespace simgear
+{
template<>
std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
{
return stream;
}
+namespace
+{
+bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
+{
+ props::Type ltype = lhs.getType();
+ props::Type rtype = rhs.getType();
+ if (ltype != rtype)
+ return false;
+ switch (ltype) {
+ case props::NONE:
+ return true;
+ case props::ALIAS:
+ return false; // XXX Should we look in aliases?
+ case props::BOOL:
+ return lhs.getValue<bool>() == rhs.getValue<bool>();
+ case props::INT:
+ return lhs.getValue<int>() == rhs.getValue<int>();
+ case props::LONG:
+ return lhs.getValue<long>() == rhs.getValue<long>();
+ case props::FLOAT:
+ return lhs.getValue<float>() == rhs.getValue<float>();
+ case props::DOUBLE:
+ return lhs.getValue<double>() == rhs.getValue<double>();
+ case props::STRING:
+ case props::UNSPECIFIED:
+ return !strcmp(lhs.getStringValue(), rhs.getStringValue());
+ case props::VEC3D:
+ return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
+ case props::VEC4D:
+ return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
+ default:
+ return false;
+ }
+}
+}
+}
+
+bool SGPropertyNode::compare(const SGPropertyNode& lhs,
+ const SGPropertyNode& rhs)
+{
+ if (&lhs == &rhs)
+ return true;
+ int lhsChildren = lhs.nChildren();
+ int rhsChildren = rhs.nChildren();
+ if (lhsChildren != rhsChildren)
+ return false;
+ if (lhsChildren == 0)
+ return compareNodeValue(lhs, rhs);
+ for (size_t i = 0; i < lhs._children.size(); ++i) {
+ const SGPropertyNode* lchild = lhs._children[i];
+ const SGPropertyNode* rchild = rhs._children[i];
+ // I'm guessing that the nodes will usually be in the same
+ // order.
+ if (lchild->getIndex() != rchild->getIndex()
+ || lchild->getNameString() != rchild->getNameString()) {
+ rchild = 0;
+ for (PropertyList::const_iterator itr = rhs._children.begin(),
+ end = rhs._children.end();
+ itr != end;
+ ++itr)
+ if (lchild->getIndex() == (*itr)->getIndex()
+ && lchild->getNameString() == (*itr)->getNameString()) {
+ rchild = *itr;
+ break;
+ }
+ if (!rchild)
+ return false;
+ }
+ if (!compare(*lchild, *rchild))
+ return false;
+ }
+ return true;
+}
+
+struct PropertyPlaceLess {
+ typedef bool result_type;
+ bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
+ {
+ int comp = lhs->getNameString().compare(rhs->getNameString());
+ if (comp == 0)
+ return lhs->getIndex() < rhs->getIndex();
+ else
+ return comp < 0;
+ }
+};
+
+size_t hash_value(const SGPropertyNode& node)
+{
+ using namespace boost;
+
+ if (node.nChildren() == 0) {
+ switch (node.getType()) {
+ case props::NONE:
+ return 0;
+
+ case props::BOOL:
+ return hash_value(node.getValue<bool>());
+ case props::INT:
+ return hash_value(node.getValue<int>());
+ case props::LONG:
+ return hash_value(node.getValue<long>());
+ case props::FLOAT:
+ return hash_value(node.getValue<float>());
+ case props::DOUBLE:
+ return hash_value(node.getValue<double>());
+ case props::STRING:
+ case props::UNSPECIFIED:
+ {
+ const char *val = node.getStringValue();
+ return hash_range(val, val + strlen(val));
+ }
+ case props::VEC3D:
+ {
+ const SGVec3d val = node.getValue<SGVec3d>();
+ return hash_range(&val[0], &val[3]);
+ }
+ case props::VEC4D:
+ {
+ const SGVec4d val = node.getValue<SGVec4d>();
+ return hash_range(&val[0], &val[4]);
+ }
+ case props::ALIAS: // XXX Should we look in aliases?
+ default:
+ return 0;
+ }
+ } else {
+ size_t seed = 0;
+ PropertyList children(node._children.begin(), node._children.end());
+ sort(children.begin(), children.end(), PropertyPlaceLess());
+ for (PropertyList::const_iterator itr = children.begin(),
+ end = children.end();
+ itr != end;
+ ++itr) {
+ hash_combine(seed, (*itr)->_name);
+ hash_combine(seed, (*itr)->_index);
+ hash_combine(seed, hash_value(**itr));
+ }
+ return seed;
+ }
}
// end of props.cxx