File indexing completed on 2024-04-06 12:01:18
0001 #ifndef _CommonTools_Utils_StringToEnumValue_h_
0002 #define _CommonTools_Utils_StringToEnumValue_h_
0003
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 #include "TEnum.h"
0006 #include "TEnumConstant.h"
0007 #include <cassert>
0008 #include <string>
0009 #include <sstream>
0010 #include <vector>
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 template <typename MyEnum>
0025 int StringToEnumValue(std::string const& enumConstName) {
0026 TEnum* en = TEnum::GetEnum(typeid(MyEnum));
0027 if (en != nullptr) {
0028 if (TEnumConstant const* enc = en->GetConstant(enumConstName.c_str())) {
0029 return enc->GetValue();
0030 }
0031 }
0032 assert(0);
0033 return -1;
0034 }
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 template <class MyType>
0057 std::vector<int> StringToEnumValue(const std::vector<std::string>& enumNames) {
0058 using std::string;
0059 using std::vector;
0060
0061 vector<int> ret;
0062 vector<string>::const_iterator str = enumNames.begin();
0063 for (; str != enumNames.end(); ++str) {
0064 ret.push_back(StringToEnumValue<MyType>(*str));
0065 }
0066 return ret;
0067
0068 }
0069
0070 #endif