]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/props.cxx
Patch from Frederic Bouvier:
[simgear.git] / simgear / misc / props.cxx
index f9ddde3d743b924bf8f6920c40de550b2f829828..fd1fb6c3fb78e92cec917080632c34d3338f1de5 100644 (file)
@@ -8,6 +8,10 @@
 
 #include "props.hxx"
 
+#include <algorithm>
+#include <stdio.h>
+#include <string.h>
+
 #if PROPS_STANDALONE
 
 #include <iostream>
@@ -24,10 +28,6 @@ SG_USING_STD(sort);
 
 #endif
 
-#include <algorithm>
-#include <stdio.h>
-#include <string.h>
-
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -40,7 +40,7 @@ SG_USING_STD(sort);
 class CompareIndices
 {
 public:
-  int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
+  int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
     return (n1->getIndex() < n2->getIndex());
   }
 };
@@ -215,15 +215,14 @@ parse_path (const string &path, vector<PathComponent> &components)
 ////////////////////////////////////////////////////////////////////////
 
 
-static const char *
+static char *
 copy_string (const char * s)
 {
                                // FIXME: potential buffer overflow.
                                // For some reason, strnlen and
                                // strncpy cause all kinds of crashes.
-  string str = s;
-  char * copy = new char[str.size() + 1];
-  strcpy(copy, str.c_str());
+  char * copy = new char[strlen(s) + 1];
+  strcpy(copy, s);
   return copy;
 }
 
@@ -237,7 +236,7 @@ compare_strings (const char * s1, const char * s2)
  * Locate a child node by name and index.
  */
 static int
