CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/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 "FWCore/Utilities/interface/MemberWithDict.h"
00007 #include "FWCore/Utilities/interface/ObjectWithDict.h"
00008 #include "FWCore/Utilities/interface/TypeWithDict.h"
00009 #include <string>
00010 #include <sstream>
00011 #include <vector>
00012 
00013 
00026 template <class MyType> 
00027 int StringToEnumValue(const std::string & enumName){
00028   
00029   edm::TypeWithDict dataType(typeid(MyType));
00030   edm::MemberWithDict member = dataType.dataMemberByName(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 member.get().objectCast<int>();
00045 
00046 } // 
00047 
00048 
00070 template <class MyType> 
00071 std::vector<int> StringToEnumValue(const std::vector<std::string> & enumNames){
00072   
00073   using std::vector;
00074   using std::string;
00075 
00076   vector<int> ret;
00077   vector<string>::const_iterator str=enumNames.begin();
00078   for (;str!=enumNames.end();++str){
00079     ret.push_back( StringToEnumValue<MyType>(*str));
00080   }
00081   return ret;
00082 
00083 } // 
00084 
00085 #endif