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