-find_child (const char * name, int index, vector<SGPropertyNode *> nodes)
+find_child (const char * name, int index, vector<SGPropertyNode_ptr> nodes)
 {
   int nNodes = nodes.size();
   for (int i = 0; i < nNodes; i++) {
@@ -418,7 +417,7 @@ SGPropertyNode::set_string (const char * val)
   if (_tied) {
     return _value.string_val->setValue(val);
   } else {
-    delete (char *)_local_val.string_val;
+    delete [] _local_val.string_val;
     _local_val.string_val = copy_string(val);
     return true;
   }
@@ -429,39 +428,53 @@ SGPropertyNode::clear_value ()
 {
   switch (_type) {
   case NONE:
+    break;
   case ALIAS:
     _value.alias = 0;
     break;
   case BOOL:
-    delete _value.bool_val;
-    _value.bool_val = 0;
+    if (_tied) {
+      delete _value.bool_val;
+      _value.bool_val = 0;
+    }
     _local_val.bool_val = SGRawValue<bool>::DefaultValue;
     break;
   case INT:
-    delete _value.int_val;
-    _value.int_val = 0;
+    if (_tied) {
+      delete _value.int_val;
+      _value.int_val = 0;
+    }
     _local_val.int_val = SGRawValue<int>::DefaultValue;
     break;
   case LONG:
-    delete _value.long_val;
-    _value.long_val = 0L;
+    if (_tied) {
+      delete _value.long_val;
+      _value.long_val = 0L;
+    }
     _local_val.long_val = SGRawValue<long>::DefaultValue;
     break;
   case FLOAT:
-    delete _value.float_val;
-    _value.float_val = 0;
+    if (_tied) {
+      delete _value.float_val;
+      _value.float_val = 0;
+    }
     _local_val.float_val = SGRawValue<float>::DefaultValue;
     break;
   case DOUBLE:
-    delete _value.double_val;
-    _value.double_val = 0;
+    if (_tied) {
+      delete _value.double_val;
+      _value.double_val = 0;
+    }
     _local_val.double_val = SGRawValue<double>::DefaultValue;
     break;
   case STRING:
   case UNSPECIFIED:
-    delete _value.string_val;
-    _value.string_val = 0;
-    delete (char *)_local_val.string_val;
+    if (_tied) {
+      delete _value.string_val;
+      _value.string_val = 0;
+    } else {
+      delete [] _local_val.string_val;
+    }
     _local_val.string_val = 0;
     break;
   }
@@ -538,12 +551,36 @@ SGPropertyNode::trace_read () const
 #endif
 }
 
+/**
+ * Increment reference counter
+ */
+void
+SGPropertyNode::incrementRef()
+{
+  ++_count;
+}
+
+/**
+ * Decrement reference counter
+ */
+int
+SGPropertyNode::decrementRef()
+{
+  return --_count;
+}
+
 
 \f
 ////////////////////////////////////////////////////////////////////////
 // Public methods from SGPropertyNode.
 ////////////////////////////////////////////////////////////////////////
 
+/**
+ * Last used attribute
+ * Update as needed when enum Attribute is changed
+ */
+const int SGPropertyNode::LAST_USED_ATTRIBUTE = TRACE_WRITE;
+
 /**
  * Default constructor: always creates a root node.
  */
@@ -554,7 +591,8 @@ SGPropertyNode::SGPropertyNode ()
     _path_cache(0),
     _type(NONE),
     _tied(false),
-    _attr(READ|WRITE)
+    _attr(READ|WRITE),
+    _count(0)
 {
   _local_val.string_val = 0;
 }
@@ -569,7 +607,8 @@ SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
     _path_cache(0),
     _type(node._type),
     _tied(node._tied),
-    _attr(node._attr)
+    _attr(node._attr),
+    _count(0)
 {
   _name = copy_string(node._name);
   _local_val.string_val = 0;
@@ -650,7 +689,8 @@ SGPropertyNode::SGPropertyNode (const char * name,
     _path_cache(0),
     _type(NONE),
     _tied(false),
-    _attr(READ|WRITE)
+    _attr(READ|WRITE),
+    _count(0)
 {
   _name = copy_string(name);
   _local_val.string_val = 0;
@@ -662,11 +702,8 @@ SGPropertyNode::SGPropertyNode (const char * name,
  */
 SGPropertyNode::~SGPropertyNode ()
 {
-  delete (char *)_name;
-  for (int i = 0; i < (int)_children.size(); i++) {
-    delete _children[i];
-  }
-//   delete _path_cache;
+  delete [] _name;
+  delete _path_cache;
   clear_value();
 }
 
@@ -763,7 +800,18 @@ SGPropertyNode::getChild (const char * name, int index, bool create)
   if (pos >= 0) {
     return _children[pos];
   } else if (create) {
-    _children.push_back(new SGPropertyNode(name, index, this));
+    SGPropertyNode_ptr node;
+    pos = find_child(name, index, _removedChildren);
+    if (pos >= 0) {
+      std::vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
+      it += pos;
+      node = _removedChildren[pos];
+      _removedChildren.erase(it);
+      node->setAttribute(REMOVED, false);
+    } else {
+      node = new SGPropertyNode(name, index, this);
+    }
+    _children.push_back(node);
     return _children[_children.size()-1];
   } else {
     return 0;
@@ -788,10 +836,10 @@ SGPropertyNode::getChild (const char * name, int index) const
 /**
  * Get all children with the same name (but different indices).
  */
-vector<SGPropertyNode *>
-SGPropertyNode::getChildren (const char * name)
+vector<SGPropertyNode_ptr>
+SGPropertyNode::getChildren (const char * name) const
 {
-  vector<SGPropertyNode *> children;
+  vector<SGPropertyNode_ptr> children;
   int max = _children.size();
 
   for (int i = 0; i < max; i++)
@@ -804,20 +852,25 @@ SGPropertyNode::getChildren (const char * name)
 
 
 /**
- * Get all children const with the same name (but different indices).
+ * Remove a child node
  */
-vector<const SGPropertyNode *>
-SGPropertyNode::getChildren (const char * name) const
+SGPropertyNode_ptr 
+SGPropertyNode::removeChild (const char * name, int index, bool keep)
 {
-  vector<const SGPropertyNode *> children;
-  int max = _children.size();
-
-  for (int i = 0; i < max; i++)
-    if (compare_strings(_children[i]->getName(), name))
-      children.push_back(_children[i]);
-
-  sort(children.begin(), children.end(), CompareIndices());
-  return children;
+  SGPropertyNode_ptr ret;
+  int pos = find_child(name, index, _children);
+  if (pos >= 0) {
+    std::vector<SGPropertyNode_ptr>::iterator it = _children.begin();
+    it += pos;
+    SGPropertyNode_ptr node = _children[pos];
+    _children.erase(it);
+    if (keep) {
+      _removedChildren.push_back(node);
+    }
+    node->setAttribute(REMOVED, true);
+    ret = node;
+  }
+  return ret;
 }
 
 
@@ -1585,22 +1638,19 @@ SGPropertyNode::getRootNode () const
 SGPropertyNode *
 SGPropertyNode::getNode (const char * 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);
-//     if (result != 0)
-//       (*_path_cache)[relative_path] = result;
-//   }
+  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);
+    if (result != 0)
+      _path_cache->put(relative_path, result);
+  }
   
-//   return result;
-  vector<PathComponent> components;
-  parse_path(relative_path, components);
-  return find_node(this, components, 0, create);
+  return result;
 }
 
 SGPropertyNode *
@@ -1889,4 +1939,227 @@ SGPropertyNode::untie (const char * relative_path)
   return (node == 0 ? false : node->untie());
 }
 
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Simplified hash table for caching paths.
+////////////////////////////////////////////////////////////////////////
+
+#define HASH_TABLE_SIZE 199
+
+SGPropertyNode::hash_table::entry::entry ()
+  : _key(0),
+    _value(0)
+{
+}
+
+SGPropertyNode::hash_table::entry::~entry ()
+{
+                               // Don't delete the value; we don't own
+                               // the pointer.
+  delete [] _key;
+}
+
+void
+SGPropertyNode::hash_table::entry::set_key (const char * key)
+{
+  _key = copy_string(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];
+}
+
+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;
+  }
+}
+
+
+SGPropertyNode::hash_table::hash_table ()
+  : _data_length(0),
+    _data(0)
+{
+}
+
+SGPropertyNode::hash_table::~hash_table ()
+{
+  for (unsigned int i = 0; i < _data_length; i++)
+    delete _data[i];
+}
+
+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);
+}
+
+unsigned int
+SGPropertyNode::hash_table::hashcode (const char * key)
+{
+  unsigned int hash = 0;
+  while (*key != 0) {
+    hash = 31 * hash + *key;
+    key++;
+  }
+  return hash;
+}
+
+
+
+/**
+ * Default constructor
+ */
+SGPropertyNode_ptr::SGPropertyNode_ptr()
+{
+  _ptr = 0;
+}
+
+/**
+ * Copy constructor
+ */
+SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
+{
+  _ptr = r._ptr;
+  if (_ptr)
+     _ptr->incrementRef();
+}
+
+/**
+ * Constructor from a pointer to a node
+ */
+SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
+{
+  _ptr = p;
+  if (_ptr)
+     _ptr->incrementRef();
+}
+
+/**
+ * Destructor
+ */
+SGPropertyNode_ptr::~SGPropertyNode_ptr()
+{
+  if (_ptr && _ptr->decrementRef() == 0)
+    delete _ptr;
+}
+
+/**
+ * Assignement operator
+ */
+SGPropertyNode_ptr &
+SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
+{
+  if (_ptr && _ptr->decrementRef() == 0)
+    delete _ptr;
+  _ptr = r._ptr;
+  if (_ptr)
+     _ptr->incrementRef();
+
+  return *this;
+}
+
+/**
+ * Pointer access operator
+ */
+SGPropertyNode *
+SGPropertyNode_ptr::operator->()
+{
+  return _ptr;
+}
+
+/**
+ * Pointer access operator (const)
+ */
+const SGPropertyNode *
+SGPropertyNode_ptr::operator->() const
+{
+  return _ptr;
+}
+
+/**
+ * Conversion to SGPropertyNode * operator
+ */
+SGPropertyNode_ptr::operator SGPropertyNode *()
+{
+  return _ptr;
+}
+
+/**
+ * Conversion to const SGPropertyNode * operator
+ */
+SGPropertyNode_ptr::operator const SGPropertyNode *() const
+{
+  return _ptr;
+}
+
+/**
+ * Validity test
+ */
+bool 
+SGPropertyNode_ptr::valid() const
+{
+  return _ptr != 0;
+}
+
+
 // end of props.cxx