A sum without diff is like foo without bar

Add a <difference> aka <diff> to SGExpression to compute
differences
This commit is contained in:
Torsten Dreyer 2010-11-20 11:05:45 +01:00
parent b4f5eaa541
commit 9d4f0f5824
2 changed files with 34 additions and 0 deletions

View 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.");

View 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: