27 #ifndef SCHNEK_EXPRESSION_HPP_ 28 #define SCHNEK_EXPRESSION_HPP_ 30 #include "variables.hpp" 31 #include "../util/logger.hpp" 33 #pragma GCC diagnostic push 34 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 35 #pragma GCC diagnostic push 36 #pragma GCC diagnostic ignored "-Wdeprecated-copy" 38 #include <boost/lexical_cast.hpp> 39 #include <boost/function.hpp> 41 #include <boost/fusion/include/push_back.hpp> 42 #include <boost/fusion/include/invoke.hpp> 43 #include <boost/fusion/include/cons.hpp> 45 #include <boost/mpl/begin.hpp> 46 #include <boost/mpl/end.hpp> 47 #include <boost/mpl/next.hpp> 48 #include <boost/mpl/deref.hpp> 50 #pragma GCC diagnostic pop 51 #pragma GCC diagnostic pop 61 namespace fusion = boost::fusion;
62 namespace mpl = boost::mpl;
63 namespace bft = boost::function_types;
85 typedef std::set<long> DependencyList;
95 template<
typename vtype>
102 virtual vtype eval() = 0;
105 virtual bool isConstant() = 0;
110 virtual DependencyList getDependencies() {
return DependencyList(); }
114 typedef vtype ValueType;
117 template<
class ResultVariant>
120 template<
class ExpressionPo
inter>
121 ResultVariant operator()(ExpressionPointer e) {
return e->eval(); }
126 template<
typename vtype>
133 typedef vtype ValueType;
152 template<
typename vtype>
171 SCHNEK_TRACE_LOG(5,
"ReferencedValue<vtype>::eval(" << var->getValue() <<
")")
172 return boost::get<vtype>(var->getValue());
181 dep.insert(var->getId());
188 template<
typename vtype>
214 template<
class oper,
class vtype>
222 UnaryOp(pExpression expr_) : expr(expr_) {}
225 vtype
eval() {
return oper::eval(expr->eval()); }
233 return expr->getDependencies();
237 template<
class vtype>
242 : positive(positive_), expression(expression_) {}
250 template<
class oper,
class vtype>
258 typedef std::shared_ptr<InvType> pInvType;
259 friend class BinaryOp<typename oper::Inverted, vtype>;
262 std::list<ExpressionInfo<vtype> > expressions;
264 BinaryOp(pExpression expr1_, pExpression expr2_)
266 typedef std::shared_ptr<BinaryOp<oper, vtype> > pBinaryOp;
268 pBinaryOp binaryExpr1 = std::dynamic_pointer_cast<SelfType>(expr1_);
269 pInvType invExpr1 = std::dynamic_pointer_cast<InvType>(expr1_);
271 pBinaryOp binaryExpr2 = std::dynamic_pointer_cast<SelfType>(expr2_);
272 pInvType invExpr2 = std::dynamic_pointer_cast<InvType>(expr2_);
276 expressions.insert(expressions.end(), binaryExpr1->expressions.begin(), binaryExpr1->expressions.end());
280 expressions.insert(expressions.end(), invExpr1->expressions.begin(), invExpr1->expressions.end());
289 typename std::list<ExpressionInfo<vtype> >::iterator it = binaryExpr2->expressions.begin();
290 if (!oper::isPositive)
295 expressions.insert(expressions.end(), it, binaryExpr2->expressions.end());
299 typename std::list<ExpressionInfo<vtype> >::iterator it = invExpr2->expressions.begin();
300 if (!oper::isPositive)
305 expressions.insert(expressions.end(), it, invExpr2->expressions.end());
315 typedef typename oper::Positive opPositive;
316 typedef typename oper::Negative opNegative;
317 typename std::list<ExpressionInfo<vtype> >::iterator it = expressions.begin();
319 vtype val = it->expression->eval();
321 while (++it != expressions.end())
324 val = it->positive ? opPositive::eval(val, it->expression->eval()) : opNegative::eval(val, it->expression->eval());
334 if (!exp.expression->isConstant())
return false;
342 DependencyList dependencies;
346 DependencyList dep = exp.expression->getDependencies();
347 dependencies.insert(dep.begin(), dep.end());
353 template<
class vtype>
356 vtype operator()(
int a);
357 vtype operator()(
double a);
358 vtype operator()(std::string a);
364 int operator()(
int a) {
return a; }
365 int operator()(
double a) {
return static_cast<int>(a); }
366 int operator()(std::string a) {
return boost::lexical_cast<
int>(a); }
372 double operator()(
int a) {
return static_cast<double>(a); }
373 double operator()(
double a) {
return a; }
374 double operator()(std::string a) {
return boost::lexical_cast<
double>(a); }
380 std::string operator()(
int a) {
return boost::lexical_cast<std::string>(a); }
381 std::string operator()(
double a) {
return boost::lexical_cast<std::string>(a); }
382 std::string operator()(std::string a) {
return a; }
387 template<
class vtype>
391 template<
class atype>
392 vtype operator()(
const atype& a) {
return static_cast<vtype
>(a); }
395 template<
class vtype>
399 template<
class atype>
400 vtype operator()(
const atype& a) {
return boost::lexical_cast<vtype>(a); }
403 template<
class vtype,
class vtype_orig,
template<
class>
class CastType =
StaticCast>
411 pExpressionOrig expr;
413 TypecastOp(pExpressionOrig expr_) : expr(expr_) {}
418 return CastType<vtype>()(expr->eval());
420 catch(boost::bad_lexical_cast &e)
432 return expr->getDependencies();
438 template<
class ExpressionPo
inter>
439 DependencyList operator()(ExpressionPointer e) {
return e->getDependencies(); }
447 #endif // SCHNEK_EXPRESSION_HPP_ std::shared_ptr< Expression > pExpression
A pointer to an Expression.
Definition: expression.hpp:113
ReferencedValue(const pVariable &var_)
Construct with a value.
Definition: expression.hpp:160
Expression< vtype_orig >::pExpression pExpressionOrig
A pointer to an original Expression.
Definition: expression.hpp:408
DependencyList getDependencies()
returns the dependencies of the sub expression
Definition: expression.hpp:231
Definition: expression.hpp:354
Definition: expression.hpp:73
Definition: expression.hpp:153
bool isConstant()
The value of the external variable can change.
Definition: expression.hpp:206
Definition: expression.hpp:396
vtype eval()
Return the value.
Definition: expression.hpp:199
bool isConstant()
Constancy depends on the constancy of both expressions.
Definition: expression.hpp:331
DependencyList getDependencies()
returns the dependencies of the sub expression
Definition: expression.hpp:430
vtype eval()
Return the modified value.
Definition: expression.hpp:416
Definition: expression.hpp:79
vtype & getReference()
Return a reference to the value.
Definition: expression.hpp:145
Definition: expression.hpp:404
Definition: expression.hpp:96
Definition: expression.hpp:67
bool isConstant()
Constancy depends on the constancy of the expression.
Definition: expression.hpp:228
vtype eval()
Return the modified value.
Definition: expression.hpp:162
bool isConstant()
Constancy depends on the constancy of the variable.
Definition: expression.hpp:175
vtype eval()
Return the calculated value.
Definition: expression.hpp:314
vtype eval()
Return the stored value.
Definition: expression.hpp:137
Definition: expression.hpp:127
Definition: expression.hpp:251
vtype eval()
Return the modified value.
Definition: expression.hpp:225
Definition: expression.hpp:118
vtype operator()()
The () operator allows expressions to be used as function objects.
Definition: expression.hpp:108
Definition: expression.hpp:238
#define SCHNEK_TRACE_LOG(i, x)
Definition: logger.hpp:54
DependencyList getDependencies()
returns a list with the variable's id
Definition: expression.hpp:178
Value(vtype val_)
Construct with a value.
Definition: expression.hpp:135
Definition: expression.hpp:189
Definition: expression.hpp:215
bool isConstant()
A literal is a constant.
Definition: expression.hpp:143
Definition: expression.hpp:388
bool isConstant()
Constancy depends on the constancy of the expression.
Definition: expression.hpp:427
DependencyList getDependencies()
returns the joint dependencies of both sub expression
Definition: expression.hpp:340
Definition: expression.hpp:436
ExternalValue(vtype *var_)
Construct with a pointer to the value.
Definition: expression.hpp:196