From 9d4f0f58248dd6107f5aeab8666993c5895cd0db Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Sat, 20 Nov 2010 11:05:45 +0100 Subject: [PATCH] A sum without diff is like foo without bar Add a aka to SGExpression to compute differences --- simgear/structure/SGExpression.cxx | 15 +++++++++++++++ simgear/structure/SGExpression.hxx | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/simgear/structure/SGExpression.cxx b/simgear/structure/SGExpression.cxx index a6776636..78d6daf0 100644 --- a/simgear/structure/SGExpression.cxx +++ b/simgear/structure/SGExpression.cxx @@ -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* output = new SGDifferenceExpression; + 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."); diff --git a/simgear/structure/SGExpression.hxx b/simgear/structure/SGExpression.hxx index 05fc8336..444d40cd 100644 --- a/simgear/structure/SGExpression.hxx +++ b/simgear/structure/SGExpression.hxx @@ -794,6 +794,25 @@ public: using SGNaryExpression::getOperand; }; +template +class SGDifferenceExpression : public SGNaryExpression { +public: + SGDifferenceExpression() + { } + SGDifferenceExpression(SGExpression* expr0, SGExpression* expr1) + : SGNaryExpression(expr0, expr1) + { } + virtual void eval(T& value, const simgear::expression::Binding* b) const + { + value = T(0); + unsigned sz = SGNaryExpression::getNumOperands(); + for (unsigned i = 0; i < sz; ++i) + value -= getOperand(i)->getValue(b); + } + using SGNaryExpression::getValue; + using SGNaryExpression::getOperand; +}; + template class SGProductExpression : public SGNaryExpression { public: -- 2.39.5