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 // $Id: FWExpressionEvaluator.cc,v 1.5 2012/08/03 18:20:28 wmtan Exp $
12 //
13 
14 // system include files
15 #include <sstream>
16 
18 
19 // user include files
24 
26 
27 //
28 // constants, enums and typedefs
29 //
30 
31 //
32 // static data member definitions
33 //
34 
35 //
36 // constructors and destructor
37 //
39  const std::string& iClassName) :
40  m_className(iClassName),
41  m_type(edm::TypeWithDict::byName(iClassName))
42 {
43  setExpression(iExpression);
44 }
45 
46 // FWExpressionEvaluator::FWExpressionEvaluator(const FWExpressionEvaluator& rhs)
47 // {
48 // // do actual copying here;
49 // }
50 
52 {
53 }
54 
55 //
56 // assignment operators
57 //
58 // const FWExpressionEvaluator& FWExpressionEvaluator::operator=(const FWExpressionEvaluator& rhs)
59 // {
60 // //An exception safe implementation is
61 // FWExpressionEvaluator temp(rhs);
62 // swap(rhs);
63 //
64 // return *this;
65 // }
66 
67 //
68 // member functions
69 //
70 void
72 {
73  if(m_type != edm::TypeWithDict() && iExpression.size()) {
74  using namespace fireworks::expression;
75 
76  //Backwards compatibility with old format
77  std::string temp = oldToNewFormat(iExpression);
78 
79  //now setup the parser
80  using namespace boost::spirit::classic;
82  reco::parser::Grammar grammar(tmpPtr,m_type);
83  try {
84  if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
85  m_expr = tmpPtr;
86  m_expression = iExpression;
87  } else {
88  throw FWExpressionException("syntax error", -1);
89  //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
90  }
91  } catch(const reco::parser::BaseException& e) {
92  //NOTE: need to calculate actual position before doing the regex
94  //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
95  }
96  } else {
97  m_expression=iExpression;
98  }
99 }
100 
101 void
103 {
104  //NOTE: How do we handle the case where the filter was created before
105  // the library for the class was loaded and therefore we don't have
106  // a dictionary for it?
107 
108  m_className = iClassName;
109  m_type = edm::TypeWithDict::byName(iClassName);
111 }
112 
113 //
114 // const member functions
115 //
116 const std::string&
118 {
119  return m_expression;
120 }
121 
122 double
123 FWExpressionEvaluator::evalExpression(const void* iObject) const
124 {
125  if(m_expression.empty() || !m_expr.get()) {
126  return 0;
127  }
128 
129  edm::ObjectWithDict o(m_type, const_cast<void *>(iObject));
130  return m_expr->value(o);
131 }
132 
133 //
134 // static member functions
135 //
reco::parser::ExpressionPtr m_expr
long indexFromNewFormatToOldFormat(const std::string &iNewFormat, long iNewFormatIndex, const std::string &iOldFormat)
static TypeWithDict byName(std::string const &className)
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
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