Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-09 23:33:15

0001 #ifndef CommonTools_Utils_TypedStringObjectMethodCaller_h
0002 #define CommonTools_Utils_TypedStringObjectMethodCaller_h
0003 /* \class TypedStringObjectMethodCaller
0004  * 
0005  * Object's method (or a chain of methods) caller functor with generic return-type, specified by string expression
0006  *
0007  */
0008 
0009 #include "FWCore/Utilities/interface/EDMException.h"
0010 #include "CommonTools/Utils/interface/parser/Exception.h"
0011 #include "CommonTools/Utils/interface/parser/MethodChain.h"
0012 #include "CommonTools/Utils/interface/parser/MethodChainGrammar.h"
0013 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0014 
0015 template <typename T, typename R, bool DefaultLazyness = false>
0016 struct TypedStringObjectMethodCaller {
0017   TypedStringObjectMethodCaller(const std::string expr, bool lazy = DefaultLazyness) : type_(typeid(T)) {
0018     using namespace boost::spirit::classic;
0019     reco::parser::MethodChainGrammar grammar(methodchain_, type_, lazy);
0020     const char* startingFrom = expr.c_str();
0021     try {
0022       if (!parse(startingFrom, grammar >> end_p, space_p).full) {
0023         throw edm::Exception(edm::errors::Configuration, "failed to parse \"" + expr + "\"");
0024       }
0025     } catch (reco::parser::BaseException& e) {
0026       throw edm::Exception(edm::errors::Configuration)
0027           << "MethodChainGrammer parse error:" << reco::parser::baseExceptionWhat(e) << " (char "
0028           << e.where - startingFrom << ")\n";
0029     }
0030   }
0031 
0032   R operator()(const T& t) const {
0033     edm::ObjectWithDict o(type_, const_cast<T*>(&t));
0034     edm::ObjectWithDict ret = methodchain_->value(o);
0035     return *static_cast<R*>(ret.address());
0036   }
0037 
0038 private:
0039   reco::parser::MethodChainPtr methodchain_;
0040   edm::TypeWithDict type_;
0041 };
0042 
0043 #endif