]> git.mxchange.org Git - simgear.git/blobdiff - simgear/props/props.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / props / props.cxx
index 09b71fb44c6b91a158fdefd0a86707d65f665cd7..9817d547ebe6a79d72f4fedd5cbb954f9621a553 100644 (file)
@@ -11,6 +11,7 @@
 #endif
 
 #include "props.hxx"
+#include "PropertyInterpolationMgr.hxx"
 #include "vectorPropTemplates.hxx"
 
 #include <algorithm>
@@ -51,13 +52,11 @@ using std::cerr;
 using std::endl;
 using std::find;
 using std::sort;
-using std::string;
 using std::vector;
 using std::stringstream;
 
 using namespace simgear;
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Local classes.
 ////////////////////////////////////////////////////////////////////////
@@ -74,14 +73,12 @@ public:
 };
 
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Convenience macros for value access.
 ////////////////////////////////////////////////////////////////////////
 
 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
 #define TEST_WRITE if (!getAttribute(WRITE)) return false
-\f
 
 ////////////////////////////////////////////////////////////////////////
 // Local path normalization code.
@@ -106,7 +103,7 @@ parse_name (const SGPropertyNode *node, const Range &path)
       i++;
     }
     if (i != max && *i != '/')
-      throw string("illegal character after . or ..");
+      throw std::string("illegal character after . or ..");
   } else if (isalpha(*i) || *i == '_') {
     i++;
 
@@ -119,7 +116,7 @@ parse_name (const SGPropertyNode *node, const Range &path)
       } else if (*i == '[' || *i == '/') {
        break;
       } else {
-        string err = "'";
+        std::string err = "'";
         err.push_back(*i);
         err.append("' found in propertyname after '"+node->getNameString()+"'");
         err.append("\nname may contain only ._- and alphanumeric characters");
@@ -131,7 +128,7 @@ parse_name (const SGPropertyNode *node, const Range &path)
 
   else {
     if (path.begin() == i) {
-      string err = "'";
+      std::string err = "'";
       err.push_back(*i);
       err.append("' found in propertyname after '"+node->getNameString()+"'");
       err.append("\nname must begin with alpha or '_'");
@@ -142,7 +139,7 @@ parse_name (const SGPropertyNode *node, const Range &path)
 }
 
 // Validate the name of a single node
-inline bool validateName(const string& name)
+inline bool validateName(const std::string& name)
 {
   using namespace boost;
   if (name.empty())
@@ -153,7 +150,6 @@ inline bool validateName(const string& name)
              is_alnum() || is_any_of("_-."));
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Other static utility functions.
 ////////////////////////////////////////////////////////////////////////
@@ -162,7 +158,7 @@ inline bool validateName(const string& name)
 static char *
 copy_string (const char * s)
 {
-  unsigned long int slen = strlen(s);
+  size_t slen = strlen(s);
   char * copy = new char[slen + 1];
 
   // the source string length is known so no need to check for '\0'
@@ -185,15 +181,15 @@ template<typename Itr>
 static int
 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
 {
-  int nNodes = nodes.size();
+  size_t nNodes = nodes.size();
   boost::iterator_range<Itr> name(begin, end);
-  for (int i = 0; i < nNodes; i++) {
+  for (size_t i = 0; i < nNodes; i++) {
     SGPropertyNode * node = nodes[i];
 
-    // searching for a mathing index is a lot less time consuming than
+    // searching for a matching index is a lot less time consuming than
     // comparing two strings so do that first.
     if (node->getIndex() == index && boost::equals(node->getName(), name))
-      return i;
+      return static_cast<int>(i);
   }
   return -1;
 }
@@ -204,10 +200,10 @@ find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
 static int
 find_last_child (const char * name, const PropertyList& nodes)
 {
-  int nNodes = nodes.size();
-  int index = 0;
+  size_t nNodes = nodes.size();
+  int index = -1;
 
-  for (int i = 0; i < nNodes; i++) {
+  for (size_t i = 0; i < nNodes; i++) {
     SGPropertyNode * node = nodes[i];
     if (compare_strings(node->getName(), name))
     {
@@ -234,39 +230,25 @@ first_unused_index( const char * name,
       return index;
   }
 
-  SG_LOG(SG_GENERAL, SG_ALERT, "Too much nodes: " << name);
+  SG_LOG(SG_GENERAL, SG_ALERT, "Too many nodes: " << name);
   return -1;
 }
 
 template<typename Itr>
 inline SGPropertyNode*
-SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
+SGPropertyNode::getExistingChild (Itr begin, Itr end, int index)
 {
   int pos = find_child(begin, end, index, _children);
-  if (pos >= 0) {
+  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);
+    SGPropertyNode* node = getExistingChild(begin, end, index);
 
     if (node) {
       return node;
@@ -304,7 +286,7 @@ find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
   if (equals(name, "..")) {
     SGPropertyNode * parent = current->getParent();
     if (parent == 0)
-      throw string("attempt to move past root with '..'");
+      throw std::string("attempt to move past root with '..'");
     return find_node_aux(parent, ++itr, create, last_index);
   }
   int index = -1;
@@ -337,10 +319,10 @@ find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
           }
         }
         if (i == token.end() || *i != ']')
-          throw string("unterminated index (looking for ']')");
+          throw std::string("unterminated index (looking for ']')");
       } else {
-        throw string("illegal characters in token: ")
-          + string(name.begin(), name.end());
+        throw std::string("illegal characters in token: ")
+          + std::string(name.begin(), name.end());
       }
     }
   }
@@ -370,7 +352,6 @@ find_node (SGPropertyNode * current,
      return find_node_aux(current, itr, create, last_index);
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Private methods from SGPropertyNode (may be inlined for speed).
 ////////////////////////////////////////////////////////////////////////
@@ -654,7 +635,6 @@ SGPropertyNode::trace_read () const
 #endif
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Public methods from SGPropertyNode.
 ////////////////////////////////////////////////////////////////////////
@@ -752,12 +732,12 @@ SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
   _local_val.string_val = 0;
   _value.val = 0;
   if (!validateName(_name))
-    throw string("plain name expected instead of '") + _name + '\'';
+    throw std::string("plain name expected instead of '") + _name + '\'';
 }
 
-SGPropertyNode::SGPropertyNode (const string& name,
-                               int index,
-                               SGPropertyNode * parent)
+SGPropertyNode::SGPropertyNode( const std::string& name,
+                                int index,
+                                SGPropertyNode * parent)
   : _index(index),
     _name(name),
     _parent(parent),
@@ -769,7 +749,7 @@ SGPropertyNode::SGPropertyNode (const string& name,
   _local_val.string_val = 0;
   _value.val = 0;
   if (!validateName(name))
-    throw string("plain name expected instead of '") + _name + '\'';
+    throw std::string("plain name expected instead of '") + _name + '\'';
 }
 
 /**
@@ -780,8 +760,6 @@ SGPropertyNode::~SGPropertyNode ()
   // zero out all parent pointers, else they might be dangling
   for (unsigned i = 0; i < _children.size(); ++i)
     _children[i]->_parent = 0;
-  for (unsigned i = 0; i < _removedChildren.size(); ++i)
-    _removedChildren[i]->_parent = 0;
   clearValue();
 
   if (_listeners) {
@@ -977,10 +955,9 @@ SGPropertyNode::getChild (const char * name, int index, bool create)
 }
 
 SGPropertyNode *
-SGPropertyNode::getChild (const string& name, int index, bool create)
+SGPropertyNode::getChild (const std::string& name, int index, bool create)
 {
-  SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
-                                          create);
+  SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index);
   if (node) {
       return node;
     } else if (create) {
@@ -1014,9 +991,9 @@ PropertyList
 SGPropertyNode::getChildren (const char * name) const
 {
   PropertyList children;
-  int max = _children.size();
+  size_t max = _children.size();
 
-  for (int i = 0; i < max; i++)
+  for (size_t i = 0; i < max; i++)
     if (compare_strings(_children[i]->getName(), name))
       children.push_back(_children[i]);
 
@@ -1024,29 +1001,28 @@ SGPropertyNode::getChildren (const char * name) const
   return children;
 }
 
+//------------------------------------------------------------------------------
+bool SGPropertyNode::removeChild(SGPropertyNode* node)
+{
+  if( node->_parent != this )
+    return false;
+
+  PropertyList::iterator it =
+    std::find(_children.begin(), _children.end(), node);
+  if( it == _children.end() )
+    return false;
 
-/**
- * Remove child by position.
- */
-SGPropertyNode_ptr
-SGPropertyNode::removeChild (int pos, bool keep)
+  eraseChild(it);
+  return true;
+}
+
+//------------------------------------------------------------------------------
+SGPropertyNode_ptr SGPropertyNode::removeChild(int pos)
 {
-  SGPropertyNode_ptr node;
   if (pos < 0 || pos >= (int)_children.size())
-    return node;
+    return SGPropertyNode_ptr();
 
-  PropertyList::iterator it = _children.begin();
-  it += pos;
-  node = _children[pos];
-  _children.erase(it);
-  if (keep) {
-    _removedChildren.push_back(node);
-  }
-
-  node->setAttribute(REMOVED, true);
-  node->clearValue();
-  fireChildRemoved(node);
-  return node;
+  return eraseChild(_children.begin() + pos);
 }
 
 
@@ -1054,12 +1030,12 @@ SGPropertyNode::removeChild (int pos, bool keep)
  * Remove a child node
  */
 SGPropertyNode_ptr
-SGPropertyNode::removeChild (const char * name, int index, bool keep)
+SGPropertyNode::removeChild(const char * name, int index)
 {
   SGPropertyNode_ptr ret;
   int pos = find_child(name, name + strlen(name), index, _children);
   if (pos >= 0)
-    ret = removeChild(pos, keep);
+    ret = removeChild(pos);
   return ret;
 }
 
@@ -1068,22 +1044,37 @@ SGPropertyNode::removeChild (const char * name, int index, bool keep)
   * Remove all children with the specified name.
   */
 PropertyList
-SGPropertyNode::removeChildren (const char * name, bool keep)
+SGPropertyNode::removeChildren(const char * name)
 {
   PropertyList children;
 
-  for (int pos = _children.size() - 1; pos >= 0; pos--)
+  for (int pos = static_cast<int>(_children.size() - 1); pos >= 0; pos--)
     if (compare_strings(_children[pos]->getName(), name))
-      children.push_back(removeChild(pos, keep));
+      children.push_back(removeChild(pos));
 
   sort(children.begin(), children.end(), CompareIndices());
   return children;
 }
 
-string
+void
+SGPropertyNode::removeAllChildren()
+{
+  for(unsigned i = 0; i < _children.size(); ++i)
+  {
+    SGPropertyNode_ptr& node = _children[i];
+    node->_parent = 0;
+    node->setAttribute(REMOVED, true);
+    node->clearValue();
+    fireChildRemoved(node);
+  }
+
+  _children.clear();
+}
+
+std::string
 SGPropertyNode::getDisplayName (bool simplify) const
 {
-  string display_name = _name;
+  std::string display_name = _name;
   if (_index != 0 || !simplify) {
     stringstream sstr;
     sstr << '[' << _index << ']';
@@ -1092,14 +1083,14 @@ SGPropertyNode::getDisplayName (bool simplify) const
   return display_name;
 }
 
-string
+std::string
 SGPropertyNode::getPath (bool simplify) const
 {
   typedef std::vector<SGConstPropertyNode_ptr> PList;
   PList pathList;
   for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
     pathList.push_back(node);
-  string result;
+  std::string result;
   for (PList::reverse_iterator itr = pathList.rbegin(),
          rend = pathList.rend();
        itr != rend;
@@ -1660,6 +1651,58 @@ SGPropertyNode::setUnspecifiedValue (const char * value)
   return result;
 }
 
+//------------------------------------------------------------------------------
+bool SGPropertyNode::interpolate( const std::string& type,
+                                  const SGPropertyNode& target,
+                                  double duration,
+                                  const std::string& easing )
+{
+  if( !_interpolation_mgr )
+  {
+    SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
+
+    // no interpolation possible -> set to target immediately
+    setUnspecifiedValue( target.getStringValue() );
+    return false;
+  }
+
+  return _interpolation_mgr->interpolate(this, type, target, duration, easing);
+}
+
+//------------------------------------------------------------------------------
+bool SGPropertyNode::interpolate( const std::string& type,
+                                  const simgear::PropertyList& values,
+                                  const double_list& deltas,
+                                  const std::string& easing )
+{
+  if( !_interpolation_mgr )
+  {
+    SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
+
+    // no interpolation possible -> set to last value immediately
+    if( !values.empty() )
+      setUnspecifiedValue(values.back()->getStringValue());
+    return false;
+  }
+
+  return _interpolation_mgr->interpolate(this, type, values, deltas, easing);
+}
+
+//------------------------------------------------------------------------------
+void SGPropertyNode::setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr)
+{
+  _interpolation_mgr = mgr;
+}
+
+//------------------------------------------------------------------------------
+simgear::PropertyInterpolationMgr* SGPropertyNode::getInterpolationMgr()
+{
+  return _interpolation_mgr;
+}
+
+simgear::PropertyInterpolationMgr* SGPropertyNode::_interpolation_mgr = 0;
+
+//------------------------------------------------------------------------------
 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
 {
     if (!getAttribute(READ))
@@ -1766,7 +1809,7 @@ SGPropertyNode::untie ()
   }
   case props::STRING:
   case props::UNSPECIFIED: {
-    string val = getStringValue();
+    std::string val = getStringValue();
     clearValue();
     _type = props::STRING;
     _local_val.string_val = copy_string(val.c_str());
@@ -1839,7 +1882,6 @@ SGPropertyNode::getNode (const char * relative_path, int index) const
   return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Convenience methods using relative paths.
 ////////////////////////////////////////////////////////////////////////
@@ -2145,12 +2187,38 @@ SGPropertyNode::fireChildAdded (SGPropertyNode * child)
   fireChildAdded(this, child);
 }
 
+void
+SGPropertyNode::fireCreatedRecursive(bool fire_self)
+{
+  if( fire_self )
+  {
+    _parent->fireChildAdded(this);
+
+    if( _children.empty() && getType() != simgear::props::NONE )
+      return fireValueChanged();
+  }
+
+  for(size_t i = 0; i < _children.size(); ++i)
+    _children[i]->fireCreatedRecursive(true);
+}
+
 void
 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
 {
   fireChildRemoved(this, child);
 }
 
+void
+SGPropertyNode::fireChildrenRemovedRecursive()
+{
+  for(size_t i = 0; i < _children.size(); ++i)
+  {
+    SGPropertyNode* child = _children[i];
+    fireChildRemoved(this, child);
+    child->fireChildrenRemovedRecursive();
+  }
+}
+
 void
 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
 {
@@ -2189,14 +2257,26 @@ SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
     _parent->fireChildRemoved(parent, child);
 }
 
-\f
+//------------------------------------------------------------------------------
+SGPropertyNode_ptr
+SGPropertyNode::eraseChild(simgear::PropertyList::iterator child)
+{
+  SGPropertyNode_ptr node = *child;
+  node->setAttribute(REMOVED, true);
+  node->clearValue();
+  fireChildRemoved(node);
+
+  _children.erase(child);
+  return node;
+}
+
 ////////////////////////////////////////////////////////////////////////
 // Implementation of SGPropertyChangeListener.
 ////////////////////////////////////////////////////////////////////////
 
 SGPropertyChangeListener::~SGPropertyChangeListener ()
 {
-  for (int i = _properties.size() - 1; i >= 0; i--)
+  for (int i = static_cast<int>(_properties.size() - 1); i >= 0; i--)
     _properties[i]->removeChangeListener(this);
 }