27 #ifndef SCHNEK_EXPRESSION_HPP_ 28 #define SCHNEK_EXPRESSION_HPP_ 30 #include "variables.hpp" 31 #include "../util/logger.hpp" 33 #include <boost/shared_ptr.hpp> 34 #include <boost/lexical_cast.hpp> 36 #include <boost/function.hpp> 38 #include <boost/fusion/include/push_back.hpp> 39 #include <boost/fusion/include/invoke.hpp> 40 #include <boost/fusion/include/cons.hpp> 42 #include <boost/mpl/begin.hpp> 43 #include <boost/mpl/end.hpp> 44 #include <boost/mpl/next.hpp> 45 #include <boost/mpl/deref.hpp> 47 #include <boost/foreach.hpp> 56 namespace fusion = boost::fusion;
57 namespace mpl = boost::mpl;
58 namespace bft = boost::function_types;
80 typedef std::set<long> DependencyList;
90 template<
typename vtype>
97 virtual vtype eval() = 0;
100 virtual bool isConstant() = 0;
105 virtual DependencyList getDependencies() {
return DependencyList(); }
109 typedef vtype ValueType;
112 template<
class ResultVariant>
115 template<
class ExpressionPo
inter>
116 ResultVariant operator()(ExpressionPointer e) {
return e->eval(); }
121 template<
typename vtype>
128 typedef vtype ValueType;
147 template<
typename vtype>
166 SCHNEK_TRACE_LOG(5,
"ReferencedValue<vtype>::eval(" << var->getValue() <<
")")
167 return boost::get<vtype>(var->getValue());
176 dep.insert(var->getId());
183 template<
typename vtype>
209 template<
class oper,
class vtype>
217 UnaryOp(pExpression expr_) : expr(expr_) {}
220 vtype
eval() {
return oper::eval(expr->eval()); }
228 return expr->getDependencies();
232 template<
class vtype>
237 : positive(positive_), expression(expression_) {}
245 template<
class oper,
class vtype>
253 typedef boost::shared_ptr<InvType> pInvType;
254 friend class BinaryOp<typename oper::Inverted, vtype>;
257 std::list<ExpressionInfo<vtype> > expressions;
259 BinaryOp(pExpression expr1_, pExpression expr2_)
261 typedef boost::shared_ptr<BinaryOp<oper, vtype> > pBinaryOp;
263 pBinaryOp binaryExpr1 = boost::dynamic_pointer_cast<SelfType>(expr1_);
264 pInvType invExpr1 = boost::dynamic_pointer_cast<InvType>(expr1_);
266 pBinaryOp binaryExpr2 = boost::dynamic_pointer_cast<SelfType>(expr2_);
267 pInvType invExpr2 = boost::dynamic_pointer_cast<InvType>(expr2_);
271 expressions.insert(expressions.end(), binaryExpr1->expressions.begin(), binaryExpr1->expressions.end());
275 expressions.insert(expressions.end(), invExpr1->expressions.begin(), invExpr1->expressions.end());
284 typename std::list<ExpressionInfo<vtype> >::iterator it = binaryExpr2->expressions.begin();
285 if (!oper::isPositive)
290 expressions.insert(expressions.end(), it, binaryExpr2->expressions.end());
294 typename std::list<ExpressionInfo<vtype> >::iterator it = invExpr2->expressions.begin();
295 if (!oper::isPositive)
300 expressions.insert(expressions.end(), it, invExpr2->expressions.end());
310 typedef typename oper::Positive opPositive;
311 typedef typename oper::Negative opNegative;
312 typename std::list<ExpressionInfo<vtype> >::iterator it = expressions.begin();
314 vtype val = it->expression->eval();
316 while (++it != expressions.end())
319 val = it->positive ? opPositive::eval(val, it->expression->eval()) : opNegative::eval(val, it->expression->eval());
329 if (!exp.expression->isConstant())
return false;
337 DependencyList dependencies;
341 DependencyList dep = exp.expression->getDependencies();
342 dependencies.insert(dep.begin(), dep.end());
348 template<
class vtype>
351 vtype operator()(
int a);
352 vtype operator()(
double a);
353 vtype operator()(std::string a);
359 int operator()(
int a) {
return a; }
360 int operator()(
double a) {
return static_cast<int>(a); }
361 int operator()(std::string a) {
return boost::lexical_cast<
int>(a); }
367 double operator()(
int a) {
return static_cast<double>(a); }
368 double operator()(
double a) {
return a; }
369 double operator()(std::string a) {
return boost::lexical_cast<
double>(a); }
375 std::string operator()(
int a) {
return boost::lexical_cast<std::string>(a); }
376 std::string operator()(
double a) {
return boost::lexical_cast<std::string>(a); }
377 std::string operator()(std::string a) {
return a; }
382 template<
class vtype>
386 template<
class atype>
387 vtype operator()(
const atype& a) {
return static_cast<vtype
>(a); }
390 template<
class vtype>
394 template<
class atype>
395 vtype operator()(
const atype& a) {
return boost::lexical_cast<vtype>(a); }
398 template<
class vtype,
class vtype_orig,
template<
class>
class CastType =
StaticCast>
406 pExpressionOrig expr;
408 TypecastOp(pExpressionOrig expr_) : expr(expr_) {}
413 return CastType<vtype>()(expr->eval());
415 catch(boost::bad_lexical_cast &e)
427 return expr->getDependencies();
433 template<
class ExpressionPo
inter>
434 DependencyList operator()(ExpressionPointer e) {
return e->getDependencies(); }
442 #endif // SCHNEK_EXPRESSION_HPP_ ReferencedValue(const pVariable &var_)
Construct with a value.
Definition: expression.hpp:155
boost::shared_ptr< Expression > pExpression
A pointer to an Expression.
Definition: expression.hpp:108
Expression< vtype_orig >::pExpression pExpressionOrig
A pointer to an original Expression.
Definition: expression.hpp:403
DependencyList getDependencies()
returns the dependencies of the sub expression
Definition: expression.hpp:226
Definition: expression.hpp:349
Definition: expression.hpp:68
Definition: expression.hpp:148
bool isConstant()
The value of the external variable can change.
Definition: expression.hpp:201
Definition: expression.hpp:391
vtype eval()
Return the value.
Definition: expression.hpp:194
bool isConstant()
Constancy depends on the constancy of both expressions.
Definition: expression.hpp:326
DependencyList getDependencies()
returns the dependencies of the sub expression
Definition: expression.hpp:425
vtype eval()
Return the modified value.
Definition: expression.hpp:411
Definition: expression.hpp:74
vtype & getReference()
Return a reference to the value.
Definition: expression.hpp:140
Definition: expression.hpp:399
Definition: expression.hpp:91
Definition: expression.hpp:62
bool isConstant()
Constancy depends on the constancy of the expression.
Definition: expression.hpp:223
vtype eval()
Return the modified value.
Definition: expression.hpp:157
bool isConstant()
Constancy depends on the constancy of the variable.
Definition: expression.hpp:170
vtype eval()
Return the calculated value.
Definition: expression.hpp:309
vtype eval()
Return the stored value.
Definition: expression.hpp:132
Definition: expression.hpp:122
Definition: expression.hpp:246
vtype eval()
Return the modified value.
Definition: expression.hpp:220
Definition: expression.hpp:113
vtype operator()()
The () operator allows expressions to be used as function objects.
Definition: expression.hpp:103
Definition: expression.hpp:233
#define SCHNEK_TRACE_LOG(i, x)
Definition: logger.hpp:54
DependencyList getDependencies()
returns a list with the variable's id
Definition: expression.hpp:173
Value(vtype val_)
Construct with a value.
Definition: expression.hpp:130
Definition: expression.hpp:184
Definition: expression.hpp:210
bool isConstant()
A literal is a constant.
Definition: expression.hpp:138
Definition: expression.hpp:383
bool isConstant()
Constancy depends on the constancy of the expression.
Definition: expression.hpp:422
DependencyList getDependencies()
returns the joint dependencies of both sub expression
Definition: expression.hpp:335
Definition: expression.hpp:431
ExternalValue(vtype *var_)
Construct with a pointer to the value.
Definition: expression.hpp:191