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
24 BasicExpression::BasicExpression(const std::string& expr, const SeExpr2::ExprType& type)
25 : Expression(expr, type), dummyFunc(dummyFuncX, 0, 16) {}
26
27 BasicExpression::~BasicExpression() { clearVars(); }
28
29 template <class T_MAP>
30 void 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
35 void BasicExpression::clearVars() {
36 deleteAndClear(varmap);
37 funcmap.clear();
38 }
39
40 void BasicExpression::setExpr(const std::string& str) {
41 clearVars();
42 Expression::setExpr(str);
43 }
44
45 SeExpr2::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
64 SeExpr2::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 }
BasicExpression::setExpr
void setExpr(const std::string &str)
Definition: BasicExpression.cpp:40
SeExpr2::map
SeExpr2::MapFuncX map
BasicExpression::resolveFunc
SeExpr2::ExprFunc * resolveFunc(const std::string &name) const
Definition: BasicExpression.cpp:64
BasicExpression.h
BasicExpression::VectorRef
Definition: BasicExpression.h:39
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::ExprFunc
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
BasicExpression::dummyFunc
SeExpr2::ExprFunc dummyFunc
Definition: BasicExpression.h:73
BasicExpression::P
VectorRef P
Definition: BasicExpression.h:77
BasicExpression::varmap
VARMAP varmap
Definition: BasicExpression.h:80
BasicExpression::~BasicExpression
virtual ~BasicExpression()
Definition: BasicExpression.cpp:27
BasicExpression::resolveVar
SeExpr2::ExprVarRef * resolveVar(const std::string &name) const
Definition: BasicExpression.cpp:45
BasicExpression::v
ScalarRef v
Definition: BasicExpression.h:76
SeExpr2::ExprFunc::lookup
static const ExprFunc * lookup(const std::string &name)
Lookup a builtin function by name.
Definition: ExprFunc.cpp:130
deleteAndClear
void deleteAndClear(T_MAP &map)
Definition: BasicExpression.cpp:30
BasicExpression::clearVars
void clearVars()
Definition: BasicExpression.cpp:35
BasicExpression::u
ScalarRef u
Definition: BasicExpression.h:75
SeExpr2::ExprVarRef
abstract class for implementing variable references
Definition: Expression.h:45
BasicExpression::funcmap
FUNCMAP funcmap
Definition: BasicExpression.h:82
expr
</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")
BasicExpression::BasicExpression
BasicExpression(const std::string &expr, const SeExpr2::ExprType &type=SeExpr2::ExprType().FP(3))
Definition: BasicExpression.cpp:24