CMS 3D CMS Logo

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  : m_className(iClassName), m_type(edm::TypeWithDict::byName(iClassName)) {
39  setExpression(iExpression);
40 }
41 
42 // FWExpressionEvaluator::FWExpressionEvaluator(const FWExpressionEvaluator& rhs)
43 // {
44 // // do actual copying here;
45 // }
46 
48 
49 //
50 // assignment operators
51 //
52 // const FWExpressionEvaluator& FWExpressionEvaluator::operator=(const FWExpressionEvaluator& rhs)
53 // {
54 // //An exception safe implementation is
55 // FWExpressionEvaluator temp(rhs);
56 // swap(rhs);
57 //
58 // return *this;
59 // }
60 
61 //
62 // member functions
63 //
65  if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
66  using namespace fireworks::expression;
67 
68  //Backwards compatibility with old format
69  std::string temp = oldToNewFormat(iExpression);
70 
71  //now setup the parser
72  using namespace boost::spirit::classic;
74  reco::parser::Grammar grammar(tmpPtr, m_type);
75  try {
76  if (parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
77  m_expr = tmpPtr;
78  m_expression = iExpression;
79  } else {
80  throw FWExpressionException("syntax error", -1);
81  //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
82  }
83  } catch (const reco::parser::BaseException& e) {
84  //NOTE: need to calculate actual position before doing the regex
86  indexFromNewFormatToOldFormat(temp, e.where - temp.c_str(), iExpression));
87  //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
88  }
89  } else {
90  m_expression = iExpression;
91  }
92 }
93 
95  //NOTE: How do we handle the case where the filter was created before
96  // the library for the class was loaded and therefore we don't have
97  // a dictionary for it?
98 
99  m_className = iClassName;
100  m_type = edm::TypeWithDict::byName(iClassName);
102 }
103 
104 //
105 // const member functions
106 //
108 
109 double FWExpressionEvaluator::evalExpression(const void* iObject) const {
110  if (m_expression.empty() || !m_expr.get()) {
111  return 0;
112  }
113 
114  edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
115  return m_expr->value(o);
116 }
117 
118 //
119 // static member functions
120 //
reco::parser::ExpressionPtr m_expr
long indexFromNewFormatToOldFormat(const std::string &iNewFormat, long iNewFormatIndex, const std::string &iOldFormat)
vector< string > parse(string line, const string &delimiter)
void setClassName(const std::string &)
const std::string & expression() const
boost::spirit::classic::parser_error< reco::parser::SyntaxErrors > BaseException
Definition: Exception.h:33
FWExpressionEvaluator(const std::string &iExpression, const std::string &iClassName)
std::shared_ptr< ExpressionBase > ExpressionPtr
void setExpression(const std::string &)
static TypeWithDict byName(std::string const &name)
Definition: TypeWithDict.cc:74
const char * baseExceptionWhat(const BaseException &e)
returns the appropriate &#39;what&#39; message for the exception
Definition: Exception.h:36
double evalExpression(const void *) const
std::string oldToNewFormat(const std::string &iExpression)
HLT enums.