CMS 3D CMS Logo

Functions

/data/refman/pasoursint/CMSSW_5_3_3/src/CommonTools/Utils/interface/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 Exception.

                                                 {
  
  using namespace ROOT::Reflex;
  
  Reflex::Type rflxType = Reflex::Type::ByTypeInfo(typeid(MyType));
  Reflex::Member member = rflxType.MemberByName(enumName);

  if (!member) {
    std::ostringstream err;
    err<<"StringToEnumValue Failure trying to convert " << enumName << " to int value";
    throw cms::Exception("ConversionError",err.str());
  }

  if (member.TypeOf().TypeInfo() != typeid(int)) {
    
    std::ostringstream err;
    err << "Type "<<  member.TypeOf().Name() << " is not Enum";
    throw cms::Exception("ConversionError",err.str());
  }
  return Object_Cast<int>(member.Get());

} // 
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 runTheMatrix::ret.

                                                                        {
  
  using namespace ROOT::Reflex;
  using std::vector;
  using std::string;

  vector<int> ret;
  vector<string>::const_iterator str=enumNames.begin();
  for (;str!=enumNames.end();++str){
    ret.push_back( StringToEnumValue<MyType>(*str));
  }
  return ret;

} //