CMS 3D CMS Logo

Parameter.h
Go to the documentation of this file.
1 #ifndef L1Trigger_L1TCommon_l1t_Parameter_h
2 #define L1Trigger_L1TCommon_l1t_Parameter_h
3 
4 #include<map>
5 #include<list>
6 #include<vector>
7 #include<string>
8 #include<memory>
9 #include<iostream>
10 #include<algorithm>
11 #include<stdexcept>
12 #include<type_traits>
13 #include<cstring>
14 
15 namespace l1t {
16 
17 // assuming string is castable to T (some specific cases are defined in the end)
18 template<class T> T castTo(const char *arg);
19 
20 class Parameter {
21 private:
22  std::string id, type, procOrRole; // attributes
23  std::string scalarOrVector, delim; // setting could be one single parameter
24  std::map<std::string, std::vector<std::string> > table; // or a map of columns, no multimap here: column names are assumed be unique!
25  std::map<std::string,unsigned int> columnNameToIndex; // remember original positions of the columns
26 
27 public:
28  std::string getId (void) const noexcept { return id; }
29  std::string getProcOrRole(void) const noexcept { return procOrRole; }
30  std::string getType (void) const noexcept { return type; }
32 
33  bool isScalar(void) const noexcept {
34  if( type.find("vector") != std::string::npos ||
35  type.find("table") != std::string::npos )
36  return false;
37  return true;
38  }
39  bool isVector(void) const noexcept {
40  if( type.find("vector") == std::string::npos ) return false;
41  return true;
42  }
43  bool isTable(void) const noexcept {
44  if( type.find("table") == std::string::npos ) return false;
45  return true;
46  }
47 
48  // cast underlying scalarOrVector string to scalar T type
49  template<class T> T getValue(void) const {
50  if( !isScalar() ) throw std::runtime_error("The registered type: '" + type + "' is not a scalar -> try getVector() or getTable()");
51  return castTo<T>(scalarOrVector.c_str());
52  }
53  // cast underlying scalarOrVector string to a vector of elements of type T
54  template<class T> std::vector<T> getVector(void) const {
55  if( !isVector() ) throw std::runtime_error("The registered type: '" + type + "' is not a vector");
56  // split the vector into elements
57  const char *d = delim.c_str();
58  std::list<T> elements;
59  std::unique_ptr<char,void(*)(void*)> copy( strdup(scalarOrVector.c_str()), free );
60  char *saveptr;
61  for(const char *item = strtok_r(copy.get(),d,&saveptr); item != nullptr; item = strtok_r(nullptr,d,&saveptr) )
62  try {
63  elements.push_back( castTo<T>(item) );
64  } catch (std::runtime_error &e){
65  throw std::runtime_error( std::string(e.what()) + "; check if delimeter '" + delim + "' is correct" );
66  }
67  return std::vector<T>(elements.begin(),elements.end());
68  }
69  // cast each element of a column of table to a vector of elements of type T
70  template<class T> std::vector<T> getTableColumn(const char *colName) const {
71  const std::vector<std::string> &column = table.at(colName);
72  std::vector<T> retval(column.size());
73  std::transform(column.begin(),column.end(),retval.begin(), [](std::string a) ->T { return castTo<T>(a.c_str()); });
74  return retval;
75  }
76  // cast each element of a row of table to a vector of elements of type T
77  template<class T> std::map<std::string,T> getTableRow(unsigned long rowNum) const {
78  std::map<std::string,T> retval;
79  for(auto &column : table) // insert below is never going to fail as the source map doesn't have duplicated by design
80  retval.insert( std::make_pair(column.first, castTo<T>( column.second.at(rowNum).c_str() )) );
81  return retval;
82  }
83  // in case the order of columns in original table is important - use function below
84  std::map<std::string,unsigned int> getColumnIndices(void) const noexcept { return columnNameToIndex; }
85 
86  Parameter& operator=(const Parameter & s) = default;
87  Parameter& operator=( Parameter && s) = default; // should be noexcept
88  Parameter(const Parameter & s) = default;
89  Parameter( Parameter && s) = default; // should be noexcept
90 
91  Parameter(const char *id,
92  const char *procOrRole,
93  const char *type,
94  const char *value,
95  const char *delimeter=","
96  );
97  Parameter(const char *id,
98  const char *procOrRole,
99  const char *types,
100  const char *columns,
101  const std::vector<std::string>& rows,
102  const char *delimeter=","
103  );
104 
105  Parameter(void){}
106  ~Parameter(void){}
107 };
108 
109 // specializations for most of the fundamental types are provided (also covers simple typedefs)
110 template<> bool castTo<bool> (const char *arg);
111 template<> char castTo<char> (const char *arg);
112 template<> short castTo<short> (const char *arg);
113 template<> int castTo<int> (const char *arg);
114 template<> long castTo<long> (const char *arg);
115 template<> long long castTo<long long> (const char *arg);
116 template<> float castTo<float> (const char *arg);
117 template<> double castTo<double> (const char *arg);
118 template<> long double castTo<long double> (const char *arg);
119 template<> unsigned char castTo<unsigned char> (const char *arg);
120 template<> unsigned short castTo<unsigned short> (const char *arg);
121 template<> unsigned int castTo<unsigned int> (const char *arg);
122 template<> unsigned long castTo<unsigned long> (const char *arg);
123 template<> unsigned long long castTo<unsigned long long>(const char *arg);
124 
125 // apart from the types above there may still be some numeric types left
126 template<class T> T castToInt_impl(const char *arg, std::true_type);
127 template<class T> T castToInt_impl(const char *arg, std::false_type);
128 template<class T> T castTo_impl(const char *arg, std::true_type, std::false_type);
129 template<class T> T castTo_impl(const char *arg, std::false_type, std::true_type);
130 template<class T> T castTo_impl(const char *arg, std::false_type, std::false_type);
131 
132 // try to guess the type trait first
133 template<class T> T castTo(const char *arg) {
134  return castTo_impl<T>(arg, std::is_integral<T>(), std::is_floating_point<T>());
135 }
136 // integral type can be signed and unsigned
137 template<class T> T castTo_impl(const char *arg, std::true_type, std::false_type){
138  return castToInt_impl<T>(arg, std::is_unsigned<T>());
139 }
140 // unsigned case
141 template<class T> T castToInt_impl(const char *arg, std::true_type){
143 }
144 // signed case
145 template<class T> T castToInt_impl(const char *arg, std::false_type){
146  return castTo<long long>(arg);
147 }
148 // floating point type
149 template<class T> T castTo_impl(const char *arg, std::false_type, std::true_type){
150  return castTo<long double>(arg);
151 }
152 // last hope that a non-fundamental type T is initializable with a string
153 template<class T> T castTo_impl(const char *arg, std::false_type, std::false_type) {
154  return T(arg);
155 }
156 
157 } // end of l1t namespace
158 
159 #endif
160 
char castTo< char >(const char *arg)
Definition: Parameter.cc:95
long double castTo< long double >(const char *arg)
Definition: Parameter.cc:100
unsigned int castTo< unsigned int >(const char *arg)
Definition: Parameter.cc:117
std::vector< T > getTableColumn(const char *colName) const
Definition: Parameter.h:70
def copy(args, dbName)
std::map< std::string, T > getTableRow(unsigned long rowNum) const
Definition: Parameter.h:77
std::string getType(void) const noexcept
Definition: Parameter.h:30
Parameter & operator=(const Parameter &s)=default
delete x;
Definition: CaloConfig.h:22
std::vector< T > getVector(void) const
Definition: Parameter.h:54
T castTo_impl(const char *arg, std::true_type, std::false_type)
Definition: Parameter.h:137
std::map< std::string, std::vector< std::string > > table
Definition: Parameter.h:24
A arg
Definition: Factorize.h:38
bool isTable(void) const noexcept
Definition: Parameter.h:43
unsigned long long castTo< unsigned long long >(const char *arg)
Definition: Parameter.cc:109
std::string getValueAsStr(void) const noexcept
Definition: Parameter.h:31
std::string getProcOrRole(void) const noexcept
Definition: Parameter.h:29
unsigned long castTo< unsigned long >(const char *arg)
Definition: Parameter.cc:118
bool isScalar(void) const noexcept
Definition: Parameter.h:33
std::string scalarOrVector
Definition: Parameter.h:23
T castToInt_impl(const char *arg, std::true_type)
Definition: Parameter.h:141
T getValue(void) const
Definition: Parameter.h:49
std::string getId(void) const noexcept
Definition: Parameter.h:28
Definition: value.py:1
std::string delim
Definition: Parameter.h:23
float castTo< float >(const char *arg)
Definition: Parameter.cc:106
bool castTo< bool >(const char *arg)
Definition: Parameter.cc:80
long long castTo< long long >(const char *arg)
Definition: Parameter.cc:72
#define noexcept
std::string procOrRole
Definition: Parameter.h:22
Parameter(void)
Definition: Parameter.h:105
T castTo(const char *arg)
Definition: Parameter.h:133
std::string type
Definition: Parameter.h:22
std::map< std::string, unsigned int > getColumnIndices(void) const noexcept
Definition: Parameter.h:84
unsigned short castTo< unsigned short >(const char *arg)
Definition: Parameter.cc:116
double castTo< double >(const char *arg)
Definition: Parameter.cc:107
double a
Definition: hdecay.h:121
short castTo< short >(const char *arg)
Definition: Parameter.cc:96
long castTo< long >(const char *arg)
Definition: Parameter.cc:98
bool isVector(void) const noexcept
Definition: Parameter.h:39
~Parameter(void)
Definition: Parameter.h:106
std::map< std::string, unsigned int > columnNameToIndex
Definition: Parameter.h:25
long double T
std::string id
Definition: Parameter.h:22
unsigned char castTo< unsigned char >(const char *arg)
Definition: Parameter.cc:115
int castTo< int >(const char *arg)
Definition: Parameter.cc:97