]> git.mxchange.org Git - simgear.git/commitdiff
A sum without diff is like foo without bar
authorTorsten Dreyer <Torsten@t3r.de>
Sat, 20 Nov 2010 10:05:45 +0000 (11:05 +0100)
committerTorsten Dreyer <Torsten@t3r.de>
Sat, 20 Nov 2010 10:05:45 +0000 (11:05 +0100)
Add a <difference> aka <diff> to SGExpression to compute
differences

simgear/structure/SGExpression.cxx
simgear/structure/SGExpression.hxx

index a6776636a0eba906dc6b070ea4251d1c0e9f012a..78d6daf0f7355072900695826dbc8f9fb7d75313 100644 (file)
@@ -293,6 +293,21 @@ SGReadIExpression(SGPropertyNode *inputRoot, const SGPropertyNode *expression)
         }
         return output;
     }
+
+    if (name == "difference" || name == "dif" ) {
+        if (expression->nChildren() < 1) {
+            SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
+            return 0;
+        }
+        SGDifferenceExpression<T>* output = new SGDifferenceExpression<T>;
+        if (!SGReadNaryOperands(output, inputRoot, expression)) {
+            delete output;
+            SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
+            return 0;
+        }
+        return output;
+    }
+
     if (name == "prod" || name == "product") {
         if (expression->nChildren() < 1) {
             SG_LOG(SG_IO, SG_ALERT, "Cannot read \"" << name << "\" expression.");
index 05fc8336f9fd1f7b8c5e6b02b9dbf0c75757d331..444d40cdc9e676940abf4bcd9b8c45a8606283d0 100644 (file)
@@ -794,6 +794,25 @@ public:
   using SGNaryExpression<T>::getOperand;
 };
 
+template<typename T>
+class SGDifferenceExpression : public SGNaryExpression<T> {
+public:
+  SGDifferenceExpression()
+  { }
+  SGDifferenceExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
+    : SGNaryExpression<T>(expr0, expr1)
+  { }
+  virtual void eval(T& value, const simgear::expression::Binding* b) const
+  {
+    value = T(0);
+    unsigned sz = SGNaryExpression<T>::getNumOperands();
+    for (unsigned i = 0; i < sz; ++i)
+      value -= getOperand(i)->getValue(b);
+  }
+  using SGNaryExpression<T>::getValue;
+  using SGNaryExpression<T>::getOperand;
+};
+
 template<typename T>
 class SGProductExpression : public SGNaryExpression<T> {
 public: