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 "TEnum.h"
#include "TEnumConstant.h"
#include <cassert>
#include <string>
#include <sstream>
#include <vector>

Go to the source code of this file.

Functions

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

Function Documentation

template<typename MyEnum >
int StringToEnumValue ( std::string const &  enumConstName)

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 assert().

27  {
28  TEnum* en = TEnum::GetEnum(typeid(MyEnum));
29  if (en != nullptr){
30  if (TEnumConstant const* enc = en->GetConstant(enumConstName.c_str())){
31  return enc->GetValue();
32  }
33  }
34  assert(0);
35  return -1;
36 }
assert(m_qm.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 61 of file StringToEnumValue.h.

References run_regression::ret, and AlCaHLTBitMon_QueryRunRegistry::string.

61  {
62 
63  using std::vector;
64  using std::string;
65 
66  vector<int> ret;
67  vector<string>::const_iterator str=enumNames.begin();
68  for (;str!=enumNames.end();++str){
69  ret.push_back( StringToEnumValue<MyType>(*str));
70  }
71  return ret;
72 
73 } //