CMS 3D CMS Logo

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

◆ StringToEnumValue() [1/2]

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 25 of file StringToEnumValue.h.

References cms::cuda::assert().

25  {
26  TEnum* en = TEnum::GetEnum(typeid(MyEnum));
27  if (en != nullptr) {
28  if (TEnumConstant const* enc = en->GetConstant(enumConstName.c_str())) {
29  return enc->GetValue();
30  }
31  }
32  assert(0);
33  return -1;
34 }
assert(be >=bs)

◆ StringToEnumValue() [2/2]

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 57 of file StringToEnumValue.h.

References runTheMatrix::ret, str, AlCaHLTBitMon_QueryRunRegistry::string, and trackerHitRTTI::vector.

57  {
58  using std::string;
59  using std::vector;
60 
61  vector<int> ret;
62  vector<string>::const_iterator str = enumNames.begin();
63  for (; str != enumNames.end(); ++str) {
64  ret.push_back(StringToEnumValue<MyType>(*str));
65  }
66  return ret;
67 
68 } //
ret
prodAgent to be discontinued
#define str(s)