#endif
#include "props.hxx"
+#include "vectorPropTemplates.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
#include <iostream>
#else
template<typename Range>
inline Range
-parse_name (const Range &path)
+parse_name (const SGPropertyNode *node, const Range &path)
{
typename Range::iterator i = path.begin();
typename Range::iterator max = path.end();
} 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 (path.begin() == i)
- throw string("name must begin with alpha or '_'");
+ 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;
+ }
}
return Range(path.begin(), i);
}
// 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(token);
+ Range name = parse_name(current, token);
if (equals(name, "."))
return find_node_aux(current, ++itr, create, last_index);
if (equals(name, "..")) {
* Last used attribute
* Update as needed when enum Attribute is changed
*/
-const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
+const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
/**
* Default constructor: always creates a root node.
SGPropertyNode::SGPropertyNode ()
: _index(0),
_parent(0),
- _path_cache(0),
_type(props::NONE),
_tied(false),
_attr(READ|WRITE),
* Copy constructor.
*/
SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
- : _index(node._index),
+ : SGReferenced(node),
+ _index(node._index),
_name(node._name),
_parent(0), // don't copy the parent
- _path_cache(0),
_type(node._type),
_tied(node._tied),
_attr(node._attr),
: _index(index),
_name(begin, end),
_parent(parent),
- _path_cache(0),
_type(props::NONE),
_tied(false),
_attr(READ|WRITE),
: _index(index),
_name(name),
_parent(parent),
- _path_cache(0),
_type(props::NONE),
_tied(false),
_attr(READ|WRITE),
_children[i]->_parent = 0;
for (unsigned i = 0; i < _removedChildren.size(); ++i)
_removedChildren[i]->_parent = 0;
- delete _path_cache;
clearValue();
if (_listeners) {
bool
SGPropertyNode::alias (SGPropertyNode * target)
{
- if (target == 0 || _type == props::ALIAS || _tied)
- return false;
- clearValue();
- get(target);
- _value.alias = target;
- _type = props::ALIAS;
- return true;
+ if (target && (_type != props::ALIAS) && (!_tied))
+ {
+ clearValue();
+ get(target);
+ _value.alias = target;
+ _type = props::ALIAS;
+ return true;
+ }
+
+#if PROPS_STANDALONE
+#else
+ if (!target)
+ {
+ SG_LOG(SG_GENERAL, SG_ALERT,
+ "Failed to create alias for " << getPath() << ". "
+ "The target property does not exist.");
+ }
+ else
+ if (_type == props::ALIAS)
+ {
+ if (_value.alias == target)
+ return true; // ok, identical alias requested
+ SG_LOG(SG_GENERAL, SG_ALERT,
+ "Failed to create alias at " << target->getPath() << ". "
+ "Source "<< getPath() << " is already aliasing another property.");
+ }
+ else
+ if (_tied)
+ {
+ SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
+ "Source " << getPath() << " is a tied property.");
+ }
+#endif
+
+ return false;
}
}
-/**
- * Remove this node and all children from nodes that link to them
- * in their path cache.
- */
-void
-SGPropertyNode::remove_from_path_caches ()
-{
- for (unsigned int i = 0; i < _children.size(); ++i)
- _children[i]->remove_from_path_caches();
-
- for (unsigned int i = 0; i < _linkedNodes.size(); i++)
- _linkedNodes[i]->erase(this);
- _linkedNodes.clear();
-}
-
-
/**
* Remove child by position.
*/
_removedChildren.push_back(node);
}
- node->remove_from_path_caches();
node->setAttribute(REMOVED, true);
node->clearValue();
fireChildRemoved(node);
return children;
}
-
-/**
- * Remove a linked node.
- */
-bool
-SGPropertyNode::remove_linked_node (hash_table * node)
-{
- for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
- if (_linkedNodes[i] == node) {
- vector<hash_table *>::iterator it = _linkedNodes.begin();
- it += i;
- _linkedNodes.erase(it);
- return true;
- }
- }
- return false;
-}
-
-
string
SGPropertyNode::getDisplayName (bool simplify) const
{
return display_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;
}
props::Type
_tied = true;
_value.val = rawValue.clone();
- if (useDefault)
+ if (useDefault) {
+ int save_attributes = getAttributes();
+ setAttribute( WRITE, true );
setStringValue(old_val.c_str());
+ setAttributes( save_attributes );
+ }
return true;
}
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) {
- result = find_node(this,
- make_iterator_range(relative_path, relative_path
- + strlen(relative_path)),
- create);
- if (result != 0)
- _path_cache->put(relative_path, result);
- }
- return result;
+ return find_node(this, make_iterator_range(relative_path, relative_path
+ + strlen(relative_path)),
+ create);
}
SGPropertyNode *
void
SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
{
+ if (_listeners == 0)
+ return;
vector<SGPropertyChangeListener*>::iterator it =
find(_listeners->begin(), _listeners->end(), listener);
if (it != _listeners->end()) {
_parent->fireChildRemoved(parent, child);
}
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Simplified hash table for caching paths.
-////////////////////////////////////////////////////////////////////////
-
-#define HASH_TABLE_SIZE 199
-
-SGPropertyNode::hash_table::entry::entry ()
- : _value(0)
-{
-}
-
-SGPropertyNode::hash_table::entry::~entry ()
-{
- // Don't delete the value; we don't own
- // the pointer.
-}
-
-void
-SGPropertyNode::hash_table::entry::set_key (const char * key)
-{
- _key = key;
-}
-
-void
-SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
-{
- _value = value;
-}
-
-SGPropertyNode::hash_table::bucket::bucket ()
- : _length(0),
- _entries(0)
-{
-}
-
-SGPropertyNode::hash_table::bucket::~bucket ()
-{
- for (int i = 0; i < _length; i++)
- delete _entries[i];
- delete [] _entries;
-}
-
-SGPropertyNode::hash_table::entry *
-SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
-{
- int i;
- for (i = 0; i < _length; i++) {
- if (!strcmp(_entries[i]->get_key(), key))
- return _entries[i];
- }
- if (create) {
- entry ** new_entries = new entry*[_length+1];
- for (i = 0; i < _length; i++) {
- new_entries[i] = _entries[i];
- }
- delete [] _entries;
- _entries = new_entries;
- _entries[_length] = new entry;
- _entries[_length]->set_key(key);
- _length++;
- return _entries[_length - 1];
- } else {
- return 0;
- }
-}
-
-bool
-SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
-{
- for (int i = 0; i < _length; i++) {
- if (_entries[i]->get_value() == node) {
- delete _entries[i];
- for (++i; i < _length; i++) {
- _entries[i-1] = _entries[i];
- }
- _length--;
- return true;
- }
- }
- return false;
-}
-
-void
-SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
-{
- for (int i = 0; i < _length; i++) {
- SGPropertyNode * node = _entries[i]->get_value();
- if (node)
- node->remove_linked_node(owner);
- }
-}
-
-SGPropertyNode::hash_table::hash_table ()
- : _data_length(0),
- _data(0)
-{
-}
-
-SGPropertyNode::hash_table::~hash_table ()
-{
- for (unsigned int i = 0; i < _data_length; i++) {
- if (_data[i]) {
- _data[i]->clear(this);
- delete _data[i];
- }
- }
- delete [] _data;
-}
-
-SGPropertyNode *
-SGPropertyNode::hash_table::get (const char * key)
-{
- if (_data_length == 0)
- return 0;
- unsigned int index = hashcode(key) % _data_length;
- if (_data[index] == 0)
- return 0;
- entry * e = _data[index]->get_entry(key);
- if (e == 0)
- return 0;
- else
- return e->get_value();
-}
-
-void
-SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
-{
- if (_data_length == 0) {
- _data = new bucket*[HASH_TABLE_SIZE];
- _data_length = HASH_TABLE_SIZE;
- for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
- _data[i] = 0;
- }
- unsigned int index = hashcode(key) % _data_length;
- if (_data[index] == 0) {
- _data[index] = new bucket;
- }
- entry * e = _data[index]->get_entry(key, true);
- e->set_value(value);
- value->add_linked_node(this);
-}
-
-bool
-SGPropertyNode::hash_table::erase (SGPropertyNode * node)
-{
- for (unsigned int i = 0; i < _data_length; i++)
- if (_data[i] && _data[i]->erase(node))
- return true;
-
- return false;
-}
-
-unsigned int
-SGPropertyNode::hash_table::hashcode (const char * key)
-{
- unsigned int hash = 0;
- while (*key != 0) {
- hash = 31 * hash + *key;
- key++;
- }
- return hash;
-}
-
-
\f
////////////////////////////////////////////////////////////////////////
// Implementation of SGPropertyChangeListener.
}
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