CMS 3D CMS Logo

Parameter.cc
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <memory>
4 
5 using namespace std;
6 
7 namespace l1t {
8 
9  Parameter::Parameter(
10  const char *id, const char *procOrRole, const char *type, const char *value, const char *delimeter) {
11  this->id = id;
12  this->procOrRole = procOrRole;
13  this->type = type;
14  this->scalarOrVector = value;
15  this->delim = delimeter;
16  }
17 
18  Parameter::Parameter(const char *id,
19  const char *procOrRole,
20  const char *types,
21  const char *columns,
22  const vector<string> &rows,
23  const char *delimeter) {
24  this->id = id;
25  this->procOrRole = procOrRole;
26  this->type = types;
27 
28  map<int, string> colIndexToName;
29  unique_ptr<char, void (*)(void *)> copy(strdup(columns), free);
30  unsigned long nItems = 0;
31  char *saveptr;
32  for (const char *item = strtok_r(copy.get(), delimeter, &saveptr); item != nullptr;
33  item = strtok_r(nullptr, delimeter, &saveptr), nItems++) {
34  // trim leading and trailing whitespaces
35  size_t pos = 0, len = strlen(item);
36  while (pos < len && isspace(item[pos]))
37  pos++;
38  while (len > 0 && isspace(item[--len]))
39  ;
40  string str = (pos < len + 1 ? string(item + pos, len + 1 - pos) : item);
41 
42  colIndexToName.insert(make_pair(nItems, str));
43  columnNameToIndex.insert(make_pair(str, nItems));
44  if (table.insert(make_pair(str, vector<string>(rows.size()))).second == false)
45  throw runtime_error("Duplicate column name: '" + str + "'");
46  }
47 
48  for (unsigned int r = 0; r < rows.size(); r++) {
49  unique_ptr<char, void (*)(void *)> copy(strdup(rows[r].c_str()), free);
50  for (unsigned int pos = 0; pos < nItems; pos++) {
51  char *item = strtok_r((pos == 0 ? copy.get() : nullptr), delimeter, &saveptr);
52  if (item == nullptr)
53  throw runtime_error("Too few elements in '" + rows[r] + "'");
54 
55  // trim leading and trailing whitespaces
56  size_t p = 0, len = strlen(item);
57  while (p < len && isspace(item[p]))
58  p++;
59  while (len > 0 && isspace(item[--len]))
60  ;
61 
62  table[colIndexToName[pos]][r] = (pos < len + 1 ? string(item + p, len + 1 - p) : item);
63  }
64  if (strtok_r(nullptr, delimeter, &saveptr) != nullptr)
65  throw runtime_error("Too many elements in '" + rows[r] + "', expected " + to_string(nItems));
66  }
67 
68  this->delim = delimeter;
69  }
70 
71  // following specifications take care of the basic types
72  template <>
73  long long castTo<long long>(const char *arg) {
74  char *endptr = nullptr;
75  long long retval = strtoll(arg, &endptr, 0);
76  if (*endptr == '\0')
77  return retval;
78  else
79  throw runtime_error("Cannot convert '" + string(arg) + "' to integral type");
80  }
81 
82  // simply cast the long long down
83  template <>
84  bool castTo<bool>(const char *arg) {
85  if (strlen(arg) > 3) {
86  // look for "true"
87  if (strstr(arg, "true") != nullptr && strstr(arg, "false") == nullptr)
88  return true;
89  // look for "false"
90  if (strstr(arg, "true") == nullptr && strstr(arg, "false") != nullptr)
91  return false;
92  }
93  // look for "a number
94  char *endptr = nullptr;
95  long retval = strtol(arg, &endptr, 0);
96  if (*endptr == '\0')
97  return retval;
98  // nothing worked
99  throw runtime_error("Cannot convert '" + string(arg) + "' to boolean");
100  }
101 
102  template <>
103  char castTo<char>(const char *arg) {
104  return castTo<long long>(arg);
105  }
106  template <>
107  short castTo<short>(const char *arg) {
108  return castTo<long long>(arg);
109  }
110  template <>
111  int castTo<int>(const char *arg) {
112  return castTo<long long>(arg);
113  }
114  template <>
115  long castTo<long>(const char *arg) {
116  return castTo<long long>(arg);
117  }
118 
119  template <>
120  long double castTo<long double>(const char *arg) {
121  char *endptr = nullptr;
122  long double retval = strtold(arg, &endptr);
123  if (*endptr == '\0')
124  return retval;
125  else
126  throw runtime_error("Cannot convert '" + string(arg) + "' to floating point type");
127  }
128  template <>
129  float castTo<float>(const char *arg) {
130  return castTo<long double>(arg);
131  }
132  template <>
133  double castTo<double>(const char *arg) {
134  return castTo<long double>(arg);
135  }
136 
137  template <>
138  unsigned long long castTo<unsigned long long>(const char *arg) {
139  char *endptr = nullptr;
140  unsigned long long retval = strtoull(arg, &endptr, 0);
141  if (*endptr == '\0')
142  return retval;
143  else
144  throw runtime_error("Cannot convert '" + string(arg) + "' to unsigned integral type");
145  }
146  template <>
147  unsigned char castTo<unsigned char>(const char *arg) {
149  }
150  template <>
151  unsigned short castTo<unsigned short>(const char *arg) {
153  }
154  template <>
155  unsigned int castTo<unsigned int>(const char *arg) {
157  }
158  template <>
159  unsigned long castTo<unsigned long>(const char *arg) {
161  }
162 
163 } // namespace l1t
char castTo< char >(const char *arg)
Definition: Parameter.cc:103
long double castTo< long double >(const char *arg)
Definition: Parameter.cc:120
unsigned int castTo< unsigned int >(const char *arg)
Definition: Parameter.cc:155
std::string to_string(const V &value)
Definition: OMSAccess.h:77
delete x;
Definition: CaloConfig.h:22
A arg
Definition: Factorize.h:31
unsigned long long castTo< unsigned long long >(const char *arg)
Definition: Parameter.cc:138
U second(std::pair< T, U > const &p)
unsigned long castTo< unsigned long >(const char *arg)
Definition: Parameter.cc:159
Definition: value.py:1
float castTo< float >(const char *arg)
Definition: Parameter.cc:129
bool castTo< bool >(const char *arg)
Definition: Parameter.cc:84
long long castTo< long long >(const char *arg)
Definition: Parameter.cc:73
unsigned short castTo< unsigned short >(const char *arg)
Definition: Parameter.cc:151
double castTo< double >(const char *arg)
Definition: Parameter.cc:133
short castTo< short >(const char *arg)
Definition: Parameter.cc:107
long castTo< long >(const char *arg)
Definition: Parameter.cc:115
rows
Definition: mysort.py:12
#define str(s)
unsigned char castTo< unsigned char >(const char *arg)
Definition: Parameter.cc:147
int castTo< int >(const char *arg)
Definition: Parameter.cc:111