CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWExpressionEvaluator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWExpressionEvaluator
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Fri Feb 29 13:39:56 PST 2008
11 //
12 
13 // system include files
14 #include <sstream>
15 
17 
18 // user include files
23 
25 
26 //
27 // constants, enums and typedefs
28 //
29 
30 //
31 // static data member definitions
32 //
33 
34 //
35 // constructors and destructor
36 //
38  const std::string& iClassName) :
39  m_className(iClassName),
40  m_type(edm::TypeWithDict::byName(iClassName))
41 {
42  setExpression(iExpression);
43 }
44 
45 // FWExpressionEvaluator::FWExpressionEvaluator(const FWExpressionEvaluator& rhs)
46 // {
47 // // do actual copying here;
48 // }
49 
51 {
52 }
53 
54 //
55 // assignment operators
56 //
57 // const FWExpressionEvaluator& FWExpressionEvaluator::operator=(const FWExpressionEvaluator& rhs)
58 // {
59 // //An exception safe implementation is
60 // FWExpressionEvaluator temp(rhs);
61 // swap(rhs);
62 //
63 // return *this;
64 // }
65 
66 //
67 // member functions
68 //
69 void
71 {
72  if(m_type != edm::TypeWithDict() && iExpression.size()) {
73  using namespace fireworks::expression;
74 
75  //Backwards compatibility with old format
76  std::string temp = oldToNewFormat(iExpression);
77 
78  //now setup the parser
79  using namespace boost::spirit::classic;
81  reco::parser::Grammar grammar(tmpPtr,m_type);
82  try {
83  if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
84  m_expr = tmpPtr;
85  m_expression = iExpression;
86  } else {
87  throw FWExpressionException("syntax error", -1);
88  //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
89  }
90  } catch(const reco::parser::BaseException& e) {
91  //NOTE: need to calculate actual position before doing the regex
93  //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
94  }
95  } else {
96  m_expression=iExpression;
97  }
98 }
99 
100 void
102 {
103  //NOTE: How do we handle the case where the filter was created before
104  // the library for the class was loaded and therefore we don't have
105  // a dictionary for it?
106 
107  m_className = iClassName;
108  m_type = edm::TypeWithDict::byName(iClassName);
110 }
111 
112 //
113 // const member functions
114 //
115 const std::string&
117 {
118  return m_expression;
119 }
120 
121 double
122 FWExpressionEvaluator::evalExpression(const void* iObject) const
123 {
124  if(m_expression.empty() || !m_expr.get()) {
125  return 0;
126  }
127 
128  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
129  return m_expr->value(o);
130 }
131 
132 //
133 // static member functions
134 //
reco::parser::ExpressionPtr m_expr
long indexFromNewFormatToOldFormat(const std::string &iNewFormat, long iNewFormatIndex, const std::string &iOldFormat)
void setClassName(const std::string &)
boost::spirit::classic::parser_error< reco::parser::SyntaxErrors > BaseException
Definition: Exception.h:37
Evaluator * parse(const T &text)
FWExpressionEvaluator(const std::string &iExpression, const std::string &iClassName)
void setExpression(const std::string &)
boost::shared_ptr< ExpressionBase > ExpressionPtr
static TypeWithDict byName(std::string const &name)
Definition: TypeWithDict.cc:60
double evalExpression(const void *) const
const char * baseExceptionWhat(const BaseException &e)
returns the appropriate &#39;what&#39; message for the exception
Definition: Exception.h:40
size_t size() const
std::string oldToNewFormat(const std::string &iExpression)
const std::string & expression() const