X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fstructure%2FSGExpression.hxx;h=2cfa3dc8909d629959816210282535f3be13f37c;hb=b2c3a90adfb6c0285d02355fd8ab061b7f2f1e6c;hp=a6012a0d42f73c3f592880346ac029e3d807efe8;hpb=9ca112c362aaea1e46f91c0aa5fbd6d19c39da1a;p=simgear.git diff --git a/simgear/structure/SGExpression.hxx b/simgear/structure/SGExpression.hxx index a6012a0d..2cfa3dc8 100644 --- a/simgear/structure/SGExpression.hxx +++ b/simgear/structure/SGExpression.hxx @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -136,6 +138,8 @@ public: { return simgear::expression::TypeTraits::typeTag; } + virtual void collectDependentProperties(std::set& props) const + { } }; /// Constant value expression @@ -184,7 +188,9 @@ public: _expression = _expression->simplify(); return SGExpression::simplify(); } - + + virtual void collectDependentProperties(std::set& props) const + { _expression->collectDependentProperties(props); } protected: SGUnaryExpression(SGExpression* expression = 0) { setOperand(expression); } @@ -196,11 +202,11 @@ private: template class SGBinaryExpression : public SGExpression { public: - const SGExpression* getOperand(unsigned i) const + const SGExpression* getOperand(size_t i) const { return _expressions[i]; } - SGExpression* getOperand(unsigned i) + SGExpression* getOperand(size_t i) { return _expressions[i]; } - void setOperand(unsigned i, SGExpression* expression) + void setOperand(size_t i, SGExpression* expression) { if (!expression) expression = new SGConstExpression(T()); @@ -218,6 +224,12 @@ public: return SGExpression::simplify(); } + virtual void collectDependentProperties(std::set& props) const + { + _expressions[0]->collectDependentProperties(props); + _expressions[1]->collectDependentProperties(props); + } + protected: SGBinaryExpression(SGExpression* expr0, SGExpression* expr1) { setOperand(0, expr0); setOperand(1, expr1); } @@ -229,16 +241,16 @@ private: template class SGNaryExpression : public SGExpression { public: - unsigned getNumOperands() const + size_t getNumOperands() const { return _expressions.size(); } - const SGExpression* getOperand(unsigned i) const + const SGExpression* getOperand(size_t i) const { return _expressions[i]; } - SGExpression* getOperand(unsigned i) + SGExpression* getOperand(size_t i) { return _expressions[i]; } - unsigned addOperand(SGExpression* expression) + size_t addOperand(SGExpression* expression) { if (!expression) - return ~unsigned(0); + return ~size_t(0); _expressions.push_back(expression); return _expressions.size() - 1; } @@ -254,18 +266,23 @@ public: virtual bool isConst() const { - for (unsigned i = 0; i < _expressions.size(); ++i) + for (size_t i = 0; i < _expressions.size(); ++i) if (!_expressions[i]->isConst()) return false; return true; } virtual SGExpression* simplify() { - for (unsigned i = 0; i < _expressions.size(); ++i) + for (size_t i = 0; i < _expressions.size(); ++i) _expressions[i] = _expressions[i]->simplify(); return SGExpression::simplify(); } + virtual void collectDependentProperties(std::set& props) const + { + for (size_t i = 0; i < _expressions.size(); ++i) + _expressions[i]->collectDependentProperties(props); + } protected: SGNaryExpression() { } @@ -288,6 +305,9 @@ public: { _prop = prop; } virtual void eval(T& value, const simgear::expression::Binding*) const { doEval(value); } + + virtual void collectDependentProperties(std::set& props) const + { props.insert(_prop.get()); } private: void doEval(float& value) const { if (_prop) value = _prop->getFloatValue(); } @@ -713,6 +733,12 @@ public: return getOperand()->simplify(); return SGUnaryExpression::simplify(); } + + virtual void collectDependentProperties(std::set& props) const + { + SGUnaryExpression::collectDependentProperties(props); + _enable->collectDependentProperties(props); + } using SGUnaryExpression::getOperand; private: @@ -782,8 +808,8 @@ public: 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) + size_t sz = SGNaryExpression::getNumOperands(); + for (size_t i = 0; i < sz; ++i) value += getOperand(i)->getValue(b); } using SGNaryExpression::getValue; @@ -801,8 +827,8 @@ public: virtual void eval(T& value, const simgear::expression::Binding* b) const { value = getOperand(0)->getValue(b); - unsigned sz = SGNaryExpression::getNumOperands(); - for (unsigned i = 1; i < sz; ++i) + size_t sz = SGNaryExpression::getNumOperands(); + for (size_t i = 1; i < sz; ++i) value -= getOperand(i)->getValue(b); } using SGNaryExpression::getValue; @@ -820,8 +846,8 @@ public: virtual void eval(T& value, const simgear::expression::Binding* b) const { value = T(1); - unsigned sz = SGNaryExpression::getNumOperands(); - for (unsigned i = 0; i < sz; ++i) + size_t sz = SGNaryExpression::getNumOperands(); + for (size_t i = 0; i < sz; ++i) value *= getOperand(i)->getValue(b); } using SGNaryExpression::getValue; @@ -838,12 +864,12 @@ public: { } virtual void eval(T& value, const simgear::expression::Binding* b) const { - unsigned sz = SGNaryExpression::getNumOperands(); + size_t sz = SGNaryExpression::getNumOperands(); if (sz < 1) return; value = getOperand(0)->getValue(b); - for (unsigned i = 1; i < sz; ++i) + for (size_t i = 1; i < sz; ++i) value = SGMisc::min(value, getOperand(i)->getValue(b)); } using SGNaryExpression::getOperand; @@ -859,12 +885,12 @@ public: { } virtual void eval(T& value, const simgear::expression::Binding* b) const { - unsigned sz = SGNaryExpression::getNumOperands(); + size_t sz = SGNaryExpression::getNumOperands(); if (sz < 1) return; value = getOperand(0)->getValue(b); - for (unsigned i = 1; i < sz; ++i) + for (size_t i = 1; i < sz; ++i) value = SGMisc::max(value, getOperand(i)->getValue(b)); } using SGNaryExpression::getOperand; @@ -875,6 +901,11 @@ typedef SGExpression SGExpressionf; typedef SGExpression SGExpressiond; typedef SGExpression SGExpressionb; +typedef SGSharedPtr SGExpressioni_ref; +typedef SGSharedPtr SGExpressionf_ref; +typedef SGSharedPtr SGExpressiond_ref; +typedef SGSharedPtr SGExpressionb_ref; + /** * Global function to make an expression out of properties. @@ -919,7 +950,7 @@ namespace simgear { struct ParseError : public sg_exception { - ParseError(const string& message = std::string()) + ParseError(const std::string& message = std::string()) : sg_exception(message) {} }; @@ -983,8 +1014,8 @@ namespace simgear class BindingLayout { public: - int addBinding(const std::string& name, expression::Type type); - bool findBinding(const string& name, VariableBinding& result) const; + size_t addBinding(const std::string& name, expression::Type type); + bool findBinding(const std::string& name, VariableBinding& result) const; std::vector bindings; }; @@ -1001,7 +1032,7 @@ namespace simgear ParserMap& map = getParserMap(); ParserMap::iterator itr = map.find(exp->getName()); if (itr == map.end()) - throw ParseError(string("unknown expression ") + exp->getName()); + throw ParseError(std::string("unknown expression ") + exp->getName()); exp_parser parser = itr->second; return (*parser)(exp, this); } @@ -1076,16 +1107,16 @@ namespace simgear class GeneralNaryExpression : public ::SGExpression { public: typedef OpType operand_type; - unsigned getNumOperands() const + size_t getNumOperands() const { return _expressions.size(); } - const ::SGExpression* getOperand(unsigned i) const + const ::SGExpression* getOperand(size_t i) const { return _expressions[i]; } - ::SGExpression* getOperand(unsigned i) + ::SGExpression* getOperand(size_t i) { return _expressions[i]; } - unsigned addOperand(::SGExpression* expression) + size_t addOperand(::SGExpression* expression) { if (!expression) - return ~unsigned(0); + return ~size_t(0); _expressions.push_back(expression); return _expressions.size() - 1; } @@ -1101,14 +1132,14 @@ namespace simgear virtual bool isConst() const { - for (unsigned i = 0; i < _expressions.size(); ++i) + for (size_t i = 0; i < _expressions.size(); ++i) if (!_expressions[i]->isConst()) return false; return true; } virtual ::SGExpression* simplify() { - for (unsigned i = 0; i < _expressions.size(); ++i) + for (size_t i = 0; i < _expressions.size(); ++i) _expressions[i] = _expressions[i]->simplify(); return SGExpression::simplify(); } @@ -1145,7 +1176,7 @@ namespace simgear } virtual void eval(bool& value, const simgear::expression::Binding* b) const { - unsigned sz = this->getNumOperands(); + size_t sz = this->getNumOperands(); if (sz != 2) return; value = _pred(this->getOperand(0)->getValue(b),