SeExpr
BasicExpression.cpp
Go to the documentation of this file.
1/*
2* Copyright Disney Enterprises, Inc. All rights reserved.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License
6* and the following modification to it: Section 6 Trademarks.
7* deleted and replaced with:
8*
9* 6. Trademarks. This License does not grant permission to use the
10* trade names, trademarks, service marks, or product names of the
11* Licensor and its affiliates, except as required for reproducing
12* the content of the NOTICE file.
13*
14* You may obtain a copy of the License at
15* http://www.apache.org/licenses/LICENSE-2.0
16*
17* @file BasicExpression.cpp
18* @brief A basic expression context for the expression previewer
19* @author aselle
20*/
21
22#include "BasicExpression.h"
23
25 : Expression(expr, type), dummyFunc(dummyFuncX, 0, 16) {}
26
28
29template <class T_MAP>
30void deleteAndClear(T_MAP& map) {
31 for (typename T_MAP::iterator i = map.begin(); i != map.end(); ++i) delete i->second;
32 map.clear();
33}
34
37 funcmap.clear();
38}
39
40void BasicExpression::setExpr(const std::string& str) {
41 clearVars();
42 Expression::setExpr(str);
43}
44
45SeExpr2::ExprVarRef* BasicExpression::resolveVar(const std::string& name) const {
46 if (name == "u")
47 return &u;
48 else if (name == "v")
49 return &v;
50 else if (name == "P")
51 return &P;
52 else {
53 // make a variable to resolve any unknown
54 VARMAP::iterator i = varmap.find(name);
55 if (i != varmap.end())
56 return i->second;
57 else {
58 varmap[name] = new VectorRef();
59 return varmap[name];
60 }
61 }
62}
63
64SeExpr2::ExprFunc* BasicExpression::resolveFunc(const std::string& name) const {
65 // check if it is builtin so we get proper behavior
66 if (SeExpr2::ExprFunc::lookup(name)) return 0;
67
68 funcmap[name] = true;
69 return &dummyFunc;
70}
void deleteAndClear(T_MAP &map)
SeExpr2::ExprFunc dummyFunc
SeExpr2::ExprFunc * resolveFunc(const std::string &name) const
virtual ~BasicExpression()
SeExpr2::ExprVarRef * resolveVar(const std::string &name) const
BasicExpression(const std::string &expr, const SeExpr2::ExprType &type=SeExpr2::ExprType().FP(3))
void setExpr(const std::string &str)
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
static const ExprFunc * lookup(const std::string &name)
Lookup a builtin function by name.
Definition: ExprFunc.cpp:130
abstract class for implementing variable references
Definition: Expression.h:45
SeExpr2::MapFuncX map
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")