CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/CommonTools/Utils/interface/StringObjectFunction.h

Go to the documentation of this file.
00001 #ifndef CommonTools_Utils_StringObjectFunction_h
00002 #define CommonTools_Utils_StringObjectFunction_h
00003 /* \class StringCutObjectSelector
00004  *
00005  * \author Luca Lista, INFN
00006  *
00007  * $Id: StringObjectFunction.h,v 1.5 2012/08/03 18:08:09 wmtan Exp $
00008  */
00009 #include "FWCore/Utilities/interface/EDMException.h"
00010 #include "CommonTools/Utils/src/ExpressionPtr.h"
00011 #include "CommonTools/Utils/src/ExpressionBase.h"
00012 #include "CommonTools/Utils/interface/expressionParser.h"
00013 #include "FWCore/Utilities/interface/ObjectWithDict.h"
00014 
00015 template<typename T, bool DefaultLazyness=false>
00016 struct StringObjectFunction {
00017   StringObjectFunction(const std::string & expr, bool lazy=DefaultLazyness) : 
00018     type_(typeid(T)) {
00019     if(! reco::parser::expressionParser<T>(expr, expr_, lazy)) {
00020       throw edm::Exception(edm::errors::Configuration,
00021                            "failed to parse \"" + expr + "\"");
00022     }
00023   }
00024   StringObjectFunction(const reco::parser::ExpressionPtr & expr) : 
00025     expr_(expr),
00026     type_(typeid(T)) {
00027   }
00028   double operator()(const T & t) const {
00029     edm::ObjectWithDict o(type_, const_cast<T *>(& t));
00030     return expr_->value(o);  
00031   }
00032 
00033 private:
00034   reco::parser::ExpressionPtr expr_;
00035   edm::TypeWithDict type_;
00036 };
00037 
00038 #endif