CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions
StringToEnumValue.h File Reference
#include "FWCore/Utilities/interface/Exception.h"
#include <Reflex/Reflex.h>
#include <string>
#include <sstream>
#include <vector>

Go to the source code of this file.

Functions

template<class MyType >
int StringToEnumValue (const std::string &enumName)
 
template<class MyType >
std::vector< int > StringToEnumValue (const std::vector< std::string > &enumNames)
 

Function Documentation

template<class MyType >
int StringToEnumValue ( const std::string &  enumName)

Convert a string into the enum value it corresponds to. Example: int value = StringToEnumValue<EcalRecHit::Flags>("kGood");

Author
Stefano Argiro
Version
Id:
StringToEnumValue.h,v 1.1 2011/03/07 14:49:48 gpetrucc Exp
Date
04 Mar 2011

Definition at line 25 of file StringToEnumValue.h.

References edm::hlt::Exception.

25  {
26 
27  using namespace ROOT::Reflex;
28 
29  Reflex::Type rflxType = Reflex::Type::ByTypeInfo(typeid(MyType));
30  Reflex::Member member = rflxType.MemberByName(enumName);
31 
32  if (!member) {
33  std::ostringstream err;
34  err<<"StringToEnumValue Failure trying to convert " << enumName << " to int value";
35  throw cms::Exception("ConversionError",err.str());
36  }
37 
38  if (member.TypeOf().TypeInfo() != typeid(int)) {
39 
40  std::ostringstream err;
41  err << "Type "<< member.TypeOf().Name() << " is not Enum";
42  throw cms::Exception("ConversionError",err.str());
43  }
44  return Object_Cast<int>(member.Get());
45 
46 } //
template<class MyType >
std::vector<int> StringToEnumValue ( const std::vector< std::string > &  enumNames)

Convert a vector<string> into a vector<int> with the enum values it corresponds to. Example:

std::vector<std::string> names; names.push_back("kWeird"); names.push_back("kGood");

std::vector<int> ints = StringToEnumValue<EcalRecHit::Flags>(names);

std::copy(ints.begin(), ints.end(), std::ostream_iterator<int>(std::cout, "-"));

Definition at line 71 of file StringToEnumValue.h.

References run_regression::ret.

71  {
72 
73  using namespace ROOT::Reflex;
74  using std::vector;
75  using std::string;
76 
77  vector<int> ret;
78  vector<string>::const_iterator str=enumNames.begin();
79  for (;str!=enumNames.end();++str){
80  ret.push_back( StringToEnumValue<MyType>(*str));
81  }
82  return ret;
83 
84 } //