]> git.mxchange.org Git - simgear.git/blobdiff - simgear/props/props.cxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / props / props.cxx
index 12a8fe833fc4c354ed2bf25febc020f904499f8a..d9c90a33c814a09815703588b42ed6ae5d2033f4 100644 (file)
 #endif
 
 #include "props.hxx"
+#include "PropertyInterpolationMgr.hxx"
+#include "vectorPropTemplates.hxx"
 
 #include <algorithm>
+#include <limits>
 
+#include <set>
 #include <sstream>
 #include <iomanip>
 #include <iterator>
@@ -27,8 +31,6 @@
 #include <boost/functional/hash.hpp>
 #include <boost/range.hpp>
 
-#include <simgear/math/SGMath.hxx>
-
 #if PROPS_STANDALONE
 #include <iostream>
 #else
@@ -56,7 +58,6 @@ using std::stringstream;
 
 using namespace simgear;
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Local classes.
 ////////////////////////////////////////////////////////////////////////
@@ -73,14 +74,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.
@@ -152,7 +151,6 @@ inline bool validateName(const string& name)
              is_alnum() || is_any_of("_-."));
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Other static utility functions.
 ////////////////////////////////////////////////////////////////////////
@@ -161,7 +159,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'
@@ -184,15 +182,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;
 }
@@ -203,10 +201,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))
     {
@@ -217,35 +215,41 @@ find_last_child (const char * name, const PropertyList& nodes)
   return index;
 }
 
+/**
+ * Get first unused index for child nodes with the given name
+ */
+static int
+first_unused_index( const char * name,
+                    const PropertyList& nodes,
+                    int min_index )
+{
+  const char* nameEnd = name + strlen(name);
+
+  for( int index = min_index; index < std::numeric_limits<int>::max(); ++index )
+  {
+    if( find_child(name, nameEnd, index, nodes) < 0 )
+      return index;
+  }
+
+  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;
@@ -349,7 +353,6 @@ find_node (SGPropertyNode * current,
      return find_node_aux(current, itr, create, last_index);
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Private methods from SGPropertyNode (may be inlined for speed).
 ////////////////////////////////////////////////////////////////////////
@@ -633,7 +636,6 @@ SGPropertyNode::trace_read () const
 #endif
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Public methods from SGPropertyNode.
 ////////////////////////////////////////////////////////////////////////
@@ -642,7 +644,7 @@ SGPropertyNode::trace_read () const
  * 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.
@@ -664,7 +666,8 @@ SGPropertyNode::SGPropertyNode ()
  * 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
     _type(node._type),
@@ -758,8 +761,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) {
@@ -777,13 +778,41 @@ SGPropertyNode::~SGPropertyNode ()
 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;
 }
 
 
@@ -830,9 +859,11 @@ SGPropertyNode::getAliasTarget () const
  * create a non-const child by name after the last node with the same name.
  */
 SGPropertyNode *
-SGPropertyNode::addChild (const char * name)
+SGPropertyNode::addChild(const char * name, int min_index, bool append)
 {
-  int pos = find_last_child(name, _children)+1;
+  int pos = append
+          ? std::max(find_last_child(name, _children) + 1, min_index)
+          : first_unused_index(name, _children, min_index);
 
   SGPropertyNode_ptr node;
   node = new SGPropertyNode(name, name + strlen(name), pos, this);
@@ -841,6 +872,52 @@ SGPropertyNode::addChild (const char * name)
   return node;
 }
 
+/**
+ * Create multiple children with unused indices
+ */
+simgear::PropertyList
+SGPropertyNode::addChildren( const std::string& name,
+                             size_t count,
+                             int min_index,
+                             bool append )
+{
+  simgear::PropertyList nodes;
+  std::set<int> used_indices;
+
+  if( !append )
+  {
+    // First grab all used indices. This saves us of testing every index if it
+    // is used for every element to be created
+    for( size_t i = 0; i < nodes.size(); i++ )
+    {
+      const SGPropertyNode* node = nodes[i];
+
+      if( node->getNameString() == name && node->getIndex() >= min_index )
+        used_indices.insert(node->getIndex());
+    }
+  }
+  else
+  {
+    // If we don't want to fill the holes just find last node
+    min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index);
+  }
+
+  for( int index = min_index;
+            index < std::numeric_limits<int>::max() && nodes.size() < count;
+          ++index )
+  {
+    if( used_indices.find(index) == used_indices.end() )
+    {
+      SGPropertyNode_ptr node;
+      node = new SGPropertyNode(name, index, this);
+      _children.push_back(node);
+      fireChildAdded(node);
+      nodes.push_back(node);
+    }
+  }
+
+  return nodes;
+}
 
 /**
  * Get a non-const child by index.
@@ -881,8 +958,7 @@ SGPropertyNode::getChild (const char * name, int index, bool create)
 SGPropertyNode *
 SGPropertyNode::getChild (const 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) {
@@ -916,9 +992,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]);
 
@@ -931,7 +1007,7 @@ SGPropertyNode::getChildren (const char * name) const
  * Remove child by position.
  */
 SGPropertyNode_ptr
-SGPropertyNode::removeChild (int pos, bool keep)
+SGPropertyNode::removeChild(int pos)
 {
   SGPropertyNode_ptr node;
   if (pos < 0 || pos >= (int)_children.size())
@@ -941,9 +1017,6 @@ SGPropertyNode::removeChild (int pos, bool keep)
   it += pos;
   node = _children[pos];
   _children.erase(it);
-  if (keep) {
-    _removedChildren.push_back(node);
-  }
 
   node->setAttribute(REMOVED, true);
   node->clearValue();
@@ -956,12 +1029,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;
 }
 
@@ -970,18 +1043,33 @@ 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;
 }
 
+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();
+}
+
 string
 SGPropertyNode::getDisplayName (bool simplify) const
 {
@@ -1562,6 +1650,52 @@ 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 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::_interpolation_mgr = 0;
+
+//------------------------------------------------------------------------------
 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
 {
     if (!getAttribute(READ))
@@ -1741,7 +1875,6 @@ SGPropertyNode::getNode (const char * relative_path, int index) const
   return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Convenience methods using relative paths.
 ////////////////////////////////////////////////////////////////////////
@@ -2047,12 +2180,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)
 {
@@ -2091,14 +2250,13 @@ SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
     _parent->fireChildRemoved(parent, child);
 }
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // 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);
 }