CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Fireworks/Core/src/FWExpressionEvaluator.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWExpressionEvaluator
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Fri Feb 29 13:39:56 PST 2008
00011 // $Id: FWExpressionEvaluator.cc,v 1.4 2012/06/26 22:13:03 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include <sstream>
00016 
00017 #include "Reflex/Object.h"
00018 
00019 // user include files
00020 #include "Fireworks/Core/interface/FWExpressionEvaluator.h"
00021 #include "Fireworks/Core/interface/FWExpressionException.h"
00022 #include "CommonTools/Utils/src/Grammar.h"
00023 #include "CommonTools/Utils/interface/Exception.h"
00024 
00025 #include "Fireworks/Core/src/expressionFormatHelpers.h"
00026 
00027 //
00028 // constants, enums and typedefs
00029 //
00030 
00031 //
00032 // static data member definitions
00033 //
00034 
00035 //
00036 // constructors and destructor
00037 //
00038 FWExpressionEvaluator::FWExpressionEvaluator(const std::string& iExpression,
00039                                              const std::string& iClassName) :
00040    m_className(iClassName),
00041    m_type(Reflex::Type::ByName(iClassName))
00042 {
00043    setExpression(iExpression);
00044 }
00045 
00046 // FWExpressionEvaluator::FWExpressionEvaluator(const FWExpressionEvaluator& rhs)
00047 // {
00048 //    // do actual copying here;
00049 // }
00050 
00051 FWExpressionEvaluator::~FWExpressionEvaluator()
00052 {
00053 }
00054 
00055 //
00056 // assignment operators
00057 //
00058 // const FWExpressionEvaluator& FWExpressionEvaluator::operator=(const FWExpressionEvaluator& rhs)
00059 // {
00060 //   //An exception safe implementation is
00061 //   FWExpressionEvaluator temp(rhs);
00062 //   swap(rhs);
00063 //
00064 //   return *this;
00065 // }
00066 
00067 //
00068 // member functions
00069 //
00070 void
00071 FWExpressionEvaluator::setExpression(const std::string& iExpression)
00072 {
00073    if(m_type != Reflex::Type() && iExpression.size()) {
00074       using namespace fireworks::expression;
00075 
00076       //Backwards compatibility with old format
00077       std::string temp = oldToNewFormat(iExpression);
00078 
00079       //now setup the parser
00080       using namespace boost::spirit::classic;
00081       reco::parser::ExpressionPtr tmpPtr;
00082       reco::parser::Grammar grammar(tmpPtr,m_type);
00083       try {
00084          if(parse(temp.c_str(), grammar.use_parser<1>() >> end_p, space_p).full) {
00085             m_expr = tmpPtr;
00086             m_expression = iExpression;
00087          } else {
00088             throw FWExpressionException("syntax error", -1);
00089             //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
00090          }
00091       } catch(const reco::parser::BaseException& e) {
00092          //NOTE: need to calculate actual position before doing the regex
00093          throw FWExpressionException(reco::parser::baseExceptionWhat(e), indexFromNewFormatToOldFormat(temp,e.where-temp.c_str(),iExpression));
00094          //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
00095       }
00096    } else {
00097       m_expression=iExpression;
00098    }
00099 }
00100 
00101 void
00102 FWExpressionEvaluator::setClassName(const std::string& iClassName)
00103 {
00104    //NOTE: How do we handle the case where the filter was created before
00105    // the library for the class was loaded and therefore we don't have
00106    // a Reflex dictionary for it?
00107 
00108    m_className = iClassName;
00109    m_type = Reflex::Type::ByName(iClassName);
00110    setExpression(m_expression);
00111 }
00112 
00113 //
00114 // const member functions
00115 //
00116 const std::string&
00117 FWExpressionEvaluator::expression() const
00118 {
00119    return m_expression;
00120 }
00121 
00122 double
00123 FWExpressionEvaluator::evalExpression(const void* iObject) const
00124 {
00125    if(m_expression.empty() || !m_expr.get()) {
00126       return 0;
00127    }
00128 
00129    Reflex::Object o(m_type, const_cast<void *>(iObject));
00130    return m_expr->value(o);
00131 }
00132 
00133 //
00134 // static member functions
00135 //