CMS 3D CMS Logo

Functions

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CommonTools/Utils/interface/StringToEnumValue.h File Reference

#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/MemberWithDict.h"
#include "FWCore/Utilities/interface/ObjectWithDict.h"
#include "FWCore/Utilities/interface/TypeWithDict.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.5 2012/08/28 22:28:38 wmtan Exp
Date:
04 Mar 2011

Definition at line 27 of file StringToEnumValue.h.

References edm::TypeWithDict::dataMemberByName(), dtTPAnalyzer_cfg::dataType, Exception, edm::MemberWithDict::get(), and edm::MemberWithDict::typeOf().

                                                 {
  
  edm::TypeWithDict dataType(typeid(MyType));
  edm::MemberWithDict member = dataType.dataMemberByName(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 member.get().objectCast<int>();

} // 
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, and AlCaHLTBitMon_QueryRunRegistry::string.

                                                                        {
  
  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;

} //