00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _SG_EXPRESSION_HXX
00023 #define _SG_EXPRESSION_HXX 1
00024
00025 #include <string>
00026 #include <vector>
00027
00028 #include <simgear/props/condition.hxx>
00029 #include <simgear/props/props.hxx>
00030 #include <simgear/math/interpolater.hxx>
00031 #include <simgear/math/SGMath.hxx>
00032 #include <simgear/scene/model/persparam.hxx>
00033 #include <simgear/structure/exception.hxx>
00034 #include <simgear/structure/Singleton.hxx>
00035
00037
00038 namespace simgear
00039 {
00040 namespace expression
00041 {
00042 enum Type {
00043 BOOL = 0,
00044 INT,
00045 FLOAT,
00046 DOUBLE
00047 };
00048 template<typename T> struct TypeTraits;
00049 template<> struct TypeTraits<bool> {
00050 static const Type typeTag = BOOL;
00051 };
00052 template<> struct TypeTraits<int> {
00053 static const Type typeTag = INT;
00054 };
00055 template<> struct TypeTraits<float> {
00056 static const Type typeTag = FLOAT;
00057 };
00058 template<> struct TypeTraits<double> {
00059 static const Type typeTag = DOUBLE;
00060 };
00061
00062 struct Value
00063 {
00064 Type typeTag;
00065 union {
00066 bool boolVal;
00067 int intVal;
00068 float floatVal;
00069 double doubleVal;
00070 } val;
00071
00072 Value() : typeTag(DOUBLE)
00073 {
00074 val.doubleVal = 0.0;
00075 }
00076
00077 Value(bool val_) : typeTag(BOOL)
00078 {
00079 val.boolVal = val_;
00080 }
00081
00082 Value(int val_) : typeTag(INT)
00083 {
00084 val.intVal = val_;
00085 }
00086
00087 Value(float val_) : typeTag(FLOAT)
00088 {
00089 val.floatVal = val_;
00090 }
00091
00092 Value(double val_) : typeTag(DOUBLE)
00093 {
00094 val.doubleVal = val_;
00095 }
00096
00097 };
00098
00099 class Binding;
00100 }
00101
00102 class Expression : public SGReferenced
00103 {
00104 public:
00105 virtual ~Expression() {}
00106 virtual expression::Type getType() const = 0;
00107 };
00108
00109 const expression::Value eval(const Expression* exp,
00110 const expression::Binding* binding = 0);
00111
00112 }
00113
00114 template<typename T>
00115 class SGExpression : public simgear::Expression {
00116 public:
00117 virtual ~SGExpression() {}
00118 typedef T result_type;
00119 typedef T operand_type;
00120 virtual void eval(T&, const simgear::expression::Binding*) const = 0;
00121
00122 T getValue(const simgear::expression::Binding* binding = 0) const
00123 { T value; eval(value, binding); return value; }
00124
00125 virtual bool isConst() const { return false; }
00126 virtual SGExpression* simplify();
00127 virtual simgear::expression::Type getType() const
00128 {
00129 return simgear::expression::TypeTraits<T>::typeTag;
00130 }
00131 virtual simgear::expression::Type getOperandType() const
00132 {
00133 return simgear::expression::TypeTraits<T>::typeTag;
00134 }
00135 };
00136
00138 template<typename T>
00139 class SGConstExpression : public SGExpression<T> {
00140 public:
00141 SGConstExpression(const T& value = T()) : _value(value)
00142 { }
00143 void setValue(const T& value)
00144 { _value = value; }
00145 const T& getValue(const simgear::expression::Binding* binding = 0) const
00146 { return _value; }
00147 virtual void eval(T& value, const simgear::expression::Binding*) const
00148 { value = _value; }
00149 virtual bool isConst() const { return true; }
00150 private:
00151 T _value;
00152 };
00153
00154 template<typename T>
00155 SGExpression<T>*
00156 SGExpression<T>::simplify()
00157 {
00158 if (isConst())
00159 return new SGConstExpression<T>(getValue());
00160 return this;
00161 }
00162
00163 template<typename T>
00164 class SGUnaryExpression : public SGExpression<T> {
00165 public:
00166 const SGExpression<T>* getOperand() const
00167 { return _expression; }
00168 SGExpression<T>* getOperand()
00169 { return _expression; }
00170 void setOperand(SGExpression<T>* expression)
00171 {
00172 if (!expression)
00173 expression = new SGConstExpression<T>(T());
00174 _expression = expression;
00175 }
00176 virtual bool isConst() const
00177 { return getOperand()->isConst(); }
00178 virtual SGExpression<T>* simplify()
00179 {
00180 _expression = _expression->simplify();
00181 return SGExpression<T>::simplify();
00182 }
00183
00184 protected:
00185 SGUnaryExpression(SGExpression<T>* expression = 0)
00186 { setOperand(expression); }
00187
00188 private:
00189 SGSharedPtr<SGExpression<T> > _expression;
00190 };
00191
00192 template<typename T>
00193 class SGBinaryExpression : public SGExpression<T> {
00194 public:
00195 const SGExpression<T>* getOperand(unsigned i) const
00196 { return _expressions[i]; }
00197 SGExpression<T>* getOperand(unsigned i)
00198 { return _expressions[i]; }
00199 void setOperand(unsigned i, SGExpression<T>* expression)
00200 {
00201 if (!expression)
00202 expression = new SGConstExpression<T>(T());
00203 if (2 <= i)
00204 i = 0;
00205 _expressions[i] = expression;
00206 }
00207
00208 virtual bool isConst() const
00209 { return getOperand(0)->isConst() && getOperand(1)->isConst(); }
00210 virtual SGExpression<T>* simplify()
00211 {
00212 _expressions[0] = _expressions[0]->simplify();
00213 _expressions[1] = _expressions[1]->simplify();
00214 return SGExpression<T>::simplify();
00215 }
00216
00217 protected:
00218 SGBinaryExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00219 { setOperand(0, expr0); setOperand(1, expr1); }
00220
00221 private:
00222 SGSharedPtr<SGExpression<T> > _expressions[2];
00223 };
00224
00225 template<typename T>
00226 class SGNaryExpression : public SGExpression<T> {
00227 public:
00228 unsigned getNumOperands() const
00229 { return _expressions.size(); }
00230 const SGExpression<T>* getOperand(unsigned i) const
00231 { return _expressions[i]; }
00232 SGExpression<T>* getOperand(unsigned i)
00233 { return _expressions[i]; }
00234 unsigned addOperand(SGExpression<T>* expression)
00235 {
00236 if (!expression)
00237 return ~unsigned(0);
00238 _expressions.push_back(expression);
00239 return _expressions.size() - 1;
00240 }
00241
00242 template<typename Iter>
00243 void addOperands(Iter begin, Iter end)
00244 {
00245 for (Iter iter = begin; iter != end; ++iter)
00246 {
00247 addOperand(static_cast< ::SGExpression<T>*>(*iter));
00248 }
00249 }
00250
00251 virtual bool isConst() const
00252 {
00253 for (unsigned i = 0; i < _expressions.size(); ++i)
00254 if (!_expressions[i]->isConst())
00255 return false;
00256 return true;
00257 }
00258 virtual SGExpression<T>* simplify()
00259 {
00260 for (unsigned i = 0; i < _expressions.size(); ++i)
00261 _expressions[i] = _expressions[i]->simplify();
00262 return SGExpression<T>::simplify();
00263 }
00264
00265 protected:
00266 SGNaryExpression()
00267 { }
00268 SGNaryExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00269 { addOperand(expr0); addOperand(expr1); }
00270
00271 private:
00272 std::vector<SGSharedPtr<SGExpression<T> > > _expressions;
00273 };
00274
00275
00276
00277
00278 template<typename T>
00279 class SGPropertyExpression : public SGExpression<T> {
00280 public:
00281 SGPropertyExpression(const SGPropertyNode* prop) : _prop(prop)
00282 { }
00283 void setPropertyNode(const SGPropertyNode* prop)
00284 { _prop = prop; }
00285 virtual void eval(T& value, const simgear::expression::Binding*) const
00286 { doEval(value); }
00287 private:
00288 void doEval(float& value) const
00289 { if (_prop) value = _prop->getFloatValue(); }
00290 void doEval(double& value) const
00291 { if (_prop) value = _prop->getDoubleValue(); }
00292 void doEval(int& value) const
00293 { if (_prop) value = _prop->getIntValue(); }
00294 void doEval(long& value) const
00295 { if (_prop) value = _prop->getLongValue(); }
00296 void doEval(bool& value) const
00297 { if (_prop) value = _prop->getBoolValue(); }
00298 SGSharedPtr<const SGPropertyNode> _prop;
00299 };
00300
00301 template<typename T>
00302 class SGAbsExpression : public SGUnaryExpression<T> {
00303 public:
00304 SGAbsExpression(SGExpression<T>* expr = 0)
00305 : SGUnaryExpression<T>(expr)
00306 { }
00307
00308 virtual void eval(T& value, const simgear::expression::Binding* b) const
00309 { value = getOperand()->getValue(b); if (value <= 0) value = -value; }
00310
00311 using SGUnaryExpression<T>::getOperand;
00312 };
00313
00314 template<typename T>
00315 class SGACosExpression : public SGUnaryExpression<T> {
00316 public:
00317 SGACosExpression(SGExpression<T>* expr = 0)
00318 : SGUnaryExpression<T>(expr)
00319 { }
00320
00321 virtual void eval(T& value, const simgear::expression::Binding* b) const
00322 { value = acos(SGMisc<T>::clip(getOperand()->getValue(b), -1, 1)); }
00323
00324 using SGUnaryExpression<T>::getOperand;
00325 };
00326
00327 template<typename T>
00328 class SGASinExpression : public SGUnaryExpression<T> {
00329 public:
00330 SGASinExpression(SGExpression<T>* expr = 0)
00331 : SGUnaryExpression<T>(expr)
00332 { }
00333
00334 virtual void eval(T& value, const simgear::expression::Binding* b) const
00335 { value = asin(SGMisc<T>::clip(getOperand()->getValue(b), -1, 1)); }
00336
00337 using SGUnaryExpression<T>::getOperand;
00338 };
00339
00340 template<typename T>
00341 class SGATanExpression : public SGUnaryExpression<T> {
00342 public:
00343 SGATanExpression(SGExpression<T>* expr = 0)
00344 : SGUnaryExpression<T>(expr)
00345 { }
00346
00347 virtual void eval(T& value, const simgear::expression::Binding* b) const
00348 { value = atan(getOperand()->getValue(b)); }
00349
00350 using SGUnaryExpression<T>::getOperand;
00351 };
00352
00353 template<typename T>
00354 class SGCeilExpression : public SGUnaryExpression<T> {
00355 public:
00356 SGCeilExpression(SGExpression<T>* expr = 0)
00357 : SGUnaryExpression<T>(expr)
00358 { }
00359
00360 virtual void eval(T& value, const simgear::expression::Binding* b) const
00361 { value = ceil(getOperand()->getValue(b)); }
00362
00363 using SGUnaryExpression<T>::getOperand;
00364 };
00365
00366 template<typename T>
00367 class SGCosExpression : public SGUnaryExpression<T> {
00368 public:
00369 SGCosExpression(SGExpression<T>* expr = 0)
00370 : SGUnaryExpression<T>(expr)
00371 { }
00372
00373 virtual void eval(T& value, const simgear::expression::Binding* b) const
00374 { value = cos(getOperand()->getValue(b)); }
00375
00376 using SGUnaryExpression<T>::getOperand;
00377 };
00378
00379 template<typename T>
00380 class SGCoshExpression : public SGUnaryExpression<T> {
00381 public:
00382 SGCoshExpression(SGExpression<T>* expr = 0)
00383 : SGUnaryExpression<T>(expr)
00384 { }
00385
00386 virtual void eval(T& value, const simgear::expression::Binding* b) const
00387 { value = cosh(getOperand()->getValue(b)); }
00388
00389 using SGUnaryExpression<T>::getOperand;
00390 };
00391
00392 template<typename T>
00393 class SGExpExpression : public SGUnaryExpression<T> {
00394 public:
00395 SGExpExpression(SGExpression<T>* expr = 0)
00396 : SGUnaryExpression<T>(expr)
00397 { }
00398
00399 virtual void eval(T& value, const simgear::expression::Binding* b) const
00400 { value = exp(getOperand()->getValue(b)); }
00401
00402 using SGUnaryExpression<T>::getOperand;
00403 };
00404
00405 template<typename T>
00406 class SGFloorExpression : public SGUnaryExpression<T> {
00407 public:
00408 SGFloorExpression(SGExpression<T>* expr = 0)
00409 : SGUnaryExpression<T>(expr)
00410 { }
00411
00412 virtual void eval(T& value, const simgear::expression::Binding* b) const
00413 { value = floor(getOperand()->getValue(b)); }
00414
00415 using SGUnaryExpression<T>::getOperand;
00416 };
00417
00418 template<typename T>
00419 class SGLogExpression : public SGUnaryExpression<T> {
00420 public:
00421 SGLogExpression(SGExpression<T>* expr = 0)
00422 : SGUnaryExpression<T>(expr)
00423 { }
00424
00425 virtual void eval(T& value, const simgear::expression::Binding* b) const
00426 { value = log(getOperand()->getValue(b)); }
00427
00428 using SGUnaryExpression<T>::getOperand;
00429 };
00430
00431 template<typename T>
00432 class SGLog10Expression : public SGUnaryExpression<T> {
00433 public:
00434 SGLog10Expression(SGExpression<T>* expr = 0)
00435 : SGUnaryExpression<T>(expr)
00436 { }
00437
00438 virtual void eval(T& value, const simgear::expression::Binding* b) const
00439 { value = log10(getOperand()->getValue(b)); }
00440
00441 using SGUnaryExpression<T>::getOperand;
00442 };
00443
00444 template<typename T>
00445 class SGSinExpression : public SGUnaryExpression<T> {
00446 public:
00447 SGSinExpression(SGExpression<T>* expr = 0)
00448 : SGUnaryExpression<T>(expr)
00449 { }
00450
00451 virtual void eval(T& value, const simgear::expression::Binding* b) const
00452 { value = sin(getOperand()->getValue(b)); }
00453
00454 using SGUnaryExpression<T>::getOperand;
00455 };
00456
00457 template<typename T>
00458 class SGSinhExpression : public SGUnaryExpression<T> {
00459 public:
00460 SGSinhExpression(SGExpression<T>* expr = 0)
00461 : SGUnaryExpression<T>(expr)
00462 { }
00463
00464 virtual void eval(T& value, const simgear::expression::Binding* b) const
00465 { value = sinh(getOperand()->getValue(b)); }
00466
00467 using SGUnaryExpression<T>::getOperand;
00468 };
00469
00470 template<typename T>
00471 class SGSqrExpression : public SGUnaryExpression<T> {
00472 public:
00473 SGSqrExpression(SGExpression<T>* expr = 0)
00474 : SGUnaryExpression<T>(expr)
00475 { }
00476
00477 virtual void eval(T& value, const simgear::expression::Binding* b) const
00478 { value = getOperand()->getValue(b); value = value*value; }
00479
00480 using SGUnaryExpression<T>::getOperand;
00481 };
00482
00483 template<typename T>
00484 class SGSqrtExpression : public SGUnaryExpression<T> {
00485 public:
00486 SGSqrtExpression(SGExpression<T>* expr = 0)
00487 : SGUnaryExpression<T>(expr)
00488 { }
00489
00490 virtual void eval(T& value, const simgear::expression::Binding* b) const
00491 { value = sqrt(getOperand()->getValue(b)); }
00492
00493 using SGUnaryExpression<T>::getOperand;
00494 };
00495
00496 template<typename T>
00497 class SGTanExpression : public SGUnaryExpression<T> {
00498 public:
00499 SGTanExpression(SGExpression<T>* expr = 0)
00500 : SGUnaryExpression<T>(expr)
00501 { }
00502
00503 virtual void eval(T& value, const simgear::expression::Binding* b) const
00504 { value = tan(getOperand()->getValue(b)); }
00505
00506 using SGUnaryExpression<T>::getOperand;
00507 };
00508
00509 template<typename T>
00510 class SGTanhExpression : public SGUnaryExpression<T> {
00511 public:
00512 SGTanhExpression(SGExpression<T>* expr = 0)
00513 : SGUnaryExpression<T>(expr)
00514 { }
00515
00516 virtual void eval(T& value, const simgear::expression::Binding* b) const
00517 { value = tanh(getOperand()->getValue(b)); }
00518
00519 using SGUnaryExpression<T>::getOperand;
00520 };
00521
00522 template<typename T>
00523 class SGScaleExpression : public SGUnaryExpression<T> {
00524 public:
00525 SGScaleExpression(SGExpression<T>* expr = 0, const T& scale = T(1))
00526 : SGUnaryExpression<T>(expr), _scale(scale)
00527 { }
00528 void setScale(const T& scale)
00529 { _scale = scale; }
00530 const T& getScale() const
00531 { return _scale; }
00532
00533 virtual void eval(T& value, const simgear::expression::Binding* b) const
00534 { value = _scale * getOperand()->getValue(b); }
00535
00536 virtual SGExpression<T>* simplify()
00537 {
00538 if (_scale == 1)
00539 return getOperand()->simplify();
00540 return SGUnaryExpression<T>::simplify();
00541 }
00542
00543 using SGUnaryExpression<T>::getOperand;
00544 private:
00545 T _scale;
00546 };
00547
00548 template<typename T>
00549 class SGBiasExpression : public SGUnaryExpression<T> {
00550 public:
00551 SGBiasExpression(SGExpression<T>* expr = 0, const T& bias = T(0))
00552 : SGUnaryExpression<T>(expr), _bias(bias)
00553 { }
00554
00555 void setBias(const T& bias)
00556 { _bias = bias; }
00557 const T& getBias() const
00558 { return _bias; }
00559
00560 virtual void eval(T& value, const simgear::expression::Binding* b) const
00561 { value = _bias + getOperand()->getValue(b); }
00562
00563 virtual SGExpression<T>* simplify()
00564 {
00565 if (_bias == 0)
00566 return getOperand()->simplify();
00567 return SGUnaryExpression<T>::simplify();
00568 }
00569
00570 using SGUnaryExpression<T>::getOperand;
00571 private:
00572 T _bias;
00573 };
00574
00575 template<typename T>
00576 class SGInterpTableExpression : public SGUnaryExpression<T> {
00577 public:
00578 SGInterpTableExpression(SGExpression<T>* expr,
00579 const SGInterpTable* interpTable) :
00580 SGUnaryExpression<T>(expr),
00581 _interpTable(interpTable)
00582 { }
00583
00584 virtual void eval(T& value, const simgear::expression::Binding* b) const
00585 {
00586 if (_interpTable)
00587 value = _interpTable->interpolate(getOperand()->getValue(b));
00588 }
00589
00590 using SGUnaryExpression<T>::getOperand;
00591 private:
00592 SGSharedPtr<SGInterpTable const> _interpTable;
00593 };
00594
00595 template<typename T>
00596 class SGClipExpression : public SGUnaryExpression<T> {
00597 public:
00598 SGClipExpression(SGExpression<T>* expr)
00599 : SGUnaryExpression<T>(expr),
00600 _clipMin(SGMisc<T>::min(-SGLimits<T>::max(), SGLimits<T>::min())),
00601 _clipMax(SGLimits<T>::max())
00602 { }
00603 SGClipExpression(SGExpression<T>* expr,
00604 const T& clipMin, const T& clipMax)
00605 : SGUnaryExpression<T>(expr),
00606 _clipMin(clipMin),
00607 _clipMax(clipMax)
00608 { }
00609
00610 void setClipMin(const T& clipMin)
00611 { _clipMin = clipMin; }
00612 const T& getClipMin() const
00613 { return _clipMin; }
00614
00615 void setClipMax(const T& clipMax)
00616 { _clipMax = clipMax; }
00617 const T& getClipMax() const
00618 { return _clipMax; }
00619
00620 virtual void eval(T& value, const simgear::expression::Binding* b) const
00621 {
00622 value = SGMisc<T>::clip(getOperand()->getValue(b), _clipMin, _clipMax);
00623 }
00624
00625 virtual SGExpression<T>* simplify()
00626 {
00627 if (_clipMin <= SGMisc<T>::min(-SGLimits<T>::max(), SGLimits<T>::min()) &&
00628 _clipMax >= SGLimits<T>::max())
00629 return getOperand()->simplify();
00630 return SGUnaryExpression<T>::simplify();
00631 }
00632
00633 using SGUnaryExpression<T>::getOperand;
00634 private:
00635 T _clipMin;
00636 T _clipMax;
00637 };
00638
00639 template<typename T>
00640 class SGStepExpression : public SGUnaryExpression<T> {
00641 public:
00642 SGStepExpression(SGExpression<T>* expr = 0,
00643 const T& step = T(1), const T& scroll = T(0))
00644 : SGUnaryExpression<T>(expr), _step(step), _scroll(scroll)
00645 { }
00646
00647 void setStep(const T& step)
00648 { _step = step; }
00649 const T& getStep() const
00650 { return _step; }
00651
00652 void setScroll(const T& scroll)
00653 { _scroll = scroll; }
00654 const T& getScroll() const
00655 { return _scroll; }
00656
00657 virtual void eval(T& value, const simgear::expression::Binding* b) const
00658 { value = apply_mods(getOperand()->getValue(b)); }
00659
00660 using SGUnaryExpression<T>::getOperand;
00661
00662 private:
00663 T apply_mods(T property) const
00664 {
00665 T modprop;
00666 if (_step > 0) {
00667 T scrollval = 0;
00668 if(_scroll > 0) {
00669
00670 T remainder = _step - fmod(fabs(property), _step);
00671 if (remainder < _scroll) {
00672 scrollval = (_scroll - remainder) / _scroll * _step;
00673 }
00674 }
00675
00676 if(property > 0)
00677 modprop = ((floor(property/_step) * _step) + scrollval);
00678 else
00679 modprop = ((ceil(property/_step) * _step) + scrollval);
00680 } else {
00681 modprop = property;
00682 }
00683 return modprop;
00684 }
00685
00686 T _step;
00687 T _scroll;
00688 };
00689
00690 template<typename T>
00691 class SGEnableExpression : public SGUnaryExpression<T> {
00692 public:
00693 SGEnableExpression(SGExpression<T>* expr = 0,
00694 SGCondition* enable = 0,
00695 const T& disabledValue = T(0))
00696 : SGUnaryExpression<T>(expr),
00697 _enable(enable),
00698 _disabledValue(disabledValue)
00699 { }
00700
00701 const T& getDisabledValue() const
00702 { return _disabledValue; }
00703 void setDisabledValue(const T& disabledValue)
00704 { _disabledValue = disabledValue; }
00705
00706 virtual void eval(T& value, const simgear::expression::Binding* b) const
00707 {
00708 if (_enable->test())
00709 value = getOperand()->getValue(b);
00710 else
00711 value = _disabledValue;
00712 }
00713
00714 virtual SGExpression<T>* simplify()
00715 {
00716 if (!_enable)
00717 return getOperand()->simplify();
00718 return SGUnaryExpression<T>::simplify();
00719 }
00720
00721 using SGUnaryExpression<T>::getOperand;
00722 private:
00723 SGSharedPtr<SGCondition> _enable;
00724 T _disabledValue;
00725 };
00726
00727 template<typename T>
00728 class SGAtan2Expression : public SGBinaryExpression<T> {
00729 public:
00730 SGAtan2Expression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00731 : SGBinaryExpression<T>(expr0, expr1)
00732 { }
00733 virtual void eval(T& value, const simgear::expression::Binding* b) const
00734 { value = atan2(getOperand(0)->getValue(b), getOperand(1)->getValue(b)); }
00735 using SGBinaryExpression<T>::getOperand;
00736 };
00737
00738 template<typename T>
00739 class SGDivExpression : public SGBinaryExpression<T> {
00740 public:
00741 SGDivExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00742 : SGBinaryExpression<T>(expr0, expr1)
00743 { }
00744 virtual void eval(T& value, const simgear::expression::Binding* b) const
00745 { value = getOperand(0)->getValue(b) / getOperand(1)->getValue(b); }
00746 using SGBinaryExpression<T>::getOperand;
00747 };
00748
00749 template<typename T>
00750 class SGModExpression : public SGBinaryExpression<T> {
00751 public:
00752 SGModExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00753 : SGBinaryExpression<T>(expr0, expr1)
00754 { }
00755 virtual void eval(T& value, const simgear::expression::Binding* b) const
00756 { value = mod(getOperand(0)->getValue(b), getOperand(1)->getValue(b)); }
00757 using SGBinaryExpression<T>::getOperand;
00758 private:
00759 int mod(const int& v0, const int& v1) const
00760 { return v0 % v1; }
00761 float mod(const float& v0, const float& v1) const
00762 { return fmod(v0, v1); }
00763 double mod(const double& v0, const double& v1) const
00764 { return fmod(v0, v1); }
00765 };
00766
00767 template<typename T>
00768 class SGPowExpression : public SGBinaryExpression<T> {
00769 public:
00770 SGPowExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00771 : SGBinaryExpression<T>(expr0, expr1)
00772 { }
00773 virtual void eval(T& value, const simgear::expression::Binding* b) const
00774 { value = pow(getOperand(0)->getValue(b), getOperand(1)->getValue(b)); }
00775 using SGBinaryExpression<T>::getOperand;
00776 };
00777
00778 template<typename T>
00779 class SGSumExpression : public SGNaryExpression<T> {
00780 public:
00781 SGSumExpression()
00782 { }
00783 SGSumExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00784 : SGNaryExpression<T>(expr0, expr1)
00785 { }
00786 virtual void eval(T& value, const simgear::expression::Binding* b) const
00787 {
00788 value = T(0);
00789 unsigned sz = SGNaryExpression<T>::getNumOperands();
00790 for (unsigned i = 0; i < sz; ++i)
00791 value += getOperand(i)->getValue(b);
00792 }
00793 using SGNaryExpression<T>::getValue;
00794 using SGNaryExpression<T>::getOperand;
00795 };
00796
00797 template<typename T>
00798 class SGProductExpression : public SGNaryExpression<T> {
00799 public:
00800 SGProductExpression()
00801 { }
00802 SGProductExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00803 : SGNaryExpression<T>(expr0, expr1)
00804 { }
00805 virtual void eval(T& value, const simgear::expression::Binding* b) const
00806 {
00807 value = T(1);
00808 unsigned sz = SGNaryExpression<T>::getNumOperands();
00809 for (unsigned i = 0; i < sz; ++i)
00810 value *= getOperand(i)->getValue(b);
00811 }
00812 using SGNaryExpression<T>::getValue;
00813 using SGNaryExpression<T>::getOperand;
00814 };
00815
00816 template<typename T>
00817 class SGMinExpression : public SGNaryExpression<T> {
00818 public:
00819 SGMinExpression()
00820 { }
00821 SGMinExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00822 : SGNaryExpression<T>(expr0, expr1)
00823 { }
00824 virtual void eval(T& value, const simgear::expression::Binding* b) const
00825 {
00826 unsigned sz = SGNaryExpression<T>::getNumOperands();
00827 if (sz < 1)
00828 return;
00829
00830 value = getOperand(0)->getValue(b);
00831 for (unsigned i = 1; i < sz; ++i)
00832 value = SGMisc<T>::min(value, getOperand(i)->getValue(b));
00833 }
00834 using SGNaryExpression<T>::getOperand;
00835 };
00836
00837 template<typename T>
00838 class SGMaxExpression : public SGNaryExpression<T> {
00839 public:
00840 SGMaxExpression()
00841 { }
00842 SGMaxExpression(SGExpression<T>* expr0, SGExpression<T>* expr1)
00843 : SGNaryExpression<T>(expr0, expr1)
00844 { }
00845 virtual void eval(T& value, const simgear::expression::Binding* b) const
00846 {
00847 unsigned sz = SGNaryExpression<T>::getNumOperands();
00848 if (sz < 1)
00849 return;
00850
00851 value = getOperand(0)->getValue(b);
00852 for (unsigned i = 1; i < sz; ++i)
00853 value = SGMisc<T>::max(value, getOperand(i)->getValue(b));
00854 }
00855 using SGNaryExpression<T>::getOperand;
00856 };
00857
00858 typedef SGExpression<int> SGExpressioni;
00859 typedef SGExpression<float> SGExpressionf;
00860 typedef SGExpression<double> SGExpressiond;
00861 typedef SGExpression<bool> SGExpressionb;
00862
00885 SGExpression<int>*
00886 SGReadIntExpression(SGPropertyNode *inputRoot,
00887 const SGPropertyNode *configNode);
00888
00889 SGExpression<float>*
00890 SGReadFloatExpression(SGPropertyNode *inputRoot,
00891 const SGPropertyNode *configNode);
00892
00893 SGExpression<double>*
00894 SGReadDoubleExpression(SGPropertyNode *inputRoot,
00895 const SGPropertyNode *configNode);
00896
00897 SGExpression<bool>*
00898 SGReadBoolExpression(SGPropertyNode *inputRoot,
00899 const SGPropertyNode *configNode);
00900
00901 namespace simgear
00902 {
00903 namespace expression
00904 {
00905 struct ParseError : public sg_exception
00906 {
00907 ParseError(const string& message = std::string())
00908 : sg_exception(message) {}
00909 };
00910
00911
00912 class Binding
00913 {
00914 public:
00915 virtual ~Binding() {}
00916 const virtual Value* getBindings() const = 0;
00917 virtual Value* getBindings() = 0;
00918 };
00919
00920 class VariableLengthBinding : public Binding
00921 {
00922 public:
00923 const Value* getBindings() const
00924 {
00925 if (_bindings.empty())
00926 return 0;
00927 else
00928 return &_bindings[0];
00929 }
00930 Value* getBindings()
00931 {
00932 if (_bindings.empty())
00933 return 0;
00934 else
00935 return &_bindings[0];
00936 }
00937 std::vector<Value> _bindings;
00938 };
00939
00940 template<int Size> class FixedLengthBinding : public Binding
00941 {
00942 public:
00943 Value* getBindings()
00944 {
00945 return &_bindings[0];
00946 }
00947 const Value* getBindings() const
00948 {
00949 return &_bindings[0];
00950 }
00951 Value _bindings[Size];
00952 };
00953
00954 struct VariableBinding
00955 {
00956 VariableBinding() : type(expression::DOUBLE), location(-1) {}
00957
00958 VariableBinding(const std::string& name_, expression::Type type_,
00959 int location_)
00960 : name(name_), type(type_), location(location_)
00961 {
00962 }
00963 std::string name;
00964 expression::Type type;
00965 int location;
00966 };
00967
00968 class BindingLayout
00969 {
00970 public:
00971 int addBinding(const std::string& name, expression::Type type);
00972 bool findBinding(const string& name, VariableBinding& result) const;
00973 std::vector<VariableBinding> bindings;
00974 };
00975
00976 class Parser {
00977 public:
00978 typedef Expression* (*exp_parser)(const SGPropertyNode* exp,
00979 Parser* parser);
00980 void addParser(const std::string& name, exp_parser parser)
00981 {
00982 getParserMap().insert(std::make_pair(name, parser));
00983 }
00984 Expression* read(const SGPropertyNode* exp)
00985 {
00986 ParserMap& map = getParserMap();
00987 ParserMap::iterator itr = map.find(exp->getName());
00988 if (itr == map.end())
00989 throw ParseError(string("unknown expression ") + exp->getName());
00990 exp_parser parser = itr->second;
00991 return (*parser)(exp, this);
00992 }
00993
00994 bool readChildren(const SGPropertyNode* exp,
00995 std::vector<Expression*>& result);
00999 typedef std::map<const std::string, exp_parser> ParserMap;
01000 virtual ParserMap& getParserMap() = 0;
01005 BindingLayout& getBindingLayout() { return _bindingLayout; }
01006 protected:
01007 BindingLayout _bindingLayout;
01008 };
01009
01010 class ExpressionParser : public Parser
01011 {
01012 public:
01013 ParserMap& getParserMap()
01014 {
01015 return ParserMapSingleton::instance()->_parserTable;
01016 }
01017 static void addExpParser(const std::string&, exp_parser);
01018 protected:
01019 struct ParserMapSingleton : public simgear::Singleton<ParserMapSingleton>
01020 {
01021 ParserMap _parserTable;
01022 };
01023 };
01024
01028 struct ExpParserRegistrar
01029 {
01030 ExpParserRegistrar(const std::string& token, Parser::exp_parser parser)
01031 {
01032 ExpressionParser::addExpParser(token, parser);
01033 }
01034 };
01035
01036 }
01037
01041 template<typename T>
01042 class VariableExpression : public ::SGExpression<T> {
01043 public:
01044 VariableExpression(int location) : _location(location) {}
01045 virtual ~VariableExpression() {}
01046 virtual void eval(T& value, const simgear::expression::Binding* b) const
01047 {
01048 const expression::Value* values = b->getBindings();
01049 value = *reinterpret_cast<const T *>(&values[_location].val);
01050 }
01051 protected:
01052 int _location;
01053
01054 };
01055
01060 template<typename T, typename OpType>
01061 class GeneralNaryExpression : public ::SGExpression<T> {
01062 public:
01063 typedef OpType operand_type;
01064 unsigned getNumOperands() const
01065 { return _expressions.size(); }
01066 const ::SGExpression<OpType>* getOperand(unsigned i) const
01067 { return _expressions[i]; }
01068 ::SGExpression<OpType>* getOperand(unsigned i)
01069 { return _expressions[i]; }
01070 unsigned addOperand(::SGExpression<OpType>* expression)
01071 {
01072 if (!expression)
01073 return ~unsigned(0);
01074 _expressions.push_back(expression);
01075 return _expressions.size() - 1;
01076 }
01077
01078 template<typename Iter>
01079 void addOperands(Iter begin, Iter end)
01080 {
01081 for (Iter iter = begin; iter != end; ++iter)
01082 {
01083 addOperand(static_cast< ::SGExpression<OpType>*>(*iter));
01084 }
01085 }
01086
01087 virtual bool isConst() const
01088 {
01089 for (unsigned i = 0; i < _expressions.size(); ++i)
01090 if (!_expressions[i]->isConst())
01091 return false;
01092 return true;
01093 }
01094 virtual ::SGExpression<T>* simplify()
01095 {
01096 for (unsigned i = 0; i < _expressions.size(); ++i)
01097 _expressions[i] = _expressions[i]->simplify();
01098 return SGExpression<T>::simplify();
01099 }
01100
01101 simgear::expression::Type getOperandType() const
01102 {
01103 return simgear::expression::TypeTraits<OpType>::typeTag;
01104 }
01105
01106 protected:
01107 GeneralNaryExpression()
01108 { }
01109 GeneralNaryExpression(::SGExpression<OpType>* expr0,
01110 ::SGExpression<OpType>* expr1)
01111 { addOperand(expr0); addOperand(expr1); }
01112
01113 std::vector<SGSharedPtr<SGExpression<OpType> > > _expressions;
01114 };
01115
01120 template<typename OpType, template<typename PredOp> class Pred>
01121 class PredicateExpression : public GeneralNaryExpression<bool, OpType> {
01122 public:
01123 PredicateExpression()
01124 {
01125 }
01126 PredicateExpression(::SGExpression<OpType>* expr0,
01127 ::SGExpression<OpType>* expr1)
01128 : GeneralNaryExpression<bool, OpType>(expr0, expr1)
01129 {
01130 }
01131 virtual void eval(bool& value, const simgear::expression::Binding* b) const
01132 {
01133 unsigned sz = this->getNumOperands();
01134 if (sz != 2)
01135 return;
01136 value = _pred(this->getOperand(0)->getValue(b),
01137 this->getOperand(1)->getValue(b));
01138 }
01139 protected:
01140 Pred<OpType> _pred;
01141 };
01142
01143 template<template<typename OT> class Pred, typename OpType>
01144 PredicateExpression<OpType, Pred>*
01145 makePredicate(SGExpression<OpType>* op1, SGExpression<OpType>* op2)
01146 {
01147 return new PredicateExpression<OpType, Pred>(op1, op2);
01148 }
01149
01150 template<typename OpType>
01151 class EqualToExpression : public PredicateExpression<OpType, std::equal_to>
01152 {
01153 public:
01154 EqualToExpression() {}
01155 EqualToExpression(::SGExpression<OpType>* expr0,
01156 ::SGExpression<OpType>* expr1)
01157 : PredicateExpression<OpType, std::equal_to>(expr0, expr1)
01158 {
01159 }
01160 };
01161
01162 template<typename OpType>
01163 class LessExpression : public PredicateExpression<OpType, std::less>
01164 {
01165 public:
01166 LessExpression() {}
01167 LessExpression(::SGExpression<OpType>* expr0, ::SGExpression<OpType>* expr1)
01168 : PredicateExpression<OpType, std::less>(expr0, expr1)
01169 {
01170 }
01171 };
01172
01173 template<typename OpType>
01174 class LessEqualExpression
01175 : public PredicateExpression<OpType, std::less_equal>
01176 {
01177 public:
01178 LessEqualExpression() {}
01179 LessEqualExpression(::SGExpression<OpType>* expr0,
01180 ::SGExpression<OpType>* expr1)
01181 : PredicateExpression<OpType, std::less_equal>(expr0, expr1)
01182 {
01183 }
01184 };
01185
01186 class NotExpression : public ::SGUnaryExpression<bool>
01187 {
01188 public:
01189 NotExpression(::SGExpression<bool>* expr = 0)
01190 : ::SGUnaryExpression<bool>(expr)
01191 {
01192 }
01193 void eval(bool& value, const expression::Binding* b) const
01194 {
01195 value = !getOperand()->getValue(b);
01196 }
01197 };
01198
01199 class OrExpression : public ::SGNaryExpression<bool>
01200 {
01201 public:
01202 void eval(bool& value, const expression::Binding* b) const
01203 {
01204 value = false;
01205 for (int i = 0; i < (int)getNumOperands(); ++i) {
01206 value = value || getOperand(i)->getValue(b);
01207 if (value)
01208 return;
01209 }
01210 }
01211 };
01212
01213 class AndExpression : public ::SGNaryExpression<bool>
01214 {
01215 public:
01216 void eval(bool& value, const expression::Binding* b) const
01217 {
01218 value = true;
01219 for (int i = 0; i < (int)getNumOperands(); ++i) {
01220 value = value && getOperand(i)->getValue(b);
01221 if (!value)
01222 return;
01223 }
01224 }
01225 };
01226
01230 template<typename T, typename OpType>
01231 class ConvertExpression : public GeneralNaryExpression<T, OpType>
01232 {
01233 public:
01234 ConvertExpression() {}
01235 ConvertExpression(::SGExpression<OpType>* expr0)
01236 {
01237 addOperand(expr0);
01238 }
01239 virtual void eval(T& value, const simgear::expression::Binding* b) const
01240 {
01241 typename ConvertExpression::operand_type result;
01242 this->_expressions.at(0)->eval(result, b);
01243 value = result;
01244 }
01245 };
01246 }
01247 #endif // _SG_EXPRESSION_HXX