SeExpr

EditableExpression.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 #include "Editable.h"
18 #include "EditableExpression.h"
19 #include <sstream>
20
21 bool ExprSpecParse(std::vector<Editable*>& literals,
22 std::vector<std::string>& variables,
23 std::vector<std::pair<int, int> >& comments,
24 const char* str);
25
26 EditableExpression::EditableExpression() {}
27
28 EditableExpression::~EditableExpression() { cleanup(); }
29
30 void EditableExpression::setExpr(const std::string& expr) {
31 // get rid of old data
32 cleanup();
33
34 // run parser
35 _expr = expr;
36 std::vector<std::pair<int, int> > comments;
37 ExprSpecParse(_editables, _variables, comments, _expr.c_str());
38
39 for (Editables::iterator it = _editables.begin(); it != _editables.end();) {
40 Editable& literal = **it;
41 int endPos = literal.endPos;
42 std::string comment = "";
43 for (size_t ci = 0; ci < comments.size(); ci++) {
44 if (comments[ci].first >= endPos) {
45 // check to make sure there is no newlines between end of editable and comment
46 size_t pos = _expr.find('\n', endPos);
47 if (pos == std::string::npos || pos >= (size_t)comments[ci].second) {
48 comment = _expr.substr(comments[ci].first, comments[ci].second - comments[ci].first);
49 break;
50 }
51 }
52 }
53 bool keepEditable = literal.parseComment(comment);
54 if (!keepEditable) { // TODO: this is potentially quadratic if we remove a bunch
55 delete &literal;
56 it = _editables.erase(it);
57 } else {
58 ++it;
59 }
60 }
61 }
62
63 void EditableExpression::cleanup() {
64 for (size_t i = 0; i < _editables.size(); i++) delete _editables[i];
65 _editables.clear();
66 _variables.clear();
67 }
68
69 std::string EditableExpression::getEditedExpr() const {
70 int offset = 0;
71 std::stringstream stream;
72 for (size_t i = 0, sz = _editables.size(); i < sz; i++) {
73 Editable& literal = *_editables[i];
74 stream << _expr.substr(offset, literal.startPos - offset);
75 literal.appendString(stream);
76 offset = literal.endPos;
77 }
78 stream << _expr.substr(offset, _expr.size() - offset);
79 return stream.str();
80 }
81
82 void EditableExpression::updateString(const EditableExpression& other) {
83 // TODO: move semantics?
84 _variables = other._variables;
85 _expr = other._expr;
86 _variables = other._variables;
87 for (size_t i = 0, sz = _editables.size(); i < sz; i++) {
88 Editable& literal = *_editables[i];
89 Editable& otherLiteral = *other._editables[i];
90 assert(literal.controlsMatch(otherLiteral));
91 literal.updatePositions(otherLiteral);
92 }
93 }
94
95 bool EditableExpression::controlsMatch(const EditableExpression& other) const {
96 if (_editables.size() != other._editables.size()) return false;
97
98 for (size_t i = 0, sz = _editables.size(); i < sz; i++) {
99 const Editable& literal = *_editables[i];
100 const Editable& otherLiteral = *other._editables[i];
101 if (!literal.controlsMatch(otherLiteral)) return false;
102 }
103 return true;
104 }
EditableExpression::setExpr
void setExpr(const std::string &expr)
Set's expressions and parses it into "control editable form".
Definition: EditableExpression.cpp:30
Editable
Definition: Editable.h:40
EditableExpression
Factors a SeExpr into an editable expression with controls (i.e. value boxes, curve boxes)
Definition: EditableExpression.h:29
Editable.h
variables
</pre > Once we have this we need an instance to store our variable and provide a reference to that We make it because it may be useful to use the same ExprVarRef from multiple expressions ! For if you have expressions that all have access to the same variables
Definition: tutorial.txt:128
EditableExpression::~EditableExpression
~EditableExpression()
Definition: EditableExpression.cpp:28
EditableExpression::cleanup
void cleanup()
clean memeory
Definition: EditableExpression.cpp:63
EditableExpression::EditableExpression
EditableExpression()
Definition: EditableExpression.cpp:26
EditableExpression::controlsMatch
bool controlsMatch(const EditableExpression &other) const
Check if the other editable expression has editables that all match i.e. the controls are same.
Definition: EditableExpression.cpp:95
ExprSpecParse
bool ExprSpecParse(std::vector< Editable * > &literals, std::vector< std::string > &variables, std::vector< std::pair< int, int > > &comments, const char *str)
it
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
Editable::updatePositions
void updatePositions(const Editable &other)
Definition: Editable.h:46
Editable::controlsMatch
virtual bool controlsMatch(const Editable &) const =0
Editable::endPos
int endPos
Definition: Editable.h:42
EditableExpression::updateString
void updateString(const EditableExpression &other)
Update the string refered to into the controls (this is only valid if controlsmatch)
Definition: EditableExpression.cpp:82
Editable::appendString
virtual void appendString(std::stringstream &stream) const =0
EditableExpression::getEditedExpr
std::string getEditedExpr() const
Return a reconstructed expression using all the editable's current values.
Definition: EditableExpression.cpp:69
Editable::parseComment
virtual bool parseComment(const std::string &comment)=0
parses a comment. if false is returned then delete the control from the editable
EditableExpression::_variables
std::vector< std::string > _variables
Definition: EditableExpression.h:33
EditableExpression::_expr
std::string _expr
Definition: EditableExpression.h:30
Editable::startPos
int startPos
Definition: Editable.h:42
EditableExpression.h
EditableExpression::_editables
std::vector< Editable * > _editables
Definition: EditableExpression.h:32
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")