CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/CommonTools/Utils/interface/StringToEnumValue.h

Go to the documentation of this file.
00001 #ifndef _CommonTools_Utils_StringToEnumValue_h_
00002 #define _CommonTools_Utils_StringToEnumValue_h_
00003 
00004 
00005 #include "FWCore/Utilities/interface/Exception.h"
00006 #include <Reflex/Reflex.h>
00007 #include <string>
00008 #include <sstream>
00009 #include <vector>
00010 
00011 
00024 template <class MyType> 
00025 int StringToEnumValue(const std::string & enumName){
00026   
00027   using namespace ROOT::Reflex;
00028   
00029   Reflex::Type rflxType = Reflex::Type::ByTypeInfo(typeid(MyType));
00030   Reflex::Member member = rflxType.MemberByName(enumName);
00031 
00032   if (!member) {
00033     std::ostringstream err;
00034     err<<"StringToEnumValue Failure trying to convert " << enumName << " to int value";
00035     throw cms::Exception("ConversionError",err.str());
00036   }
00037 
00038   if (member.TypeOf().TypeInfo() != typeid(int)) {
00039     
00040     std::ostringstream err;
00041     err << "Type "<<  member.TypeOf().Name() << " is not Enum";
00042     throw cms::Exception("ConversionError",err.str());
00043   }
00044   return Object_Cast<int>(member.Get());
00045 
00046 } // 
00047 
00048 
00070 template <class MyType> 
00071 std::vector<int> StringToEnumValue(const std::vector<std::string> & enumNames){
00072   
00073   using namespace ROOT::Reflex;
00074   using std::vector;
00075   using std::string;
00076 
00077   vector<int> ret;
00078   vector<string>::const_iterator str=enumNames.begin();
00079   for (;str!=enumNames.end();++str){
00080     ret.push_back( StringToEnumValue<MyType>(*str));
00081   }
00082   return ret;
00083 
00084 } // 
00085 
00086 #endif